You can use these commands on Linux remote machines, servers, and/or Linux machines with or without GUI.įor most of the cases du command would be sufficient. In this article, we learned how to get directory sizes in Linux. For Debian/Ubuntu, we can install the tree by running sudo apt install tree.Īfter 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 Note that tree is not installed by default. Tree is a recursive directory listing program that will list directories and files in a tree-like format. Get Size of Directory in Linux with tree -du -h The second option is more of a workaround rather than the most efficient way. 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. To recursively get all subdirectories of /var/, you can use sudo du /var/ -h. Setting -max-depth to 1 returns the first-level, 2 for the second, and so on. max-depth=N will return all subdirectory levels that are equal or less than the number N. This is also equivalent of sudo du /var/ -h -exclude=lib -max-depth=1 $ sudo du /var/ -h -exclude=lib -max-depth=1 Note that excluding lib also affects the total size ( 3,2G total). We can also add -exclude to exclude any directory: $ sudo du /var/* -shc -exclude=lib * lists all the first-level subdirectories in the /var/ directory. c or -total returns the total size of the path ( 11G total). To get size of first-level subdirectories as well as the total size of the path directory: $ sudo du /var/* -shc Note that you would need to use it with sudo privileges for some directories, otherwise you would get a Permission denied error. 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 We can add the -h parameter to get the size in a more human-readable format: $ du -sh To get the size of the current working directory only, and not the subdirectories, we can use du -s or du -summarize: $ du -s Get Size of the Current Working Directory 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 To see the full description and argument list of du command, refer to the man du. Getting Size of Directory in Linux with du 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. 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. The reason is that ls returns meta-data for the directories, not the actual size. 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. Note that for symlinks, it reports the size of the symlink (not of the file the symlink points to).In Linux, ls -l would list the files and directories in a particular path, with their names, dates, and sizes (disk usage). It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one. ! -type d -printf '%s\n' | paste -sd - | bc ! -type d -print0 | xargs -r0 stat -c %s | paste -sd - | bc To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system): find. If there are hard links in the directory, then du may print smaller value as well because several different files in the directory refer the same data on the media. If the file system is compressible, then du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.
0 Comments
Leave a Reply. |