[小西ホームページ]   [目次・索引]   [前の授業]   [次の授業]

コンピュータIID(UNIXとHTML)第3回

目次
3.1 ファイルの管理(2)
3.1.1 ディレクトリの作成
3.1.2 詳しいファイルの一覧
3.1.3 カレント・ディレクトリ
3.1.4 ディレクトリの中へのコピー
3.1.5 ディレクトリの中への移動
3.1.6 ディレクトリのコピー
3.1.7 ディレクトリの移動
3.1.8 ディレクトリの削除
3.2 演習3
3.3 レポート課題
3.4 参考文献
索引

3.1 ファイルの管理(2)

3.1.1 ディレクトリの作成

前回注意した通り、「ターミナル」を起動したら、カレント・ディレクトリを授業用のディレクトリにしてください。

asiaa1:~ b08a001$ cd comp2d
asiaa1:~/comp2d b08a001$

mkdirコマンド (MaKe DIRectory)は、ディレクトリを作成します。 形式は、

mkdir directory

です。

asiaa1:~/comp2d b08a001$ mkdir test03
asiaa1:~/comp2d b08a001$ ls
test03
asiaa1:~/comp2d b08a001$

前回、lsコマンドはファイルの一覧を表示すると言いましたが、ファイルだけでなくディレクトリも表示します。 より正確に言うと、カレント・ディレクトリの内容の一覧を表示します。

ここで、テスト用のテキスト・ファイルを、2つ作成します。 Jeditを起動して、以下のテキストを入力してください。

今日は晴れです。

そして、test03a.txtというファイルに保存します。 ファイルは授業用のディレクトリcomp2dに保存し、保存するときのエンコーディングは「Unicode(UTF-8)」、改行タイプは「Unix(LF)」にしてください。

もう1つ、

今日は曇りです。

というファイルも作成し、test03b.txtというファイルに保存します。 ファイルは、先ほど作成したディレクトリtest03に保存してください。

lsコマンドは、カレント・ディレクトリだけでなく、一般のディレクトリの内容の一覧も表示します。 形式は、

ls directory

です。 ディレクトリの指定を省略すると、カレント・ディレクトリの内容の一覧を表示します。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ ls test03
test03b.txt
asiaa1:~/comp2d b08a001$

3.1.2 詳しいファイルの一覧

上記のlsコマンドでは、ファイルとディレクトリの区別がつきません。 区別するためには、オプションを使います。

lsコマンドに-Fオプションを付けると、ディレクトリの名前の最後に記号"/"が付きます。 また、lsコマンドに-lオプションを付けると、詳しい一覧表示になります。 この読み方は、先頭が"-"ならファイル、"d"ならディレクトリです。 その他の項目については、次回以降説明します。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ ls -F
test03/  test03a.txt
asiaa1:~/comp2d b08a001$ ls -l
drwxr-xr-x 2 b08a001 cis 4096 10 10 13:30 test03
-rw-r--r-- 1 b08a001 cis   68 10 10 13:30 test03a.txt
asiaa1:~/comp2d b08a001$

3.1.3 カレント・ディレクトリ

pwdコマンド (Print Working Directory)は、カレント・ディレクトリを表示します。 スラッシュ(/)で区切られた最後の文字列がディレクトリ名です。

cdコマンド (Change Directory)は、カレント・ディレクトリを変更します。 形式は、

cd directory

です。

cdコマンドでは、特別なディレクトリ名".."が使えます。 これは、一つ上のディレクトリを表します。 つまり、コマンドcd ..で、カレント・ディレクトリは一つ上になります。

下記の例では、カレント・ディレクトリをtest03に変更してから、元に戻しています。

asiaa1:~/comp2d b08a001$ pwd
/Users/b08a001/comp2d
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ cd test03
asiaa1:~/comp2d/test03 b08a001$ pwd
/Users/b08a001/comp2d/test03
asiaa1:~/comp2d/test03 b08a001$ ls
test03b.txt
asiaa1:~/comp2d/test03 b08a001$ cd ..
asiaa1:~/comp2d b08a001$ pwd
/Users/b08a001/comp2d
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$

3.1.4 ディレクトリの中へのコピー

cpコマンドは、ファイルのコピーでしたが、

cp file directory

という形式もあります。 これで、ファイル file をディレクトリ directory の中へコピーします。 コピー先のファイル名は、コピー元と同じです。

もし、すでにコピー先にファイルがあるなら、そのファイルは上書きされます。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ cp test03a.txt test03
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ ls test03
test03a.txt  test03b.txt
asiaa1:~/comp2d b08a001$

3.1.5 ディレクトリの中への移動

mvコマンドは、ファイルの移動(名称変更)でしたが、

mv file directory

という形式もあります。 これで、ファイル file をディレクトリ directory の中へ移動します。 移動先のファイル名は、移動元と同じです。

もし、すでに移動先にファイルがあるなら、そのファイルは上書きされます。 下記の例では、ファイルの上書きを避けるため、ファイルtest03a.txtをtest03c.txtにコピーしています。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ cp test03a.txt test03c.txt
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt  test03c.txt
asiaa1:~/comp2d b08a001$ mv test03c.txt test03
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ ls test03
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d b08a001$

3.1.6 ディレクトリのコピー

ディレクトリをコピーするには、ファイルのコピーと同じく、cpコマンドを使います。 形式は

cp -r directory1 directory2

です。 ディレクトリ directory2 が存在すれば、 directory2 の中へコピーします。 存在しなければ、 directory2 へコピーします。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$ ls test03
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d b08a001$ cp -r test03 test03d
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt  test03d
asiaa1:~/comp2d b08a001$ ls test03d
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d b08a001$

3.1.7 ディレクトリの移動

ディレクトリを移動するには、ファイルの移動と同じく、mvコマンドを使います。 形式は

mv directory1 directory2

です。 ディレクトリ directory2 が存在すれば、 directory2 の中へ移動します。 存在しなければ、 directory2 へ移動します。 directory1directory2 が同じディレクトリの中であり、 directory2 が存在しなければ、名称変更になります。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt  test03d
asiaa1:~/comp2d b08a001$ ls test03d
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d b08a001$ mv test03d test03e
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt  test03e
asiaa1:~/comp2d b08a001$ ls test03e
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d b08a001$

3.1.8 ディレクトリの削除

rmdirコマンド (ReMove DIRectory)は、ディレクトリを削除します。 形式は

rmdir directory

です。

このコマンドは、ディレクトリだけを削除するので、ディレクトリが空でなければエラーとなります。 下記の例では、ディレクトリtest03eを削除するために、その内容をすべて削除してからrmdirコマンドを使っています。

asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt  test03e
asiaa1:~/comp2d b08a001$ cd test03e
asiaa1:~/comp2d/test03e b08a001$ ls
test03a.txt  test03b.txt  test03c.txt
asiaa1:~/comp2d/test03e b08a001$ rm test03a.txt
asiaa1:~/comp2d/test03e b08a001$ rm test03b.txt
asiaa1:~/comp2d/test03e b08a001$ rm test03c.txt
asiaa1:~/comp2d/test03e b08a001$ ls
asiaa1:~/comp2d/test03e b08a001$ cd ..
asiaa1:~/comp2d b08a001$ rmdir test03e
asiaa1:~/comp2d b08a001$ ls
test03  test03a.txt
asiaa1:~/comp2d b08a001$

3.2 演習3

コマンドを使って、ディレクトリを作成し、その中にファイルのコピーを作成してください。

まず、Jeditを使って、コピー元のテキスト(例えば

今日は雨です。

)を入力し、report03.txtというファイルに保存します。 ファイルは、ディレクトリcomp2dに保存します。 そして、コマンドを使ってディレクトリreport03を作成し、その中にファイルをコピーしてください。

asiaa1:~/comp2d b08a001$ cat report03.txt
今日は雨です。
asiaa1:~/comp2d b08a001$ ???
asiaa1:~/comp2d b08a001$ ???
asiaa1:~/comp2d b08a001$ cd report03
asiaa1:~/comp2d/report03 b08a001$ cat report03.txt
今日は雨です。
asiaa1:~/comp2d/report03 b08a001$ cd ..
asiaa1:~/comp2d b08a001$

3.3 レポート課題

今日の演習3の答案(「ターミナル」画面のコピー)をメールで提出してください。 差出人は学内のメール・アドレス(b08a001@cis.twcu.ac.jpなど)とし、宛先はkonishi@cis.twcu.ac.jpとします。 メールの本文には、学生番号、氏名、科目名、授業日(10月10日)を明記してください。


3.4 参考文献


[小西ホームページ]   [目次・索引]   [前の授業]   [次の授業]

2008年10月10日更新
小西 善二郎 <konishi@cis.twcu.ac.jp>
Copyright (C) 2008 Zenjiro Konishi. All rights reserved.