Learning the shell — Lesson 5: Manipulating Files

Shuma Mizuno
Sep 6, 2018 · 4 min read

学習日: 180906
所要時間: 3時間

Manipulating Files

この記事では下記の4つのコマンドについて学ぶ。

cp — copy files and directories
mv — move or rename files and directories
rm — remove files and directories
mkdir — create directories

4つは頻繁に使用する コマンドで
ファイルとディレクトリを操作できる。

なぜGUIではなくCLIでやるのか
柔軟性があるから。
複雑なタスクの場合にGUIよりも容易にファイル操作ができる。

Wildcard

一定のパターンに基づいてファイル名やディレクトリを補完することができる。

*: 0文字以上の任意の文字を表す
?: 任意の1文字を表す
[]: []に含まれているいずれかの文字
[!]: [ ]に含まれない文字

cp

ファイルとディレクトリのコピーができる。

$ cp file1 file2

file1をfile2にコピー
予めfile2が存在していた場合、黙って上書きする。

$ cp -i file1 file2

-i オプションをつけるとfile2があった際は確認してくれる。

$ cp file1 dir1

file1をdir1にコピー

$ cp -R dir1 dir2

dir1をdir2にコピー
予めdir2が存在していた場合、中身だけをコピーする。

mv

ファイルとディレクトリの移動、リネームができる。

$ mv file1 file2

file1をfile2にリネーム
予めfile2が存在していた場合、黙ってfile1の情報で中身が差し替えられる。

$ mv -i file1 file2

-i オプションをつけるとfile2があった際は確認してくれる。

$ mv file1 file2 file3 dir1

file1, file2, file3をdir1に移動
もしdir1がなければエラーになる。

$ mv dir1 dir2

dir1をdir2にリネーム
予めdir2が存在していた場合、dir1がdir2の中に移動される。

rm

ファイルとディレクトリの削除ができる。

$ rm file1 file2

file1とfile2の削除

$ rm -i file1 file2

ファイル削除前に確認する 。

$ rm -r dir1 dir2

dir1 dir2をディレクトリ内のデータごと削除

Be careful with rm!

rmコマンドで削除したデータは復元できない。

意図しないデータを削除するとシステムに深刻なダメージを与える危険があるため注意。(特にワイルドカード)

ワイルドカードを使用する際には一度rmコマンドをlsコマンドに置き換えて試すと良い。

mkdir

ディレクトリの作成ができる。

その他

1通り実際に試したり
気になった部分深めたりした。

質問事項

特になし

所感

便利なのはわかった。
慣れるまでGUIの3倍ぐらい時間かかりそうな遅さなのでがんばりたい。

    Shuma Mizuno

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade