Last Update 2014/03/11
ファイルの検索を行います。
find オプション1 パス1 式1
オプション1(任意)
シンボリックリンクに関する処理方法の指定などのオプション
詳細はマニュアル等参照
パス1
検索の対象となるディレクトリへのパス
式1
検索の条件式
※値1は+を付けた際は値より大きい場合、-であれば値未満の場合、符号無しは一致の場合
-amin 値1
最終アクセス日時が(値1)分前であれば真
-anewer ファイルパス1
最終アクセス日時が(ファイルパス1)のファイルより新しければ真
-atime 値1
最終アクセス日時が(値1)日前であれば真
-delete
検索結果のファイルを消去
-empty
空ファイルの場合に真
-exec コマンド1 ;
(コマンド1)のコマンドを実行
戻り値が0であれば真
コマンドは検索結果のファイルを順次実行
コマンド実行時のディレクトリはfindを実行したディレクトリ
-exec コマンド1 {} +
(コマンド1)のコマンドを実行
戻り値が0であれば真
検索結果のファイルをコマンドラインに追加して一括で実行(長さにより分割)
コマンド実行時のディレクトリはfindを実行したディレクトリ
-execdir コマンド1 ;
-execdir コマンド1 {} +
(コマンド1)のコマンドを実行
戻り値が0であれば真
コマンドは検索結果のファイルを順次実行
※「+」が付く場合も実行時の結果が同じくなる現象がみられる(2014/01/26現在)
コマンド実行時のディレクトリは検索結果のファイルの存在するディレクトリ
-gid 値1
ファイルのグループIDが(値1)であれば真
-group 名前1
ファイルのグループ名が(名前1)であれば真
-ilname パターン1
ファイルがシンボリックリンクでリンク先のパスが(パターン1)とマッチする場合に真
大文字小文字を区別しない
-iname パターン1
ファイル名が(パターン1)とマッチする場合に真
大文字小文字を区別しない
-inum 値1
ファイルのinode番号が(値1)であれば真
-iregex パターン1
ファイルパスが正規表現(パターン1)とマッチする場合に真
大文字小文字を区別しない
-links 値1
ファイルのリンク数が(値1)であれば真
-lname パターン1
ファイルがシンボリックリンクでリンク先のパスが(パターン1)とマッチする場合に真
-ls
常に真
ファイルの情報を標準出力に出力
-name パターン1
ファイル名が(パターン1)とマッチする場合に真
-newer ファイルパス1
更新日時が(ファイルパス1)のファイルより新しければ真
-ok コマンド1 ;
(コマンド1)のコマンドを実行
実行前に確認メッセージが出力される。「y」を入力すると指定コマンドを実行
確認メッセージで同意しない場合は偽を返す
コマンドは検索結果のファイルを順次実行
コマンド実行時のディレクトリはfindを実行したディレクトリ
※検索結果のファイルの存在するディレクトリでの実行は、okdirを使用
-path パターン1
ファイルパスが(パターン1)とマッチする場合に真
-perm 値1
(値1)で指定したモードビットとファイルのパーミッションが一致している場合に真
-perm - 値1
(値1)で指定したモードのセットされている全てのビットがファイルでもセット
されている場合に真
-print
常に真
検索結果のファイルパスを標準出力に出力し、末尾に改行文字を付ける
-print0
常に真
検索結果のファイルパスを標準出力に出力し、末尾にヌル文字を付ける
-prune
常に真
指定したファイルがディレクトリの場合、そのディレクトリ以下には下りていかない
-regex パターン1
ファイルパスが正規表現(パターン1)とマッチする場合に真
-samefile ファイルパス1
ファイルが(値1)のファイルと同じinodeを参照している場合に真
-size 値1 文字1
ファイルサイズが、(値1)ブロックであれば真
ブロックサイズはデフォルトで512バイト
(文字1)が指定されている場合のブロックサイズは以下の通り
c バイト
b 512バイト(デフォルト)
k キロバイト
M メガバイト
G ギガバイト
-type 文字1
ファイルが(文字1)で指定したファイルタイプと同じ場合に真
ファイルタイプ(主なもの)
d ディレクトリ
f 通常ファイル
l シンボリックリンク
-user 名前1
ファイルのユーザー名が(名前1)であれば真
※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はFreeBSD 9.1です。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はFreeBSD 9.1です。
例で使用するファイル情報
ファイルリスト
$ ls -l
total 16
-rwxr-xr-x 1 testuser2 testgroup2 6 Jan 29 22:01 test.sh
-rw-r--r-- 1 testuser2 testgroup1 15 Jan 29 22:02 test.txt
-rw-r--r-- 1 testuser1 testgroup1 21 Dec 27 16:25 test2.txt
-rw-r--r-- 1 testuser1 testgroup2 0 Jan 29 22:03 test3.txt
lrwxr-xr-x 1 testuser1 testuser1 9 Jan 30 19:20 test4.txt -> test3.txt
drwxr-xr-x 2 testuser2 testgroup2 512 Jan 29 22:08 testdir
$ ls -l testdir
total 4
-rw-r--r-- 1 testuser2 testgroup2 20 Jan 29 22:08 test.txt
詳細情報
フォーマット : %d %i %Sp %l %Su %Sg %r %z "%Sa" "%Sm" "%Sc" "%SB" %k %b %#Xf %N
$ stat test.sh
75 189058 -rwxr-xr-x 1 testuser2 testgroup2 383912 6
"Jan 29 22:01:23 2014" "Jan 29 22:01:23 2014" "Jan 30 19:14:37 2014" "Jan 29 22:01:23 2014"
32768 8 0 test.sh
$ stat test.txt
75 188941 -rw-r--r-- 1 testuser2 testgroup1 385088 15
"Jan 29 22:02:29 2014" "Jan 29 22:02:29 2014" "Jan 30 19:15:26 2014" "Jan 29 22:02:29 2014"
32768 8 0 test.txt
$ stat test2.txt
75 189022 -rw-r--r-- 1 testuser1 testgroup1 383589 21
"Dec 27 16:53:27 2013" "Dec 27 16:25:47 2013" "Jan 30 19:15:48 2014" "Dec 27 16:25:47 2013"
32768 8 0 test2.txt
$ stat test3.txt
75 188993 -rw-r--r-- 1 testuser1 testgroup2 0 0
"Jan 29 22:03:35 2014" "Jan 29 22:03:43 2014" "Jan 30 19:25:38 2014" "Jan 29 22:03:19 2014"
32768 0 0 test3.txt
$ stat test4.txt
75 188930 lrwxr-xr-x 1 testuser1 testuser1 1953719668 9
"Jan 30 19:20:18 2014" "Jan 30 19:20:18 2014" "Jan 30 19:24:08 2014" "Jan 30 19:20:18 2014"
32768 0 0 test4.txt
$ stat testdir
75 188904 drwxr-xr-x 2 testuser2 testgroup2 383917 512
"Jan 30 19:32:06 2014" "Jan 29 22:08:00 2014" "Jan 30 19:25:27 2014" "Jan 29 22:07:30 2014"
32768 8 0 testdir
$ stat testdir/test.txt
75 189055 -rw-r--r-- 1 testuser2 testgroup2 385334 20
"Jan 29 22:08:00 2014" "Jan 29 22:08:00 2014" "Jan 30 19:26:40 2014" "Jan 29 22:08:00 2014"
32768 8 0 testdir/test.txt
実行結果
$ find . -amin -60 ← 最終アクセス日時が現在時刻より60分未満のファイルを検索
.
./test4.txt
./testdir
$ find . -amin +60 ← 最終アクセス日時が現在時刻から60分を超えるファイルを検索
./test.txt
./test2.txt
./test.sh
./test3.txt
./testdir/test.txt
$ date
Thu Jan 30 19:53:36 JST 2014 ← 現在時刻
実行結果
$ find . -anewer test3.txt
.
./test4.txt
./testdir
./testdir/test.txt
実行結果
$ find . -atime -1 ← 最終アクセス日時が現在時刻より1日未満のファイルを検索
.
./test.txt
./test.sh
./test3.txt
./test4.txt
./testdir
./testdir/test.txt
$ find . -atime +1 ← 最終アクセス日時が現在時刻より1日を超えるファイルを検索
./test2.txt
$ date
Thu Jan 30 20:11:15 JST 2014 ← 現在時刻
実行結果
$ ls -l
total 0
-rw-r--r-- 1 devusr devusr 0 Jan 30 20:13 test1.txt
-rw-r--r-- 1 devusr devusr 0 Jan 30 20:14 test2.txt
-rw-r--r-- 1 devusr devusr 0 Jan 30 20:14 test3.txt
$ find . -name "test1*" -delete ← ※位置に注意。検索結果の後に設置
$ ls -l
total 0
-rw-r--r-- 1 devusr devusr 0 Jan 30 20:14 test2.txt
-rw-r--r-- 1 devusr devusr 0 Jan 30 20:14 test3.txt
実行結果
$ find . -empty
./test3.txt
実行結果
$ find . -name "test*" -exec echo '{}' \; ← {}はfindによって処理中のファイル名
./test.txt
./test2.txt
./test.sh
./test3.txt
./test4.txt
./testdir
./testdir/test.txt
実行結果
$ find . -name "test*" -exec echo '{}' \+ ← {}はfindによって処理中のファイル名
./test.txt ./test2.txt ./test.sh ./test3.txt ./test4.txt ./testdir ./testdir/test.txt
実行結果
$ find . -name "test*" -execdir echo '{}' \;
test.txt
test2.txt
test.sh
test3.txt
test4.txt
testdir
test.txt
$ find . -name "test*" -execdir echo '{}' \+
test.txt test2.txt test.sh test3.txt test4.txt testdir test.txt
実行結果
$ find . -gid 1004
./test.txt
./test2.txt
実行結果
$ find . -group testgroup1
./test.txt
./test2.txt
実行結果
$ find . -ilname "Test*"
./test4.txt
実行結果
$ find . -iname "Test*"
./test.txt
./test2.txt
./test.sh
./test3.txt
./test4.txt
./testdir
./testdir/test.txt
実行結果
$ find . -inum 189022
./test2.txt
実行結果
$ find . -iregex ".*Test.*txt"
./test.txt
./test2.txt
./test3.txt
./test4.txt
./testdir/test.txt
実行結果
$ find . -links 2
./testdir
実行結果
$ find . -lname "test*"
./test4.txt
実行結果
$ find . -name "test.txt" -ls
188941 8 -rw-r--r-- 1 testuser2 testgroup1 15 Jan 29 22:02 ./test.txt
189055 8 -rw-r--r-- 1 testuser2 testgroup2 20 Jan 29 22:08 ./testdir/test.txt
実行結果
$ find . -name "test*"
./test.txt
./test2.txt
./test.sh
./test3.txt
./test4.txt
./testdir
./testdir/test.txt
実行結果
$ find . -newer test3.txt
.
./test4.txt
./testdir
./testdir/test.txt
実行結果
$ find . -name "test.txt" -ok echo '{}' \;
"echo ./test.txt"? y ← 確認メッセージde「y」を入力
./test.txt
"echo ./testdir/test.txt"? y
./testdir/test.txt
実行結果
$ find . -path "*test*.txt"
./test.txt
./test2.txt
./test3.txt
./test4.txt
./testdir/test.txt
実行結果
$ find . -perm 644
./test.txt
./test2.txt
./test3.txt
./testdir/test.txt
実行結果
$ find . -perm -755
.
./test.sh
./test4.txt
./testdir
実行結果
$ find . -name "test*.txt" -print
./test.txt
./test2.txt
./test3.txt
./test4.txt
./testdir/test.txt
実行結果
$ find . -name "test*.txt" -print0
./test.txt./test2.txt./test3.txt./test4.txt./testdir/test.txt
実行結果
$ find . -name "testdir" -prune -o -print ← testdirという名前のファイルやディレクトリを除外
.
./test.txt
./test2.txt
./test.sh
./test3.txt
./test4.txt
実行結果
$ ln test2.txt test5.txt
$ ls -il
total 20
189058 -rwxr-xr-x 1 testuser2 testgroup2 6 Jan 29 22:01 test.sh
188941 -rw-r--r-- 1 testuser2 testgroup1 15 Jan 29 22:02 test.txt
189022 -rw-r--r-- 2 testuser1 testgroup1 21 Dec 27 16:25 test2.txt
188993 -rw-r--r-- 1 testuser1 testgroup2 0 Jan 29 22:03 test3.txt
188930 lrwxr-xr-x 1 testuser1 testuser1 9 Jan 30 19:20 test4.txt -> test3.txt
189022 -rw-r--r-- 2 testuser1 testgroup1 21 Dec 27 16:25 test5.txt
188904 drwxr-xr-x 2 testuser2 testgroup2 512 Jan 29 22:08 testdir
$ find . -samefile test2.txt
./test2.txt
./test5.txt
実行結果
$ find . -size 1 ← 1ブロック(512バイト)
.
./test.txt
./test2.txt
./test.sh
./test4.txt
./testdir
./testdir/test.txt
$ find . -size 6c ← 1バイト
./test.sh
実行結果
$ find . -type f ← 通常ファイル
./test.txt
./test2.txt
./test.sh
./test3.txt
./testdir/test.txt
$ find . -type l ← シンボリックリンク
./test4.txt
実行結果
$ find . -user testuser1
./test2.txt
./test3.txt
./test4.txt