多くのコマンドでは、引数にファイル名を書く代わりに、ファイル名のパターンが書けます。 この機能をうまく利用しますと、能率的なファイル管理が可能になります。
ファイル名のパターンを表すには、特殊な記号が使われます。 この記号を、 ワイルド・カード と呼びます。 ここでは、"*"と"?"の2つを使います。 記号"*"は任意の文字列にマッチし、記号"?"は任意の文字にマッチします。 つまり、1文字を表すには"?"を書き、0文字以上を表すには"*"を書きます。
例として、カレント・ディレクトリが次の内容だとします。
b04a001@AsiaA1:~% cd comp2d b04a001@AsiaA1:~/comp2d% ls France/ english01.doc month01.txt month03.txt Germany/ english01.txt month02.txt b04a001@AsiaA1:~/comp2d%
テキスト・ファイルを一覧表示したければ、その拡張子は.txtなので、パターンは*.txtとなります。
b04a001@AsiaA1:~/comp2d% ls *.txt english01.txt month01.txt month02.txt month03.txt b04a001@AsiaA1:~/comp2d%
12か月のファイルを一覧表示したければ、そのファイル名はmonth(2桁の数).txtなので、パターンはmonth??.txtとなります。
b04a001@AsiaA1:~/comp2d% ls month??.txt month01.txt month02.txt month03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでワイルド・カードを使う方法は、ないようです。
今まで、テキスト・ファイルの中身を表示するのに、catコマンドを使ってきました。 このコマンドでは、長文のテキストの場合、「ターミナル」の画面に収まりません。 そのときは、テキストのスクロールができるコマンドを利用します。
moreコマンド は、テキストを一画面ずつ表示します。 形式は
more file
です。 returnキーで1行スクロールし、スペース・キーで1画面スクロールします。 q キーで終了します。
次のlessコマンドやlvコマンドのほうがより便利なので、このコマンドはあまり使われません。
lessコマンド も、テキストを一画面ずつ表示します。 この名前は、moreの反対の動きもできるという意味です。 形式は
less file
です。 キー操作は以下の通りです
キー操作 | 動作 |
---|---|
return, 下矢印 | 1行スクロール |
スペース、右矢印 | 1画面スクロール |
k, 上矢印 | 1行バック・スクロール |
b, 左矢印 | 1画面バック・スクロール |
q | 終了 |
lvコマンド も、テキストを一画面ずつ表示します。 これは、日本語など、多くの言語のテキスト・ファイルを表示できるようにしたものです。 形式は
lv file
です。 キー操作は、lessコマンドと同じです。
ファイルを作成するには、何らかのアプリケーションを利用して、ファイルに保存します。 空のファイルなら、コマンドで作成できます。
touchコマンド は、空のファイルを作成します。 形式は
touch file
です。
ファイル file がすでに存在するなら、このファイルの最終更新日時が今の日時に変わります。
b04a001@AsiaA1:~/comp2d% ls -l empty??.txt /bin/ls: No match. b04a001@AsiaA1:~/comp2d% touch empty01.txt b04a001@AsiaA1:~/comp2d% ls -l empty??.txt -rw-r--r-- 1 b04a001 cis 0 11 15 13:45 empty01.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでファイルを作成する方法はないようです。
mvコマンド (MoVe)は、ファイルを移動します。 形式は2通りあります。
第1の形式は、
mv file1 file2
です。 これで、ファイル file1 をファイル file2 へ移動します。 同じディレクトリの中なら、名称変更になります。
第2の形式は、
mv file directory
です。 これで、ファイル file をディレクトリ directory の中へ移動します。 移動先のファイル名は、移動元と同じです。
もし、すでに移動先にファイルがあるなら、そのファイルは上書きされます。
b04a001@AsiaA1:~/comp2d% mv empty01.txt empty02.txt b04a001@AsiaA1:~/comp2d% ls -l empty??.txt -rw-r--r-- 1 b04a001 cis 0 11 15 13:45 empty02.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでファイルを移動するには、ファイルのアイコンを移動先へドロップします。
cpコマンド (CoPy)は、ファイルをコピーします。 形式は2通りあります。
第1の形式は、
cp file1 file2
です。 これで、ファイル file1 をファイル file2 へコピーします。
第2の形式は、
cp file directory
です。 これで、ファイル file をディレクトリ directory の中へコピーします。 コピー先のファイル名は、コピー元と同じです。
もし、すでにコピー先にファイルがあるなら、そのファイルは上書きされます。
b04a001@AsiaA1:~/comp2d% cp empty02.txt empty03.txt b04a001@AsiaA1:~/comp2d% ls -l empty??.txt -rw-r--r-- 1 b04a001 cis 0 11 15 13:45 empty02.txt -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでファイルをコピーするには、Optionキーを押しながら、ファイルのアイコンをコピー先へドロップします。
rmコマンド (ReMove)は、ファイルを削除します。 形式は、
rm file
です。
設定によっては、本当にファイルを削除するかと確認されます。 確認なしで即座に削除するには
rm -f file
と入力します。
オプション -f とワイルド・カードを使いますと、複数のファイルが即座に削除されます。 このコマンドは、慎重に入力してください。
b04a001@AsiaA1:~/comp2d% rm empty02.txt remove empty02.txt? yes b04a001@AsiaA1:~/comp2d% ls -l empty??.txt -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでファイルを削除するには、ファイルのアイコンをゴミ箱にドロップしてから、ゴミ箱を空にします。
mkdirコマンド (MaKe DIRectory)は、ディレクトリを作成します。 形式は、
mkdir directory
です。
b04a001@AsiaA1:~/comp2d% ls -l test01 ls: test01: No such file or directory b04a001@AsiaA1:~/comp2d% mkdir test01 b04a001@AsiaA1:~/comp2d% mv empty03.txt test01 b04a001@AsiaA1:~/comp2d% ls -l test01 total 0 -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでディレクトリを作成するには、メニューバーの「ファイル」をクリックし、「新規フォルダ」を選択します。
ディレクトリを移動するには、ファイルの移動と同じく、mvコマンドを使います。 形式は
mv directory1 directory2
です。 ディレクトリ directory2 が存在すれば、 directory2 の中へ移動します。 存在しなければ、 directory2 へ移動します。 directory1 と directory2 が同じディレクトリの中であり、 directory2 が存在しなければ、名称変更になります。
b04a001@AsiaA1:~/comp2d% ls -l test02 ls: test02: No such file or directory b04a001@AsiaA1:~/comp2d% mv test01 test02 b04a001@AsiaA1:~/comp2d% ls -l test01 ls: test01: No such file or directory b04a001@AsiaA1:~/comp2d% ls -l test02 total 0 -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでディレクトリを移動するには、ディレクトリのアイコンを移動先へドロップします。
ディレクトリをコピーするには、ファイルのコピーと同じく、cpコマンドを使います。 形式は
cp -r directory1 directory2
です。 ディレクトリ directory2 が存在すれば、 directory2 の中へコピーします。 存在しなければ、 directory2 へコピーします。
b04a001@AsiaA1:~/comp2d% ls -l test03 ls: test03: No such file or directory b04a001@AsiaA1:~/comp2d% cp -r test02 test03 b04a001@AsiaA1:~/comp2d% ls -l test02 total 0 -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d% ls -l test03 total 0 -rw-r--r-- 1 b04a001 cis 0 11 15 14:00 empty03.txt b04a001@AsiaA1:~/comp2d%
なお、Finderでディレクトリをコピーするには、Optionキーを押しながら、ディレクトリのアイコンをコピー先へドロップします。
rmdirコマンド (ReMove DIRectory)は、ディレクトリを削除します。 形式は
rmdir directory
です。 このコマンドは、ディレクトリだけを削除するので、ディレクトリが空でなければエラーとなります。
空でないディレクトリをまるごと削除するには、rmコマンドを使います。 形式は、
rm -r directory
です。
設定によっては、本当にファイルとディレクトリを削除するかと確認されます。 確認なしで即座に削除するには
rm -rf directory
と入力します。 このコマンドは、慎重に入力してください。
b04a001@AsiaA1:~/comp2d% rmdir test02 rmdir: test02: Directory not empty b04a001@AsiaA1:~/comp2d% ls -l test02 total 0 -rw-r--r-- 1 b04a001 cis 0 11 15 13:50 empty03.txt b04a001@AsiaA1:~/comp2d% rm test02/empty03.txt remove test02/empty03.txt? yes b04a001@AsiaA1:~/comp2d% rmdir test02 b04a001@AsiaA1:~/comp2d% ls -l test02 ls: test02: No such file or directory b04a001@AsiaA1:~/comp2d%
なお、Finderでディレクトリを削除するには、ディレクトリのアイコンをゴミ箱にドロップしてから、ゴミ箱を空にします。
今日は、ファイルを管理するコマンドを紹介しました。 その大半の機能は、Finderでもできることです。 Finderでできないことは、次のようなものです。
ここで、コマンドの組合せとは、ファイル管理コマンドとテキスト処理コマンドを両方利用することです。 コマンドの結果表示がテキストなので、コマンドが組み合わせられるのです。
例1. ディレクトリの内容総数
lsコマンドでディレクトリの内容一覧を出力し、wcコマンドでその行数を数えれば、ディレクトリの内容総数が分かります。
b04a001@AsiaA1:~/comp2d% ls France/ english01.doc month01.txt month03.txt Germany/ english01.txt month02.txt b04a001@AsiaA1:~/comp2d% ls | wc 7 7 81 b04a001@AsiaA1:~/comp2d%
例2. ファイルのみの一覧表示
lsコマンドでディレクトリの内容一覧を出力し、grepコマンドで先頭が"d"の行を探索すれば、ディレクトリのみの一覧表示ができます。 同様に、grepコマンドで先頭が"-"の行を探索すれば、ファイルのみの一覧表示ができます。 ここで、grepコマンドのパターンに記号"^"を書きますと、「〜を先頭とする」という意味になります。
b04a001@AsiaA1:~/comp2d% ls -l total 44 drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 France/ drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 Germany/ -rw-r--r-- 1 b04a001 cis 19456 10 25 13:45 english01.doc -rw-r--r-- 1 b04a001 cis 42 10 18 14:00 english01.txt -rw-r--r-- 1 b04a001 cis 86 10 4 13:45 month01.txt -rw-r--r-- 1 b04a001 cis 76 10 11 13:30 month02.txt -rw-r--r-- 1 b04a001 cis 77 10 11 13:45 month03.txt b04a001@AsiaA1:~/comp2d% ls -l | grep ^d drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 France/ drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 Germany/ b04a001@AsiaA1:~/comp2d% ls -l | grep ^- -rw-r--r-- 1 b04a001 cis 19456 10 25 13:45 english01.doc -rw-r--r-- 1 b04a001 cis 42 10 18 14:00 english01.txt -rw-r--r-- 1 b04a001 cis 86 10 4 13:45 month01.txt -rw-r--r-- 1 b04a001 cis 76 10 11 13:30 month02.txt -rw-r--r-- 1 b04a001 cis 77 10 11 13:45 month03.txt b04a001@AsiaA1:~/comp2d%
例3. 最大サイズのファイル
lsコマンドでディレクトリの内容一覧を出力し、sortコマンドでファイル・サイズの部分を整列すれば、最大サイズのファイルが分かります。
b04a001@AsiaA1:~/comp2d% ls -l total 44 drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 France/ drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 Germany/ -rw-r--r-- 1 b04a001 cis 19456 10 25 13:45 english01.doc -rw-r--r-- 1 b04a001 cis 42 10 18 14:00 english01.txt -rw-r--r-- 1 b04a001 cis 86 10 4 13:45 month01.txt -rw-r--r-- 1 b04a001 cis 76 10 11 13:30 month02.txt -rw-r--r-- 1 b04a001 cis 77 10 11 13:45 month03.txt b04a001@AsiaA1:~/comp2d% ls -l | sort +4n total 44 -rw-r--r-- 1 b04a001 cis 42 10 18 14:00 english01.txt -rw-r--r-- 1 b04a001 cis 76 10 11 13:30 month02.txt -rw-r--r-- 1 b04a001 cis 77 10 11 13:45 month03.txt -rw-r--r-- 1 b04a001 cis 86 10 4 13:45 month01.txt drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 France/ drwxr-xr-x 2 b04a001 cis 512 11 1 13:45 Germany/ -rw-r--r-- 1 b04a001 cis 19456 10 25 13:45 english01.doc b04a001@AsiaA1:~/comp2d%
コマンドを使って、授業用ディレクトリcomp2dの内容を調べてください。
問1. テキスト・ファイル(〜.txt)はいくつありますか。 個数が表示されるようにしてください。
問2. 最大サイズのファイル(ディレクトリも含む)はどれですか。 一行だけ表示されるようにしてください。
余力のある人は、次の問にも答えてください。
問3. 最新のファイル(ディレクトリは含まない)はどれですか。 一行だけ表示されるようにしてください。
今日の演習8の答案(「ターミナル」画面のコピー)をメールでkonishi@twcu.ac.jp宛に提出してください。 メールを送るときは、大学のパソコンを使うか、大学のメール・サーバに接続するかして、差出人が大学のメール・アドレスになるようにしてください。 メールの本文には、学生番号、氏名、科目名、授業日(11月15日)を明記してください。