Linux sort directories by size

How to Sort Directories by Actual Size in Linux

Linux file browsers behave much like File Explorer under Windows or the Finder under OS X in that sorting directories by size doesn’t work quite the way that many users would expect it to. You can sort directories by the number of subdirectories that they contain or the number of files inside of them. Nevertheless, actual file size doesn’t seem to work in a majority of cases and you’ll need an extra tool.

Fortunately, there are a few tricks that you can do in order to find out the actual size of directories by the amount of computer storage space they take up. In terms of file systems, there’s very little difference between folders and directories. What your file browser calls a folder really is the same thing, so these tricks will work regardless of what verbiage you prefer. The term directory is used for the sake of consistency.

Method 1: Sorting Directories with the Disk Usage Analyzer

Ubuntu, Debian and Linux Mint users who prefer graphical disk analysis tools may wish to try using sudo apt-get install baobab from the prompt. Fedora and Red Hat users can generally use sudo yum install baobab from the command line, but keep in mind that being a GTK+ application you might end up needing to fill some dependencies if you’re using a QT-based desktop environment like KDE or LXQT.

Once you have everything satisfied, then you could start the application from the command line by typing baobab or you could search for it from the Dash in Ubuntu’s Unity desktop. You could hold down the Super or Windows key and push R then type baobab if you’d rather use the Application Finder or you could start it by clicking on the Applications menu and finding GNOME Disk Usage Analyzer in the System Tools category. It all depends on which desktop environment you’re using.

As soon as it starts, baobab will prompt you to select a file system. Select whichever device contains the directory you’re looking for and give it a few moments to enumerate the directory structure on it. Once it does, the program will present you with a top-level sort of all the directories on your device.

You can click on the Size button to sort the directories from highest-to-lowest in terms of actual size, but the opposite is the default. Click on the arrows next to a directory’s name to expand it and thus sort the subdirectories that live beneath it.

Clicking on the Contents subheading will actually sort the directories in the same manner that a file manager usually does, so this can be useful to compare the actual size versus the sheer number of items living within the subdirectories of each top-level directory.

Читайте также:  Do all steam games run on linux

Method 2: Using the Classic du Tool

You can use the Unix command line disk usage (du) tool from nearly any Linux prompt if you don’t mind working on the command line. This program will summarize the disk usage of any set of files. Should you run it without any arguments, then it will proceed to look recursively through each directory and summarize the size of each until it reaches the end of a tree.

Assuming you’d rather sort each directory from a certain section by their size, you could use the following command:

du –si –max-depth=1 nameOfDirectory | sort -h

You’ll need to replace nameOfDirectory with the directory that you’d prefer to start in. Say for instance you wanted to sort all of the directories found in directly underneath /lib by size. You could run the command as:

du –si –max-depth=1 /lib | sort -h

You may wish to change the number after –max-depth= since this value defines how far down in the directory structure the du command should search. However, since the object here was to avoid searching through an entire tree we elected to leave it at 1 and look underneath a single directory.

The –si argument indicates that the du command should print sizes using the International System of Units, which defines one kilobyte as equal to 1,000 bytes. While this is preferred by those who migrated to Linux from OS X or are used to calculating directory sizes with hardware sizes, many users are most used to binary sizes where 1,024 bytes equal 1 megabyte. Replace –si with -h as follows:

du -h –max-depth=1 /lib | sort -h

This will render the output as expected if you prefer binary sizes. If you’re used to measuring things in so-called kibibytes, then you’ll want to use this command as well. You may also wish to include the | less or | more command to the end of this command line if you’re finding so many subdirectories in a top-level directory that the output surges right off the page. Remember that you should be able to use your scrollbar, trackpad or touchscreen to scroll through the results in any modern X terminal emulator.

Should you find yourself using this solution often and wish instead that you had a version built on new curses, then you can use sudo apt-get install ncdu on Debian, Ubuntu, the various Ubuntu spins, Bodhi and Linux Mint to install a ncurses-based du viewer. Fedora and Red Hat users should be able to use sudo yum install ncdu if they’ve set up a sudoers file, or su – followed by the administration password followed by yum install ncdu if they haven’t yet.

Читайте также:  Generate ssh key linux putty

More than likely you shouldn’t face any dependencies because the program is based on ncurses and little else. You can either run it from the current directory by typing ncdu or look inside of a different part of the tree by typing ncdu /lib or whichever directory you’re interested in browsing.

You’ll be told that the software is calculating the number of items that it found within the requested directory. Once it’s finished you can browse the directories in the order of their true size by using the arrow keys. You can push the S key to sort the directories back and forth in their size order.

Источник

List all directories sorted by size in descending order

I have a requirement to sort all directories of current directory in descended order by size. I tried following du -sh * | sort -rg It is listing all the folders by size but it’s just listing by size of folder by values. However it’s not sorting correcting. 100 MB Dir should be listed before 200KB. Any help will be appreciable.

It didnt work for me in MAC. Getting following issue. du -sh * | sort -rh sort: invalid option — h Try `sort —help’ for more information.

3 Answers 3

-g is for floats. For human-readable output use human-readable sort:

If you have numfmt utility from coreutils, you can use numeric sort with numfmt formatting:

du -B 1 -s * | sort -rn | numfmt --to=iec -d$'\t' --field=1 

It didnt work for me in MAC. Getting following issue. du -sh * | sort -rh sort: invalid option — h Try `sort —help’ for more information.

Yes it works partially. But it’s not listing as MB and KB. It’s not displaying the human readable character for size. It’s displaying in plain total Byte

I prefer to just go straight to comparing bytes.

sort -n sorts numerically. Obviously, -r reverses.

104857600 wbxtra_RESIDENT_07202018_075931.wbt 815372 wbxtra_RESIDENT_07192018_075744.wbt 215310 Slack Crashes 148028 wbxtra_RESIDENT_07182018_162525.wbt 144496 wbxtra_RESIDENT_07182018_163507.wbt 141688 wbxtra_RESIDENT_07182018_161957.wbt 56617 Notification Cache 20480 ~DFFA6E4895E749B423.TMP 16384 ~DF543949D7B4DF074A.TMP 13254 AdobeARM.log 3614 PhishMeOutlookReporterLoader.log 3448 msohtmlclip1/01 3448 msohtmlclip1 512 ~DF92FFF2C02995D884.TMP 28 ExchangePerflog_8484fa311d504d0fdcd6c672.dat 0 WPDNSE 0 VPMECTMP 0 VBE 

Источник

Sort all directories based on their size

I’d like to sort all the directories/files in a specific directory based on their size (using du -sh «name»). I need to apply this command to all directories in my location, then sort them based on this result. How can I do that ?

6 Answers 6

With GNU sort and GNU du (which it appears you have, since you state you are using du ‘s -h option):

du -sh -- * | sort -rh # Files and directories, or du -sh -- */ | sort -rh # Directories only 

The output looks something like this:

22G foo/ 21G bar/ 5.4G baz/ 2.1G qux/ 1021M wibble/ 4.0K wobble/ 

Note that if a file is hard linked in two or more directories, then the sizes of each directory reported by du depends on the order of the directories on the command line. Also, -h is available on other du implementations than GNU.

Читайте также:  Reading gpio in linux

Sort by sizes (as unformatted numbers of kibibytes) and then turn those into human readable:

du -sk -- * | sort -nr | cut -f2 | xargs du -sh 

This is an improved version based on jabalv’s answer. It works with a GNU as well as a BSD userland.

IFS='\n' du -sk -- * | sort -n | cut -f2 | while read line ; do xargs du -sh "$line" done 
4.0K games 2.7M local 6.7M lib32 19M sbin 152M src 177M include 321M bin 2.2G share 2.9G lib 

To reverse the sort order and list the largest files and directories first, use sort -nr .

(1) How is this “an improved version”? It still fails if the directory names contain whitespace. (2) Does IFS=’\n’ do anything useful? (3) Does setting IFS at the point in your answer where you have set it have any effect on the execution?

This improves on jabalv’s answer, fixing the space in names issue:

du -sk -- * | sort -nr | awk -F '\t' '' | xargs du -sh 

OK, this fixes the problem for spaces in directory names. It still fails for other whitespace. I’ll admit that it is very hard to write complex commands that handle filenames that contain newlines correctly, but this also fails for names that contain tabs. … or quote characters ( » ). Also, before I fixed it, it could have failed for filenames beginning with (hyphen/dash). … … … … … … … … … … … … … P.S. Please don’t refer to “the above answer”, as different people see the answers in different orders.

As inspired by jabalv’s answer, sort by sizes (as unformatted numbers of kibibytes) and then turn those into human readable numbers — without running du a second time:

du -s -- * | sort -n | numfmt --from-unit=1024 --to=iec 
du -s --block-size=1 -- * | sort -n | numfmt --to=iec 
  • You can add the -k option to the first command line (i.e., du -sk ) to specify that output should be in kibibytes, but this seems to be the default.
  • Use */ instead of * to list directories only, as in Chris Down’s answer.
  • Add the -r option to the sort command (i.e., sort -nr ) if you want to sort from high to low.
  • Use —from-unit=1024 because du uses binary prefix notation (i.e., K=1024, M=2 20 , etc.) by default.
  • Likewise, use —to=iec to output binary-prefix numbers.
  • This replaces the tabs in du ’s output with spaces. Add —padding=-7 to the numfmt command to get the output to look like it has tabs. (I guess it is counting zero-based.)
    ( —padding=7 (without the ) would right-justify the numbers.)

Источник

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