Get folder size in linux

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.

@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 ).

Читайте также:  Команда выключения сервера linux

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 :

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!

Источник

Check folder size in Bash

I’m trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?

# 10GB SIZE="1074747474" # check the current size CHECK="`du /data/sflow_log/`" if [ "$CHECK" -gt "$SIZE" ]; then echo "DONE" fi 

Since this is a popular question — If any beginner is encountering the answers on this question and wants to learn more about what the heck du is and how everyone knows all these commands: You can type man du in your terminal to lookup the du command in the manual. This will display an output which you can view, and will summarize all the flags like -h, -c, -s, -b, -B, —apparent-size, etc. that answers are you suggesting you use. Then, you can decide for yourself how you best want to use du for your specific use case.

Читайте также:  Компас 3d linux ubuntu

8 Answers 8

which will give you a brief output of the size of your target directory. Using a wildcard like * can select multiple directories.

If you want a full listing of sizes for all files and sub-directories inside your target, you can do:

  • Add the argument -c to see a Total line at the end. Example: du -hcs or du -hc .
  • Remove the argument -h to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example: du -s or du -cs .

With no directory path specified it will default to the current working directory. so du -hs == du -hs . .

if you just want to see the folder size and not the sub-folders, you can use:

You should know that du shows the used disk space; and not the file size.

You can use —apparent-size if u want to see sum of actual file sizes.

--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, and the like 

And of course theres no need for -h (Human readable) option inside a script.

Instead You can use -b for easier comparison inside script.

But You should Note that -b applies —apparent-size by itself. And it might not be what you need.

-b, --bytes equivalent to '--apparent-size --block-size=1' 

so I think, you should use —block-size or -B

#!/bin/bash SIZE=$(du -B 1 /path/to/directory | cut -f 1 -d " ") # 2GB = 2147483648 bytes # 10GB = 10737418240 bytes if [[ $SIZE -gt 2147483648 && $SIZE -lt 10737418240 ]]; then echo 'Condition returned True' fi 

Источник

Get Total Size of a Directory in Linux

In Linux, ls -l would list the files and directories in a particular path, with their names, dates, and sizes (disk usage). The first thing you’ll notice using that command is that the size of directories is always shown as 4096 bytes (or 4,0K if you’re using ls -lh ) even though they contain files that are greater than 4 KB in size. The reason is that ls returns meta-data for the directories, not the actual size.

So what’s the shortest and easiest way to get the size of a directory in Linux, you ask? To get the total size of a directory in Linux, you can use the du (disk-usage) command.

In this article, we’ll take a look at some of the most common usages of the du commands, including but not limited to du -sh , du -ch , and du —max-depth .

Getting Size of Directory in Linux with du

To see the full description and argument list of du command, refer to the man du .

If we type du without any arguments, it will list all the directory names and sizes for the current working directory and all subdirectories recursively:

$ du 2156 ./corpora/state_union 64 ./corpora/names 7624 ./corpora/conll2002 432 ./corpora/toolbox/rotokas ### .  246984 ./corpora 16792 ./tokenizers/punkt/PY3 36028 ./tokenizers/punkt 49420 ./tokenizers 296408 . 

Get Size of the Current Working Directory

To get the size of the current working directory only, and not the subdirectories, we can use du -s or du —summarize :

We can add the -h parameter to get the size in a more human-readable format:

Читайте также:  Установка драйверов через терминал линукс

We can also use du with $PATH parameter to get the size of a directory that is located somewhere other than the current working directory:

$ sudo du /var -sh # or "du -sh /var" if you prefer 11G /var 

Note that you would need to use it with sudo privileges for some directories, otherwise you would get a Permission denied error.

Get Size of First-Level Subdirectories

To get size of first-level subdirectories as well as the total size of the path directory:

$ sudo du /var/* -shc 6,1M /var/backups 144M /var/cache 4,0K /var/crash 7,2G /var/lib 4,0K /var/local 0 /var/lock 3,0G /var/log 4,0K /var/mail 4,0K /var/metrics 4,0K /var/opt 0 /var/run 3,8M /var/snap 52K /var/spool 72K /var/tmp 28K /var/www 11G total 

-c or —total returns the total size of the path ( 11G total ). * lists all the first-level subdirectories in the /var/ directory. We can also add —exclude to exclude any directory:

$ sudo du /var/* -shc --exclude=lib 6,1M /var/backups 144M /var/cache 4,0K /var/crash 4,0K /var/local 0 /var/lock 3,0G /var/log 4,0K /var/mail 4,0K /var/metrics 4,0K /var/opt 0 /var/run 3,8M /var/snap 52K /var/spool 72K /var/tmp 28K /var/www 3,2G total 

Note that excluding lib also affects the total size ( 3,2G total ). This is also equivalent of sudo du /var/ -h —exclude=lib —max-depth=1

$ sudo du /var/ -h --exclude=lib --max-depth=1 4,0K /var/mail 52K /var/spool 3,8M /var/snap 4,0K /var/metrics 144M /var/cache 6,1M /var/backups 72K /var/tmp 4,0K /var/crash 3,0G /var/log 4,0K /var/opt 28K /var/www 4,0K /var/local 3,2G /var/ 

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

—max-depth=N will return all subdirectory levels that are equal or less than the number N . Setting —max-depth to 1 returns the first-level, 2 for the second, and so on.

Get Size of All Subdirectories

To recursively get all subdirectories of /var/ , you can use sudo du /var/ -h . Or you can pass a number to the —max-depth that you’re sure is greater than or equal to the max level of subdirectory depth: sudo du /var/ -h —max-depth=999 .

The second option is more of a workaround rather than the most efficient way.

Get Size of Directory in Linux with tree —du -h

tree is a recursive directory listing program that will list directories and files in a tree-like format. Note that tree is not installed by default. For Debian/Ubuntu, we can install the tree by running sudo apt install tree .

After the installation complete, we use the tree command to list names and sizes of all directories and files in a particular path, in a tree-like format:

$ tree /var/www/ --du -h /var/www/ ├── [4.2K] demosite │ └── [ 189] index.html └── [ 15K] html └── [ 11K] index.html 23K used in 2 directories, 2 files 

Conclusion

In this article, we learned how to get directory sizes in Linux. You can use these commands on Linux remote machines, servers, and/or Linux machines with or without GUI.

For most of the cases du command would be sufficient. It has also the advantage of being installed by default. On the other hand, the tree command would provide a more visual and detailed user interface to display almost the same results, making it a powerful alternative for du .

Источник

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