ファイルが何も格納されていない使用しないディレクトリを削除してみます。
最初に実験ディレクトリ作ります。適当な英単語からディレクトリ名を決めます。
grep -v \' /usr/share/dict/words | sort -R | head -30 | pr -t3J | sed 's/\t/\//g' | xargs mkdir -p
ディレクトリが作れたかfindで確認します。
takk@deb8:~/a$ find . ./Belushi ./Belushi/re ./Belushi/re/detonate (省略) ./djinn ./djinn/dulcimer ./djinn/dulcimer/Peruvians ./oxymoron ./oxymoron/venerable ./oxymoron/venerable/vagabonds takk@deb8:~/a$
作成したディレクトリのうち、これまた適当なディレクトリにdummy.txtを保存します。
takk@deb8:~/a$ for i in `find | sort -R | head`;do touch $i/dummy.txt;done takk@deb8:~/a$
どこに格納されたか確認します。
takk@deb8:~/a$ find -name dummy.txt ./Belushi/re/dummy.txt ./Belushi/re/detonate/dummy.txt ./violated/credits/dummy.txt ./novellas/millers/dummy.txt ./novellas/millers/unspecified/dummy.txt ./workforce/dummy.txt ./lacing/Lars/dummy.txt ./intestines/dummy.txt ./djinn/dulcimer/Peruvians/dummy.txt ./oxymoron/venerable/vagabonds/dummy.txt takk@deb8:~/a$
不要ディレクトリを消す前に、どこが消されたかわかるようにコピーを作っておきます。
takk@deb8:~/a$ cp -r . ../b
では不要ディレクトリを消します。
find -type d -empty -delete
中身を見てみます。
takk@deb8:~/a$ tree
.
├── Belushi
│ └── re
│ ├── detonate
│ │ └── dummy.txt
│ └── dummy.txt
├── djinn
│ └── dulcimer
│ └── Peruvians
│ └── dummy.txt
├── intestines
│ └── dummy.txt
├── lacing
│ └── Lars
│ └── dummy.txt
├── novellas
│ └── millers
│ ├── dummy.txt
│ └── unspecified
│ └── dummy.txt
├── oxymoron
│ └── venerable
│ └── vagabonds
│ └── dummy.txt
├── violated
│ └── credits
│ └── dummy.txt
└── workforce
└── dummy.txt
18 directories, 10 files
ファイルの存在しないディレクトリは完全に消えてますね。
差分も確認してみましょう。
takk@deb8:~/a$ diff -r . ../b ../b のみに存在: dainty ../b のみに存在: hackle ../b/intestines のみに存在: semimonthlies ../b/lacing/Lars のみに存在: weaklings ../b/violated/credits のみに存在: pelagic ../b/workforce のみに存在: triumphing takk@deb8:~/a$
ちなみにいろいろ出ますがこんな消し方もあります。find使っているので意味はないですが。
takk@deb8:~/a$ find -type d | xargs rmdir -p --ignore-fail-on-non-empty rmdir: `.' を削除できません: 無効な引数です rmdir: ディレクトリ `.' の削除に失敗しました: 無効な引数です rmdir: ディレクトリ `.' の削除に失敗しました: 無効な引数です takk@deb8:~/a$


コメント