シェル vol.5

  • コピー、移動、削除コマンド
  • 総集編

cp・・・コピーコマンド

cp [オプション] [コピー元のディレクトリパス] [コピー先のディレクトリパス]
//例)ファイルのコピー
cp test.txt test2.txt
//例)ディレクトリのコピー
cp -r test test2
cp -r test/ test2 //test内のデータをtest2にコピーする。
//例)ファイルの上書きの確認
cp -i copy.txt test/copy.txt
overwrite test/copy.txt? (y/n [n]) n
not overwritten

オプション

-r ディレクトリのコピー
-i ファイル上書きの確認

mv・・・ファイルの移動/名前の変更

mv [オプション] [移動元ファイル] [移動先ファイル]
//例)ファイルの移動
mv copy.txt test2/
//例)ディレクトリの移動
mv test2/ test3/ //ディレクトリtest2をディレクトリtest3の下に移動。
//例)ファイル名の変更
mv copy.txt move.txt //copy.txtのファイル名をmove.txtに変更。

オプション

-i ファイル上書きの確認

rm・・・ファイルの削除

rm [オプション] [パス]
例)ファイルの削除
rm copy.txt
例)ディレクトリの削除
rm -r test2

オプション

-r ディレクトリの削除
-f 削除の問い合わせをしない

課題1

  1. ホーム移動
  2. sample/grep/testディレクトリを作成後、testに移動。
  3. 4回で使用したディレクトリをtest以下に移動


cd ~
mkdir -p sample/grep/test
mv shell_training sample/grep/test/
tree shell_training
shell_training/

-- books
-- bocchan.txt
-- gingatetsudono_yoru.txt
`-- hashire_merosu.txt
-- error.txt
-- grep.txt
-- output.txt
-- test1
-- sample1.txt
-- sample2.txt
`-- sample3.txt

`-- ?\202??\202??\202\231?\202??\203??\202??\202\231.txt

2 directories, 10 files

2009/4/14追記

間違えがあったようなのでやり直しました。

$ cd ~
$ mkdir -p sample/grep/test
$ cd sample/grep/test
$ mv ~/shell_training/ ./
$ ls -l
total 0
drwxr-xr-x  8 shota  staff  272  4 14 17:22 shell_training
$ tree shell_training
shell_training
|-- books
|   |-- bocchan.txt
|   |-- gingatetsudono_yoru.txt
|   `-- hashire_merosu.txt
|-- error.txt
|-- grep.txt
|-- output.txt
|-- test1
|   |-- sample1.txt
|   |-- sample2.txt
|   `-- sample3.txt
`-- ?\202??\202??\202\231?\202??\203??\202??\202\231.txt

課題2

  1. ホーム以下にlogs/ディレクトリを作成。
  2. 探索
    1. そのまま
    2. 出力を見やすい形に直してみる
    3. logs/検索内容毎のファイルに出力
    4. 追記の形で先ほどのファイルに出力
$ mkdir ~/logs/
$ grep -r aozora ./shell_training/books/
$ grep -nr aozora ./shell_training/books/ | less
$ grep -nr aozora ./shell_training/books > ~/logs/aozora.log
$ grep -nr 夏目 ./shell_training/books >> ~/logs/aozora.log

課題3

  1. ホーム移動
  2. sample/prems/testディレクトリを一度で作成
  3. prems/に移動
  4. testディレクトリの所有者をnobodyに変更後、リスト表示で確認
  5. cdでtestディレクトリに移動してみる
  6. testディレクトリを削除する。
$ cd ~
$ mkdir -p sample/prems/test
$ cd sample/prems/
$ sudo chown nobody test/
$ ls -l
$ cd test/
$ cd ..
$ rm -r test/

tail/head

tail・・・ファイルの末尾を表示する。

tail [ファイル名]

head・・・ファイルの最初の部分を表示する。

head [ファイル名]