
CentOS 7にファイル改ざん検知を行う『AIDE』をインストールする
今回は、CentOS 7にファ…
-->
Linuxで、0バイトのファイルや空ディレクトリを一括削除するには、どうすればよいのだろうか。
答えは簡単で、findコマンド一発でできる。
find PATH -empty -delete
[root@BS-PUB-CENT7-01 tmp]# mkdir -p /tmp/empty/{aaa,bbb,ccc,ddd,eee/fff} [root@BS-PUB-CENT7-01 tmp]# touch /tmp/empty/{1,2,3,4,5} [root@BS-PUB-CENT7-01 tmp]# echo ' ' > /tmp/empty/6 [root@BS-PUB-CENT7-01 tmp]# ls -la /tmp/empty 合計 8 drwxr-xr-x. 7 root root 104 6月 25 06:54 . drwxrwxrwt. 6 root root 4096 6月 25 06:53 .. -rw-r--r--. 1 root root 0 6月 25 06:53 1 -rw-r--r--. 1 root root 0 6月 25 06:53 2 -rw-r--r--. 1 root root 0 6月 25 06:53 3 -rw-r--r--. 1 root root 0 6月 25 06:53 4 -rw-r--r--. 1 root root 0 6月 25 06:53 5 -rw-r--r--. 1 root root 2 6月 25 06:54 6 drwxr-xr-x. 2 root root 6 6月 25 06:53 aaa drwxr-xr-x. 2 root root 6 6月 25 06:53 bbb drwxr-xr-x. 2 root root 6 6月 25 06:53 ccc drwxr-xr-x. 2 root root 6 6月 25 06:53 ddd drwxr-xr-x. 3 root root 16 6月 25 06:53 eee [root@BS-PUB-CENT7-01 tmp]# find /tmp/empty -empty -print /tmp/empty/aaa /tmp/empty/bbb /tmp/empty/ccc /tmp/empty/ddd /tmp/empty/eee/fff /tmp/empty/1 /tmp/empty/2 /tmp/empty/3 /tmp/empty/4 /tmp/empty/5 [root@BS-PUB-CENT7-01 tmp]# find /tmp/empty -empty -delete [root@BS-PUB-CENT7-01 tmp]# find /tmp/empty -empty -print [root@BS-PUB-CENT7-01 tmp]# ls -al /tmp/empty/ 合計 8 drwxr-xr-x. 2 root root 14 6月 25 06:54 . drwxrwxrwt. 6 root root 4096 6月 25 06:53 .. -rw-r--r--. 1 root root 2 6月 25 06:54 6
空ディレクトリ、空ファイルをそれぞれ別に削除したい場合は、typeを指定してやるとよいだろう。
find /tmp/empty -type f -empty -delete # 空ファイルのみ find /tmp/empty -type d -empty -delete # 空ディレクトリのみ
Pingback: WindowsのBatch(cmd)/PowerShellで0バイトのファイル(空ファイル)や空のディレクトリを一括削除する | 俺的備忘録 〜なんかいろいろ〜