- Sorting files according to size recursively
- 11 Answers 11
- Breakdown of approach
- How to List All Files Ordered by Size in Linux
- How can I sort all files by size in a directory?
- 7 Answers 7
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- How to List or Sort All Files by Size in Linux
- List or Sort All Files by Size in Linux
- List All Files Ordered by Size
- List or Sort Files by Size According to a Specific File Type
- Exclude Directories When Listing or Sorting Files by Size
- List or Sort All Files Using a Common Unit Size
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
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/
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/
You can output the file sizes in human-readable format by adding the -h option as shown.
$ ls -laSh /var/www/html/admin_portal/
And to sort in reverse order, add the -r flag as follows.
$ ls -laShr /var/www/html/admin_portal/
Besides, you can list subdirectories recursively using the -R option.
$ ls -laShR /var/www/html/admin_portal/
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.
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.
Related
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.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to List or Sort All Files by Size in Linux
The Linux ls command is a handy tool for listing files inside a directory. In the past, we have covered how to list and sort files by last modification time using the ls command.
In this article, we will go a step further and explore ways that you can list all the files in a specific directory and sort them by file size.
List or Sort All Files by Size in Linux
To list all files contained in a directory, launch your terminal and run the following command. When invoked without any arguments, the ls command simply lists the files and folders inside a directory.
In the following command the -l flag means long listing and -a tells ls to list all files including hidden files – which are prefixed by a period (.) . To avoid displaying the . and .. files, use the -A option instead of -a .
List All Files Ordered by Size
To list all the files and sort them by size, use the -S flag. By default, it displays output in descending order (biggest to smallest in size).
You can display the output in a human-readable format by appending the -h option as shown.
In addition, you can sort in reverse order (from the smallest to the largest) by adding the -r option as shown.
List or Sort Files by Size According to a Specific File Type
You can list or sort out files by size by applying file type as a filter. For example, to list or sort zipped files by size, use the wildcard symbol as shown.
Exclude Directories When Listing or Sorting Files by Size
To exclude directories in listing or sorting files by size, use the following syntax:
List or Sort All Files Using a Common Unit Size
In the above examples, the file sizes have been printed in different unit sizes, i.e Kilobytes (k) and Megabytes (M).
To print or display all file sizes in a specific unit, for example in Megabytes, add the —block-size= option specifying Megabytes as M as shown below.
From the output, you can see the file sizes are now in MB only.
NOTE: For files less than the specified size (in this case MB), the size will be rounded off to the nearest MB. This implies files in Kilobytes and bytes will appear as 1MB. The same will hold true for any specified size.
You can also print the size in KB units by replacing M with k:
This time around, the file sizes are accurately displayed in kilobytes since no file is less than a kilobyte, and hence no rounding off to the nearest kilobyte is done.
Conclusion
And that’s it for this lecture. In this article, we have demonstrated how to list or sort files by size in Linux. We hope you found this information insightful.