Sorting files by size linux

Sorting files according to size recursively

I need to find the largest files in a folder.
How do I scan a folder recursively and sort the contents by size? I have tried using ls -R -S , but this lists the directories as well.
I also tried using find .

Do you want to list the files in each subdirectory separately or do you want to find all files in all subdirs and list them by size irrespective of which subdir they are in? Also, what do you mean by «directory» and «folder»? You seem to be using them to describe different things.

Are you saying that you just want to list the files in a given directory as well as the files in its sub-directories without showing just the sub-directories? Please try and clean up you question, it’s not very clear.

11 Answers 11

You can also do this with just du . Just to be on the safe side I’m using this version of du :

$ du --version du (GNU coreutils) 8.5 

Breakdown of approach

The command du -ah DIR will produce a list of all the files and directories in a given directory DIR . The -h will produce human readable sizes which I prefer. If you don’t want them then drop that switch. I’m using the head -6 just to limit the amount of output!

$ 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 

Easy enough to sort it smallest to biggest:

$ 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 

Reverse it, biggest to smallest:

$ 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 

Don’t show me the directory, just the files:

$ 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 

If you want to exclude all directories from the output, you can use a trick with the presence of a dot character. This assumes that your directory names do not contain dots, and that the files you are looking for do. Then you can filter out the directories with 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 

If you just want the list of smallest to biggest, but the top 6 offending files you can reverse the sort switch, drop ( -r ), and use tail -6 instead of the 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 

Источник

Читайте также:  Shared memory linux usage

How to List All Files Ordered by Size in Linux

In one of our several articles about listing files using the popular ls command, we covered how to list and sort files by last modification time (date and time) in Linux. In this short handy article, we will present a number of useful ls command options to list all of the files in a certain directory and sort them by file size in Linux.

To list all files in a directory, open a terminal window and run the following command. Note that when ls invoked without any arguments, it will list the files in the current working directory.

In the following command the -l flag means long listing and -a tells ls to list all files including (.) or hidden files. To avoid showing the . and .. files, use the -A option instead of -a .

$ ls -la OR $ ls -la /var/www/html/admin_portal/

List All Files in Linux

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size).

$ ls -laS /var/www/html/admin_portal/

List All Files Sort By Sizes

You can output the file sizes in human-readable format by adding the -h option as shown.

$ ls -laSh /var/www/html/admin_portal/

List Files Sort By Sizes in Linux

And to sort in reverse order, add the -r flag as follows.

$ ls -laShr /var/www/html/admin_portal/

List All Files Sort By Sizes in Reverse Order

Besides, you can list subdirectories recursively using the -R option.

$ ls -laShR /var/www/html/admin_portal/

List Sub-directories Recursively

You will also find the following related articles useful:

If you any other way to list the files ordered by sizes in Linux, do share with us or do you have questions or thoughts to share about this guide? If yes, reach us via the feedback form below.

Читайте также:  Sql server connection linux

Источник

How can I sort all files by size in a directory?

Can you please clarify if you are expecting the subdirectory sizes to appear in the output too, and also if you are looking for the apparent size of the files or the actual size they use on disk ?

7 Answers 7

-S is no longer a valid sort argument at least on ubuntu. The below answer by @alex worked for me. The answer link is superuser.com/a/990437/528836.

-l use a long listing format -h with -l, print sizes in human readable format (e.g., 1K 234M 2G) -S sort by file size 

If you have the appropriate sort version you may simply use:

$ sort --version sort (GNU coreutils) 8.12 

du : estimate file disk usage.

-h : for human -a : all files 

man du; man sort for more. It works for me on ubuntu v15.

ls -S wasn’t an option on the OS for me. The following worked:
ls -l | sort -k 5nr
They «key» was to specify the column to sort (get it, the «key»). Above I’m specifying -k 5nr meaning sort on 5th column which is size (5) evaluated as a number (n) in descending order (n)

Reference sort documentation for more information

I got this to work for me:

Which (I just figured-out) is the same as:

Unlike ls -S , this will properly handle sparse files:

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Читайте также:  Чем почистить linux mint

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector