ファイルの扱い
ファイルの内容表示
ファイルのコピー
ファイルの移動
ファイル名の変更
ファイルの削除
ファイル名や属性情報での検索
テキストファイルの内容から検索する
ファイルのリンク
lessコマンド
ファイルの内容がテキストファイルなら,lessコマンドで表示するのが一番便利。
[toshim@gw2000 ~]$less /etc/passwd
root:j9b4ORHYWi6aA:0:0:root:/root:/bin/bash
bin:*:1:1:bin:/bin:
daemon:*:2:2:daemon:/sbin:
adm:*:3:4:adm:/var/adm:
lp:*:4:7:lp:/var/spool/lpd:
sync:*:5:0:sync:/sbin:/bin/sync
shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown
halt:*:7:0:halt:/sbin:/sbin/halt
mail:*:8:12:mail:/var/spool/mail:
news:*:9:13:news:/var/spool/news:
uucp:*:10:14:uucp:/var/spool/uucp:
operator:*:11:0:operator:/root:
games:*:12:100:games:/usr/games:
gopher:*:13:30:gopher:/usr/lib/gopher-data:
ftp:*:14:50:FTP User:/home/ftp:
nobody:*:99:99:Nobody:/:
toshim:1A0KDMJ1/fyJ6:500:100:Toshi,,,,:/home/toshim:/bin/bash
/etc/passwd (END)
|
lessで表示中のキー操作
| キー |
動作 |
| j |
1行スクロールアップ |
| k |
1行スクロールダウン |
| d |
半画面分スクロールアップ |
| スペースキー |
1画面分スクロールアップ |
| u |
半画面分スクロールダウン |
| /string |
表示しているページの中からstringに合致する文字列を検索する |
| h |
ヘルプ表示 |
| q |
終了 |
less以外にも次のコマンドでテキストファイルを表示できる。
cat
|
ファイルの内容を単純に標準出力に出力するだけ。
(実行例)
$ cat /etc/password
|
more
|
lessより低機能。スペースキーで1画面ずつ閲覧できる。逆戻りはできない。
(実行例)
$ more /etc/password
|
hexdumpコマンド
テキストファイル以外のファイルはhexdumpコマンドでダンプ表示する。
hexdumpの実行例
[toshim@gw2000 ~]$hexdump /etc/passwd
0000000 6f72 746f 6a3a 6239 4f34 4852 5759 3669
0000010 4161 303a 303a 723a 6f6f 3a74 722f 6f6f
0000020 3a74 622f 6e69 622f 7361 0a68 6962 3a6e
0000030 3a2a 3a31 3a31 6962 3a6e 622f 6e69 0a3a
(以下省略)
|
hexdumpのオプション。
|
-b
|
1バイト単位に8進数でダンプ
|
|
-c
|
1バイト単位にASCII キャラクタでダンプ
|
|
-d
|
2バイト単位に10進数でダンプ
|
|
-o
|
2バイト単位に8進数でダンプ
|
|
-x
|
2バイト単位に16進数でダンプ。デフォルトの表示よりフィールド間のスペースを多くとる。
|
cpコマンドの形式
cp [options] source dest
cp [options] source1 source2 ... directory
注意点
コピー先は省略できない。(WindowsのCOPYはコピー先を省略するとカレントディレクトリにコピーする)
元ファイルを複数指定するときに,コピー先がディレクトリ以外だとエラーとなる。
同名のファイルがコピー先に存在する場合,それが書き込み可なら警告なしに上書きする。(オプションで変更可能)
'*'や'?'といったワイルドカードはWindowsより高機能。(詳しくはファイル名展開を参照)
cpコマンドの実行例
[toshim@gw2000 ~/work]$cp /etc/passwd . ←/etc/passwdをカレントディレクトリにコピー
[toshim@gw2000 ~/work]$ls -l
total 1
-rw-r--r-- 1 toshim users 584 Apr 30 19:27 passwd
[toshim@gw2000 ~/work]$cp /etc/passwd pw ←/etc/passwdをpwという名前にしてカレントディレクトリにコピー
[toshim@gw2000 ~/work]$ls -l
total 4
-rw-r--r-- 1 toshim users 584 Apr 30 19:27 passwd
-rw-r--r-- 1 toshim users 584 Apr 30 19:30 pw
[toshim@gw2000 ~/work]$cp /etc/hosts /etc/group . ←/etc/hostsと/etc/groupをカレントディレクトリにコピー
[toshim@gw2000 ~/work]$ls -l
total 4
-rw-r--r-- 1 toshim users 365 Apr 30 19:35 group
-rw-r--r-- 1 toshim users 43 Apr 30 19:35 hosts
-rw-r--r-- 1 toshim users 584 Apr 30 19:27 passwd
-rw-r--r-- 1 toshim users 584 Apr 30 19:30 pw
|
[toshim@gw2000 ~/work]$cp -R test test2
[toshim@gw2000 ~/work]$ls -l
total 31
-rw-r--r-- 1 toshim users 365 Apr 30 19:35 group
-rw-r--r-- 1 toshim users 43 Apr 30 19:35 hosts
-rw-r--r-- 1 toshim users 584 Apr 30 19:27 passwd
-rw-r--r-- 1 toshim users 584 Apr 30 19:30 pw
drwxr-xr-x 2 toshim users 1024 May 1 21:07 test
drwxr-xr-x 2 toshim users 1024 May 2 20:59 test2
|
cpコマンドの主要なオプション
| -a |
コピー元の構成や属性などをなるべくそのままにしてコピーする。-doRオプションと同じ。 |
-d
|
コピー元がシンボリックリンクの場合,リンクしたファイルをコピーするのではなく,シンボリックリンクの情報をコピーする。
|
| -f |
コピー先に同名のファイルあり書き込み不可でも,強制的にコピーする。 |
| -i |
コピー先に同名のファイルある場合などは確認メッセージを出す。 |
-p
|
元のファイルの所有者,グループ,モード,タイムスタンプをそのままにしてコピーする。
|
-u
|
コピー先に同じファイルや新しいファイルがある場合はコピーしない。
|
| -v
|
実行結果を逐一表示する。
|
-R
|
サブディレクトリを再帰的にコピーする。
|
mvコマンドの形式
mv [options] source dest
mv [options] source1 source2 ... directory
注意点
同名のファイルがコピー先に存在する場合,それが書き込み可なら警告なしに置き換える。(オプションで変更可能)
ディレクトリを移動すると,その下のファイルやサブディレクトリも全部移動する。
mvコマンドの主要なオプション
| -f |
コピー先に同名のファイルあり書き込み不可でも,強制的にコピーする。 |
| -i |
コピー先に同名のファイルある場合などは確認メッセージを出す。 |
-u
|
コピー先に同じファイルや新しいファイルがある場合はコピーしない。
|
| -v
|
実行結果を逐一表示する。
|
ファイル名の変更にもmvコマンドを使う。
使い方は移動と同じ。
rmコマンドの形式
rm [options] name...
注意点
ファイルが書き込み可の場合は,警告なしに削除する。(オプションで変更可能)
rmコマンドの主要なオプション
| -f |
削除するファイルが存在しない場合や,書き込み不可の場合でも警告を出さない。 |
| -i |
削除する前に確認確認メッセージを出す。 |
| -v
|
実行結果を逐一表示する。
|
-rまたは-R
|
サブディレクトリを全部削除する。
|
findコマンドの形式
find [path...] [expression]
expressionはオプション,判別式,アクションを複数指定できる。
実行例
/etcディレクトリの下にある,ファイル名が"*.conf"に一致するファイルを探す
[toshim@gw2000 ~]$ find /etc -name *.conf -print
/etc/host.conf
/etc/nsswitch.conf
/etc/ld.so.conf
/etc/httpd/conf/access.conf
/etc/httpd/conf/httpd.conf
(以下省略)
|
/etcディレクトリの下にあるファイルで,24時間以内に内容を修正したものを探す
[toshim@gw2000 ~]$ find /etc -mtime 1 -print
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/conf.linuxconf
/etc/smb.conf
(以下省略)
|
主な判別式
| -iname pattern |
大文字と小文字を同一文字としてファイル名を検索 |
| -mtime n |
n*24時間前にファイルの内容が更新されたもの |
| -name pattern |
ファイル名による検索 |
| -newer file
|
fileよりも新しいファイル
|
-user name
|
所有者による検索
|
主なアクション
| -exec command |
commandを実行する |
| -fprint file |
検索したファイルのフルパス名をfileに出力する |
| -print |
検索したファイルのフルパス名を標準出力に出力する |
| -ls
|
検索したファイルを'ls
-dils'の形式で標準出力に出力する
|
grepコマンドの形式
grep [options] pattern file
実行例
/etc/rc.d/init.dディレクトリの下にあるファイルで,中にhttpdという文字列を含むものを探す
[toshim@gw2000 ~]$ grep httpd /etc/rc.d/init.d/*
/etc/rc.d/init.d/httpd:# processname: httpd
/etc/rc.d/init.d/httpd:# pidfile: /var/run/httpd.pid
/etc/rc.d/init.d/httpd:# config: /etc/httpd/conf/access.conf
/etc/rc.d/init.d/httpd:# config: /etc/httpd/conf/httpd.conf
/etc/rc.d/init.d/httpd:# config: /etc/httpd/conf/srm.conf
/etc/rc.d/init.d/httpd: echo -n "Starting httpd: "
/etc/rc.d/init.d/httpd: daemon httpd
/etc/rc.d/init.d/httpd: touch /var/lock/subsys/httpd
(以下省略)
|
主なオプション
| -c |
一致した行の内容ではなく一致した行の数を表示する |
| -i |
パターンマッチのときに,大文字と小文字を同じ文字と見なす |
| -l |
一致した行の内容を表示せずに,ファイル名だけを表示する |
| -n |
行番号を表示する |
| -L |
一致しなかったファイルの名前を表示する |
ハードリンクとシンボリックリンク
リンクは他のファイルやディレクトリへのポインタ。
Windowsのショートカットと似たもの。
リンクに対する操作が,ポイント先のファイルやディレクトリへの操作となる。
Unixにはハードリンクとシンボリックリンクの二つがある。
シンボリックリンクはポインタとして,ファイル/ディレクトリ名を使うもの。
ポインタとして名前を登録するだけなので,ファイルやディレクトリの実体がないものに対してリンクを作ることができる。
もちろん,実体がないものへのシンボリックリンクにアクセスしても,何も実行できない。
ハードリンクはポインタとしてファイルシステムの管理情報であるi-nodeを使う。
i-nodeはファイルの実体を記録したディスク上のポインタなので,実体がないものにはリンクを作れない。
一般ユーザはディレクトリに対するハードリンクを作成できない。
ハードリンクしたファイルはi-nodeを共有するのでファイルの属性が同じ値となる。
ファイルシステムの管理情報を使うので,削除するときに注意しないと管理情報の不整合を起こすこともある。

lnコマンド
リンクを作成するときはlnコマンドを使う。
ln -s <リンク先> <作成するリンク>
|
シンボリックリンクを作成する
|
ln <リンク先> <作成するリンク>
|
ハードリンクを作成する
|
リンクを削除するときはrmコマンドを使う。
[toshim@gw2000 ~/work]$ln -s test1 sltest1
[toshim@gw2000 ~/work]$ls -l
total 31
lrwxrwxrwx 1 toshim users 5 May 20 18:48 sltest1 -> test1 <-- lntest1はtest1へのポインタ
-rwxr--r-- 1 toshim users 25 May 4 17:52 test1
[toshim@gw2000 ~/work]$cat sltest1
This is a file named test1. <-- test1の内容が表示される
[toshim@gw2000 ~/work]$ln -s test10 sltest10
[toshim@gw2000 ~/work]$ls -l
total 32
lrwxrwxrwx 1 toshim users 5 May 20 19:11 sltest1 -> test1
lrwxrwxrwx 1 toshim users 6 May 20 19:16 sltest10 -> test10 <-- test10というファイルがなくてもリンクを作れる
-rwxr--r-- 1 toshim users 25 May 4 17:52 test1
[toshim@gw2000 ~/work]$ln test1 hltest1
[toshim@gw2000 ~/work]$ls -l
total 33
-rw-r--r-- 2 toshim users 21 May 20 19:11 hltest1
lrwxrwxrwx 1 toshim users 5 May 20 19:11 sltest1 -> test1
lrwxrwxrwx 1 toshim users 6 May 20 19:16 sltest10 -> test10
-rw-r--r-- 2 toshim users 21 May 20 19:11 test1
[toshim@gw2000 ~/work]$cat hltest1
This is a test file.
[toshim@gw2000 ~/work]$rm test1 <-- リンク先を削除
[toshim@gw2000 ~/work]$cat hltest1 <-- ハードリンクはファイルの実体へのポインタなので実体にアクセスできる
This is a test file.
[toshim@gw2000 ~/work]$cat sltest1 <-- シンボリックリンクはリンク先のファイル名がなくなったのでアクセスできない
cat: sltest1: No such file or directory
|
|