Linux list folders with sizes

How do I determine the total size of a directory (folder) from the command line?

The -h flag on sort will consider «Human Readable» size values.

If want to avoid recursively listing all files and directories, you can supply the —max-depth parameter to limit how many items are displayed. Most commonly, —max-depth=1

du -h --max-depth=1 /path/to/directory 

I use du -sh or DOOSH as a way to remember it (NOTE: the command is the same, just the organization of commandline flags for memory purposes)

There is a useful option to du called the —apparent-size. It can be used to find the actual size of a file or directory (as opposed to its footprint on the disk) eg, a text file with just 4 characters will occupy about 6 bytes, but will still show up as taking up ~4K in a regular du -sh output. However, if you pass the —apparent-size option, the output will be 6. man du says: —apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (‘sparse’) files, internal fragmentation, indirect blocks

This works for OS X too! Thanks, I was really looking for a way to clear up files, both on my local machine, and my server, but automated methods seemed not to work. So, I ran du -hs * and went into the largest directory and found out which files were so large. This is such a good method, and the best part is you don’t have to install anything! Definitely deserved my upvote

@BandaMuhammadAlHelal I think there are two reasons: rounding ( du has somewhat peculiar rounding, showing no decimals if the value has more than one digit in the chosen unit), and the classical 1024 vs. 1000 prefix issue. du has an option -B (or —block-size ) to change the units in which it displays values, or you could use -b instead of -h to get the «raw» value in bytes.

Источник

How to list the size of a directoy and subdirectories and files inside it by terminal?

Is there a command to list a directory total size and then each sub-folder and files inside it . I also want to sort them by increasing order so that I can see which folder is greater in size.

3 Answers 3

will get you a human-readable ascending list of the sizes of files and subdirectories in your current directory,

Читайте также:  How to get root access on linux

will summarize the current directory size.

i used du command for that purpose .

du : Summarize disk usage of each FILE, recursively for directories. 

As the picture below i want to see the size of a folder name test and recursively the sub folders and each file in kb :

enter image description here

You can do ls -sh to list the file size of the files and folder in the current directory
ls -shR * will list the size and name of files recursively

You must log in to answer this question.

Linked

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.43530

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

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 get the summarized sizes of directories and their subdirectories?

Let’s say I want to get the size of each directory of a Linux file system. When I use ls -la I don’t really get the summarized size of the folders. If I use df I get the size of each mounted file system but that also doesn’t help me. And with du I get the size of each subdirectory and the summary of the whole file system. But I want to have only the summarized size of each directory within the ROOT folder of the file system. Is there any command to achieve that?

The —total flag was helpful for me. E.g. du -sh —total applications/* . askubuntu.com/a/465436/48214

9 Answers 9

This does what you’re looking for:

  • -s to give only the total for each command line argument.
  • -h for human-readable suffixes like M for megabytes and G for gigabytes (optional).
  • /* simply expands to all directories (and files) in / . Note: dotfiles are not included; run shopt -s dotglob to include those too.

Also useful is sorting by size:

If you have dot-directories in the root directory, you can use shopt -s dotglob to include them in the count.

It’s very usefull, because it’s simple and you can place what path you want instead of /* , e.g. ./ for current directory or ./* for each item in current directory.

@c1phr If your sort doesn’t have -h , you need to leave it off from du as well, otherwise the sorting will mix up kilo/mega/gigabytes. du -s /* | sort -nr .

I often need to find the biggest directories, so to get a sorted list containing the 20 biggest dirs I do this:

du -m /some/path | sort -nr | head -n 20 

In this case the sizes will be reported in megabytes.

Читайте также:  Удаленное управление alt linux

@Xedecima the problem with using h is the sort doesn’t know how to handle different sizes. For example 268K is sorted higher than 255M, and both are sorted higher than 2.7G

The -h (human readable) argument on the ‘sort’ command should properly read these values. Just like du’s -h flag exports them. Depending on what you’re running I’m guessing.

I like to use Ncdu for that, you can use the cursor to navigate and drill down through the directory structure it works really well.

The existing answers are very helpful, maybe some beginner (like me) will find this helpful as well.

    Very basic loop, but for me this was a good start for some other size related operations:

for each in $(ls) ; do du -hs "$each" ; done 

The following du invocation should work on BSD systems:

Right portable option combination on BSD/*NIX is du -sk /* . I hate the -k stuff soooo much. Linux’ -h totally rocks.

This isn’t easy. The du command either shows files and folders (default) or just the sizes of all items which you specify on the command line (option -s ).

To get the largest items (files and folders), sorted, with human readable sizes on Linux:

This will bury you in a ton of small files. You can get rid of them with —threshold (1 MB in my example):

The advantage of this command is that it includes hidden dot folders (folders which start with . ).

If you really just want the folders, you need to use find but this can be very, very slow since du will have to scan many folders several times:

find . -type d -print0 | sort -z | xargs --null -I '<>' du -sh '<>' | sort -h 

@podarok It’s available on OpenSUSE 13.2 Linux. Try to find a more recent version of your distribution or compile a more recent version of the package yourself.

Caching might have been a bad term. I was thinking of something like done in this port superuser.com/a/597173/121352 where we scan the disks contents once into a mapping and then continue using data from that mapping rather than hitting the disk again.

You might also want to check out xdiskusage. Will give you the same information, but shown graphically, plus allows to drill down (very useful). There are other similar utilities for KDE and even Windows.

Be aware, that you can’t compare directories with du on different systems/machines without getting sure, both share the same blocksize of the filesystem. This might count if you rsync some files from a linux machine to a nas and you want to compare the synced directory on your own. You might get different results with du because of different blocksizes.

You could use ls in conjunction with awk :

Читайте также:  Linux mint установить репозиторий

The output of ls is piped to awk . awk starts processing the data. Standard delimiter is space. The sum variable tot is initialised to zero; the following statement is executed for each row/line outputted by ls . It merely increments tot with the size. $5 stands for fifth column (outputted by ls ). At the end we divide by (1024*1024) to sum in megabytes.

If you would convert this into a script or function (.bashrc) you can also use it to get the size of certain subsets of directories, according to filetypes.

If you want system wide information, kdirstat may came in handy!

Источник

Display each sub-directory size in a list format using one line command in Bash?

I want to get a list of the directories and their sizes in a list format like how you get when you do a ls -l . The thing is that is there a one line command that can do this? I see others have long commands just to output this. That’s just too long. What command can do this or combination of commands that can be easily typed? du -h gives it, but it displays all of the sub-folders which is not what I want. just the current directories folders.

4 Answers 4

You probably want to see the directories ordered by size:

856M lib 746M share 612M lib64 312M src 267M java 239M bin 179M sbin 173M local 93M i686-w64-mingw32 72M libexec 26M include 20M puppet 772K X11R6 20K man 4.0K games 4.0K etc 0 tmp 

This does not work if there large number of entries in the directory. bash: /run/current-system/sw/bin/du: Argument list too long

Output

oliver@home:/usr$ sudo du -h --max-depth=1 24M ./include 20M ./sbin 228M ./local 4.0K ./src 520M ./lib 8.0K ./games 1.3G ./share 255M ./bin 2.4G . 

Alternative

If —max-depth=1 is a bit too long for your taste, you can also try using:

This uses -s ( —summarize ) and will only print the size of the folder itself by default. By passing all elements in the current working directory ( * ), it produces similar output as —max-depth=1 would:

Output

oliver@cloud:/usr$ sudo du -h -s * 255M bin 8.0K games 24M include 520M lib 0 lib64 228M local 20M sbin 1.3G share 4.0K src 

The difference is subtle. The former approach will display the total size of the current working directory and the total size of all folders that are contained in it. but only up to a depth of 1.

The latter approach will calculate the total size of all passed items individually. Thus, it includes the symlink lib64 in the output, but excludes the hidden items (whose name start with a dot). It also lacks the total size for the current working directory, as that was not passed as an argument.

Источник

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