なんでもfindお役立ち実例集

Nid: 10
  • Posted on: 31 May 2014
  • By: nayeli

実行可能なPHPファイルを検索

$ find . -perm /u+x -type f -name "*.php"

 

カレント直下のサブディレクトリ内で同一コマンドを実行

$ find "$PWD" -mindepth 1 -maxdepth 1 -type d | while read -r line; do cd "$line" && pwd; done;

 

*.mp3ファイルを999個探して、mpg123でランダム再生

$ find /home/username/music/ -name "*.[mM][pP]3" | xargs -d '\n' -n999 mpg123 -vCZ
$ find ~/music/ -type f | egrep '/.*\.([mM][pP]3)$' |xargs -d '\n' -n999 mpg123 -vCZ

 


日付範囲指定でファイル検索

$ find . -type f -newermt "1999-09-09" ! -newermt "2099-01-01"

 


48時間(2x24)以内に変更されたCSVファイルを削除

$ find . -type f -name '*.csv' -mtime -2 -exec rm {} \;

30分以内に変更されたjsonファイルを指定場所にコピー

$ find /tmp -type f -name '*.json' -mmin -30 -exec cp {} ~/work \;

 


ファイルサイズが5M以上のファイル

$ find . -size +5000k -print

特定ファイルを検索してサイズを合計

$ { find . -name "*.jpg" -size +300k -printf "%s+"; echo 0; } | bc

 


不信なSUIDビットの立ったファイル

$ find / -perm -u+s -exec ls -l {} \;

 


mime-typeでファイル検索

$ find . -exec file -i {} \; | grep image/png

 



ファイルを種類毎にカウント

$ find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr

 



テキストファイルを検索して文字コード変換(UTF-8へ)

$ find /tmp -type f | egrep '/.*\.(txt|text)$' | xargs -d '\n' nkf -w --overwrite

 



ファイルをUNIX形式に変換

$ for f in `find *.txt -type f`; do echo "dos2unix $f"; done | sh

 



ファイル名が入力できない場合に、inodeでファイルを削除

$ ls -il
$ find . -inum 1187215 -exec rm -i {} \;

 



ファイル拡張子を一括変換(.php3 to .php)

$ find -name "*.php3" | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh



ファイルだけ、またはディレクトリだけの権限を変更

$ find . -type f -print0 | xargs -0 chmod 604
$ find . -name '*.txt' -exec chmod 604 {} \;
$ find . -type d -print0 xargs -0 chmod 755

 



特定所有者のファイルを探して、所有者変更

$ find . -user root | xargs sudo chown www-data.www-data
$ sudo find . -user root -exec chown www-data.www-data {} \;

 


特定権限のファイルを探して、権限変更

$ find . -perm 666 -ls -exec chmod 640 {} \;

 



ディレクトリサイズ一覧

$ find . -type d | du

 



特定ファイルだけをtarでまとめて圧縮

$ find ~/ -name '*.pl' | tar -c --files-from=- | bzip2 > myperl.tar.bz2

 


キーワード検索したファイルの中身「before」を「after」に置換して、ファイル名.bakのバックアップも作成

$ find . -name *whatyouwant* -exec perl -pi.bak -e 's/before/after/g' {} \;

 


検索したファイルに指定文字列を追加

$ find . -name "*.template" | while read TEMPLATE ; do echo -e "\nText to Append=" >> $TEMPLATE ; done