サイズに応じてファイルを再帰的に並べ替える

サイズに応じてファイルを再帰的に並べ替える

フォルダ内で最大のファイルを見つける必要があります。
フォルダを再帰的にスキャンし、コンテンツをサイズで並べ替える方法は?

を試してみましたが、ls -R -Sここにもディレクトリがリストされています。
を試してみましたfind

ベストアンサー1

を使用してこれを行うこともできますdu。安全のため、次のバージョンを使用してくださいdu

$ du --version
du (GNU coreutils) 8.5

この方法:

$ du -ah <some DIR> | grep -v "/$" | sort -rh

方法の分解

このコマンドは、指定されdu -ah DIRたディレクトリ内のすべてのファイルとディレクトリのリストを生成しますDIR。これにより、-h私が好む人が読めるサイズが作成されます。必要でない場合は、スイッチを取り外してください。私はhead -6出力を制限するために使用しています!

$ du -ah ~/Downloads/ | head -6
4.4M    /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020_WirelessFrames_exUG_GLB_en.pdf
624K    /home/saml/Downloads/kodak_W820_wireless_frame/easyshare_w820.pdf
4.9M    /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020WirelessFrameExUG_GLB_en.pdf
9.8M    /home/saml/Downloads/kodak_W820_wireless_frame
8.0K    /home/saml/Downloads/bugs.xls
604K    /home/saml/Downloads/netgear_gs724t/GS7xxT_HIG_5Jan10.pdf

最も小さいものから最大のものまで整列するのは簡単です。

$ du -ah ~/Downloads/ | sort -h | head -6
0   /home/saml/Downloads/apps_archive/monitoring/nagios/nagios-check_sip-1.3/usr/lib64/nagios/plugins/check_ldaps
0   /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/index/write.lock
0   /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/translog/translog-1365292480753
0   /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/index/write.lock
0   /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/translog/translog-1365292480946
0   /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/2/index/write.lock

最大のものから最小のものの順に裏返します。

$ du -ah ~/Downloads/ | sort -rh | head -6
10G /home/saml/Downloads/
3.8G    /home/saml/Downloads/audible/audio_books
3.8G    /home/saml/Downloads/audible
2.3G    /home/saml/Downloads/apps_archive
1.5G    /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
1.5G    /home/saml/Downloads/digital_blasphemy

ディレクトリを表示せずにファイルのみを表示します。

$ du -ah ~/Downloads/ | grep -v "/$" | sort -rh | head -6 
3.8G    /home/saml/Downloads/audible/audio_books
3.8G    /home/saml/Downloads/audible
2.3G    /home/saml/Downloads/apps_archive
1.5G    /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
1.5G    /home/saml/Downloads/digital_blasphemy
835M    /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run

除外したい場合すべてのディレクトリ出力で既存のドット文字のトリックを使用できます。これは、ディレクトリ名にドットが含まれておらず、探しているファイルにドットが含まれていると仮定します。その後、次を使用してディレクトリをフィルタリングできますgrep -v '\s/[^.]*$'

$ du -ah ~/Downloads/ | grep -v '\s/[^.]*$' | sort -rh | head -2
1.5G    /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
835M    /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run

最も小さいものから最大のものの順にリストが必要ですが、問題の最初の6つのファイルが必要な場合は、ソートスイッチを反転して削除(-r)してからtail -6使用できますhead -6

$ du -ah ~/Downloads/ | grep -v "/$" | sort -h | tail -6
835M    /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run
1.5G    /home/saml/Downloads/digital_blasphemy
1.5G    /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
2.3G    /home/saml/Downloads/apps_archive
3.8G    /home/saml/Downloads/audible
3.8G    /home/saml/Downloads/audible/audio_books

おすすめ記事