Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total size of the contents of all the files in a directory [closed]

Tags:

linux

shell

When I use ls or du, I get the amount of disk space each file is occupying.

I need the sum total of all the data in files and subdirectories I would get if I opened each file and counted the bytes. Bonus points if I can get this without opening each file and counting.

like image 942
Arthur Ulfeldt Avatar asked Aug 06 '09 22:08

Arthur Ulfeldt


People also ask

How do you get the total size of all the files in a directory?

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 .

What are the contents of a directory?

You can get two kinds of information from the directory: bindings and attributes. The directory can be viewed as consisting of name-to-object bindings. That is, each object in the directory has a corresponding name. You can retrieve an object in the directory by looking up its name.


2 Answers

If you want the 'apparent size' (that is the number of bytes in each file), not size taken up by files on the disk, use the -b or --bytes option (if you got a Linux system with GNU coreutils):

% du -sbh <directory> 
like image 156
Arkady Avatar answered Oct 05 '22 23:10

Arkady


Use du -sb:

du -sb DIR 

Optionally, add the h option for more user-friendly output:

du -sbh DIR 
like image 40
rob Avatar answered Oct 05 '22 23:10

rob