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

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

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

を試してみましたが、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

答え2

現在のディレクトリとそのサブディレクトリにあるすべてのファイルを見つけて、サイズに応じて(パスに関係なく)リストし、すべてのファイル名に改行文字が含まれていないと仮定し、GNUを使用すると、find次のことができます。

find . -type f -printf "%s\t%p\n" | sort -n

man findGNUシステムから:

   -printf format
          True; print format  on  the  standard  output,
          interpreting  `\'  escapes and `%' directives.
          Field widths and precisions can  be  specified
          as  with the `printf' C function.  Please note
          that many of the  fields  are  printed  as  %s
          rather  than  %d, and this may mean that flags
          don't work as you  might  expect.   This  also
          means  that  the `-' flag does work (it forces
          fields to be  left-aligned).   Unlike  -print,
          -printf  does  not add a newline at the end of
          the string.  The escapes and directives are:

          %p     File's name.
          %s     File's size in bytes.

からman sort

   -n, --numeric-sort
          compare according to string numerical value

答え3

次のコマンドを試してください。

ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20

現在のディレクトリで最大20個のファイルを繰り返しリストします。

-h注:OSX / BSDではこのオプションはsort使用できないため、sortからインストールしてcoreutils(たとえば経由でbrew)ローカルのbinパスをに適用する必要がありますPATH

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" # Add a "gnubin" for coreutils.

または以下を使用してください。

ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20

最大のディレクトリの場合を使用しますdu。たとえば、次のようになります。

du -ah . | sort -rh | head -20

または:

du -a . | sort -rn | head -20

答え4

Mac / Linux用のディレクトリをスキップするための簡単なソリューション:

find . -type f -exec du -h {} \; | sort -h

関連情報