Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total size of folders with find and du?

Tags:

I'm trying to get the size of the directories named "bak" with find and du.

I do that : find -name bak -type d -exec du -ch '{}' \;

But it returns the size for each folder named "bak" not the total.

Anyway to get them ? Thanks :)

like image 589
Piokaz Avatar asked Mar 20 '12 20:03

Piokaz


People also ask

How can I see the total size of folders?

Go to Windows Explorer and right-click on the file, folder or drive that you're investigating. From the menu that appears, go to Properties. This will show you the total file/drive size.

How do you find the total file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do I find the size of a directory and subfolders in Linux?

To get the total size of a directory in Linux, you can use the du (disk-usage) command.

How do I find the size of a directory in Unix?

You can run "df" UNIX command with the current directory or any specified directory. See below example of df command in UNIX to find out the size of a directory along with space left in file system. $ df -h .


1 Answers

Use xargs(1) instead of -exec:

find . -name bak -type d | xargs du -ch 

-exec executes the command for each file found (check the find(1) documentation). Piping to xargs lets you aggregate those filenames and only run du once. You could also do:

find -name bak -type d -exec du -ch '{}' \; + 

If your version of find supports it.

like image 79
Carl Norum Avatar answered Sep 22 '22 21:09

Carl Norum