前回、現場で必須!超基本Linuxコマンド Vol.1の続き。
今回は、ファイルやディレクトリの操作をしてみよう。

前回、現場で必須!超基本Linuxコマンド Vol.1の続き。
今回は、ファイルやディレクトリの操作と確認をしてみよう。

現場では変更が行われる処理の前後には必ず状態を確認する。なぜならば、思った通りに動作しているかを確認するためだ。これは必ず必要な作業なので押さえておこう。

ls

「list segmentation」の略
ファイルやディレクトリの一覧を表示する。

1
2
# ls
sample1.txt sample2.txt sample3.txt sample4.txt sample5.txt temp1 temp2

ほとんどのコマンドには、オプションがあり、オプションを設定することで、こまやかな処理を実行する事ができるようになる。

例えば、ls単体で実行すると先ほど結果を書いた通り、ファイルやディレクトリの一覧を出してくれる。今度は-lオプションを渡して、みる。

1
2
3
4
5
6
7
8
9
# ls -l
total 0
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample1.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample2.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample3.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample4.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample5.txt
drwxr-xr-x 2 atani staff 68 9 5 16:24 temp1
drwxr-xr-x 7 atani staff 238 9 5 16:24 temp2

すると、ファイルやディレクトリのより詳細な情報を表示してくれるようになった。
今度は、-tオプションを付け加えてみよう。

1
2
3
4
5
6
7
8
9
# ls -l -t
total 0
drwxr-xr-x 7 atani staff 238 9 5 16:24 temp2
drwxr-xr-x 2 atani staff 68 9 5 16:24 temp1
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample1.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample2.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample3.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample4.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample5.txt

-t オプションはリストをタイムスタンプ順が最新のもの順に並び替えてくれる。また、オプションは複数渡すことが可能である。さらに、オプションは同時に記述することも可能である場合がある。
上記のls -l -tls -lt と同等である。

mkdir

「make directory」の略で、ディレクトリを作成する。

例:

1
2
3
# mkdir temp3
# ls -ld temp3/
drwxr-xr-x 2 atani staff 68 9 7 20:56 temp3/

mv

「move」の略でファイルやディレクトリの移動または名前を変更する。

例:

1
2
3
# mv temp3 temp4
# ls -ld temp4
drwxr-xr-x 2 atani staff 68 9 7 20:56 temp4

cp

「copy」の略でファイルやディレクトリをコピーする。

例:

1
2
3
4
# cp sample6.txt sample7.txt
# ls -l sample6.txt sample7.txt
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample6.txt
-rw-r--r-- 1 atani staff 0 9 7 20:58 sample7.txt

rm

「remove」の略でファイルやディレクトリを削除する。

例:

1
2
3
4
# rm sample7.txt
# ls -l sample6.txt sample7.txt
ls: sample7.txt: No such file or directory
-rw-r--r-- 1 atani staff 0 9 3 21:47 sample6.txt

man

ここまで足早に見てきたが、それぞれのコマンドにはもちろんオプションが用意されている。しかし、どのオプションが使えるの?と迷ったときに使うのがmanコマンドだ。

man ls と実行すれば、ls コマンドの説明ページが出てくる。

読み終わったら q で終了することができる。

最後に総集編の動画をどうぞ。

[command.gif]