Linux list folder sizes

How to list recursive file sizes of files and directories in a directory?

How do I list all the files in a directory and their recursive file sizes?

I want to see the sizes ‘rolled up’ to the parent directories in the directory listed. I don’t want to see the child directories or their contents or sizes.

11 Answers 11

It is interactive too so if you want to check on a sub folder just UP, DOWN, and Enter to it.

I guess the easiest way is by typing ls -l , or ls -lh which will provide the file size in human-readable format (KB, MB, etc).

If ‘recursively’ means listing all the subsequent folders, e.g.:

Then you should also add parameter R , like ls -lR or ls -lhR

More information for ls can be found by typing man ls

The following command as Lekensteyn proposed will probably do the job:

-h is for human-readable
—apparent-size is another way to display sizes as already stated
—max-depth is the level of subfolders you want to go down to.

To get the total size of a directory and all children

Also check out tree . It is not installed by default but is the repositories.

More options can be found in the man page.

Since you don’t specifically mention you need a terminal-based solution, I think baobab a.k.a. Disk Usage Analyzer is missing from the list.

It is installed in Ubuntu by default and does exactly what you want in a nice graphical UI with the ability to drill down the directory hierarchy.

Apart from displaying a list of directories with their sizes, it is also showing a rings or treemap chart of filesystem usage, which is extremely useful for visualising the directories which take up the most space.

Источник

How to find the size of a directory in Linux

It is very easy to check the size of directories and files in Linux using the GUI, but it is not always easy to get the size of a directory using the command line.

The ls command can be used to list the contents of a directory, but it does not display the exact directory size and always shows each directory size as 4096 bytes (4 KB), which is the size of space on the disk that is used to store the meta-information for the directory, not what it contains

You can get the actual size of a directory using the du command (Disk Usage), which is widely used by Linux administrators, but you can explore other commands for this purpose. Let’s explore them.

This guide shows you how to find the directory size in Linux using the below three commands:

  • du command
  • ncdu command
  • tree command

Method-1: Get the size of a directory in Linux with du command

The du command refers to disk usage. It is a standard Unix program that is used to estimate disk space usage in the present working directory when no path is specified.

It recursively summarizes the disk usage to obtain a directory and its sub-directory sizes.

Use the below du command format to get the total size of each directory, including sub-directories.

The above command will print the size of each file and the actual size of each directory, including their sub-directory as well as the total size.

Details:

  • du: It’s a command
  • -h: Print sizes in human readable format (e.g., 1K, 234M, 2G)
  • -c: Produce a grand total
  • /home/daygeek/Documents/: The path of directory
  • sort -rh: Sort the results with numerical value
  • head -20: Output the first 20 lines result
Читайте также:  Если копия windows нелицензионная

Use the following du command format to get the total size of a specific directory:

If you want to find out the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the following du command format:

Method-2: Find the size of a directory in Linux with ncdu command

The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’ command, and provides a fast way to see which directories are consuming your disk space.

The ncdu command scans the given directory and displays their files and folder sizes recursively as shown below:

Method-3: Check a directory size in Linux with tree command

The pstree command displays directory contents recursively in a tree-like format, which is very convenient way to display the directory hierarchy and that improves the readability of the output.

As you can see, the tree command output is straight forward compared to the du & ncdu commands.

Conclusion

You have learnt the three different commands to find the size of a directory in Linux.

If you have questions, feel free to leave a comment below, and we will get back to you as soon as we can. Happy learning!

Источник

How to list the size of each file and directory and sort by descending size in Bash?

I found that there is no easy to get way the size of a directory in Bash?

I want that when I type ls — , it can list of all the sum of the file size of directory recursively and files at the same time and sort by size order.

Is that possible?

11 Answers 11

Simply navigate to directory and run following command:

OR add -h for human readable sizes and -r to print bigger directories/files first.

Apparently —max-depth option is not in Mac OS X’s version of the du command. You can use the following instead.

du -h -d 1 | sort -n

(this willnot show hidden (.dotfiles) files)

Use du -sm for Mb units etc. I always use

because the total line ( -c ) will end up at the bottom for obvious reasons 🙂

  • See comments for handling dotfiles
  • I frequently use e.g. ‘du -smc /home// | sort -n |tail’ to get a feel of where exactly the large bits are sitting

Command

Output

Explanation

  • du displays «disk usage»
  • h is for «human readable» (both, in sort and in du)
  • max-depth=0 means du will not show sizes of subfolders (remove that if you want to show all sizes of every file in every sub-, subsub-, . folder)
  • r is for «reverse» (biggest file first)

When I came to this question, I wanted to clean up my file system. The command line tool ncdu is way better suited to this task.

Installation on Ubuntu:

Just type ncdu [path] in the command line. After a few seconds for analyzing the path, you will see something like this:

Delete the currently highlighted element with d , exit with CTRL + c

ls -S sorts by size. Then, to show the size too, ls -lS gives a long ( -l ), sorted by size ( -S ) display. I usually add -h too, to make things easier to read, so, ls -lhS .

Simple and fast:

I think I might have figured out what you want to do. This will give a sorted list of all the files and all the directories, sorted by file size and size of the content in the directories.

[enhanced version]
This is going to be much faster and precise than the initial version below and will output the sum of all the file size of current directory:

the stat -c %s command on a file will return its size in bytes. The tr command here is used to overcome xargs command limitations (apparently piping to xargs is splitting results on more lines, breaking the logic of my command). Hence tr is taking care of replacing line feed with + (plus) sign. sed has the only goal to remove the last + sign from the resulting string to avoid complains from the final bc (basic calculator) command that, as usual, does the math.

Performances: I tested it on several directories and over

Читайте также:  Что делать если windows не находит драйвер для сетевого адаптера

150.000 files top (the current number of files of my fedora 15 box) having what I believe it is an amazing result:

Just in case you want to make a comparison with the du -sb / command, it will output an estimated disk usage in bytes ( -b option)

As I was expecting it is a little larger than my command calculation because the du utility returns allocated space of each file and not the actual consumed space.

[initial version]
You cannot use du command if you need to know the exact sum size of your folder because (as per man page citation) du estimates file space usage. Hence it will lead you to a wrong result, an approximation (maybe close to the sum size but most likely greater than the actual size you are looking for).

I think there might be different ways to answer your question but this is mine:

It finds all files under . directory (change . with whatever directory you like), also hidden files are included and (using xargs ) outputs their names in a single line, then produces a detailed list using ls -l . This (sometimes) huge output is piped towards cut command and only the fifth field ( -f5 ), which is the file size in bytes is taken and again piped against xargs which produces again a single line of sizes separated by blanks. Now take place a sed magic which replaces each blank space with a plus ( + ) sign and finally bc (basic calculator) does the math.

It might need additional tuning and you may have ls command complaining about arguments list too long.

Источник

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

Is there a simple command to display the total aggregate size (disk usage) of all files in a directory (folder)?

I have tried these, and they don’t do what I want:

  • ls -l , which only displays the size of the individual files in a directory, nor
  • df -h , which only displays the free and used space on my disks.

14 Answers 14

The command du «summarizes disk usage of each FILE, recursively for directories,» e.g.,

  • -h is to get the numbers «human readable», e.g. get 140M instead of 143260 (size in KBytes)
  • -s is for summary (otherwise you’ll get not only the size of the folder but also for everything in the folder separately)

As you’re using -h you can sort the human readable values using

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

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

Recently I found a great, ncurses based interactive tool, that quickly gives you an overview about directory sizes. Searched for that kind of tool for years.

  • quickly drilldown through file hierarchy
  • you can delete e.g. huge temporary files from inside the tool
  • extremely fast

Think of it as baobab for the command line:

This finds the size recursively and puts it next to each folder name, along with total size at the bottom, all in the human format

More information on that command here

Below is what I am using to print total, folder, and file size:

Details

Output

tree is another useful command for this job:

Just install it via sudo apt-get install tree and type the following:

The answers have made it obvious that du is the tool to find the total size of a directory. However, there are a couple of factors to consider:

Occasionally, du output can be misleading because it reports the space allocated by the filesystem, which may be different from the sum of the sizes of the individual files. Typically the filesystem will allocate 4096 bytes for a file even if you stored just one character in it!

Output differences due to power of 2 and power of 10 units. The -h switch to du divides the number of bytes by 2^10 (1024), 2^20 (1048576) etc to give a human readable output. Many people might be more habituated to seeing powers of 10 (e.g. 1K = 1000, 1M = 1000000) and be surprised by the result.

Читайте также:  Чем запустить файл md5 windows 10

To find the total sum of sizes of all files in a directory, in bytes, do:

Источник

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?

9 Answers 9

This does what you’re looking for:

What this means:

  • -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:

  • -h ensures that sort interprets the human-readable suffixes correctly.

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

In this case the sizes will be reported in megabytes.

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:

Very similar to the first answer and nearly the same result as 1.), but it took me some time to understand the difference of * to ./* if in a subdirectory:

The following du invocation should work on BSD systems:

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:

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

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!

Источник

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