Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the total space consumption of specific files in unix terminal

I have a folder containing .tcb and .tch files. I need to know what the size of all .tcb files together, respectively of all .tch files is. I did like this:

1) I created a temp folder and then:

mv *tch temp

2) and then:

du -sk temp

I found the command in the Internet and wikipedia says this: "du (abbreviated from disk usage) is a standard Unix program used to estimate the file space usage". I think the reason why it says that it is an estimation is that if there are links then the size of the link will be shown instead of the linked file.

But if I do

ls -l

in the temp folder (which contains the all *.tch) files and then sum up the sizes which are displayed in the terminal, I have another file size. Why is that the case?

Well in sum, what I need is a command which shows me the real file size of *all .tch files in a folder, which can contain also other file types.

I hope anyone can help me with that. Thanks a lot!

like image 935
mkn Avatar asked Aug 17 '10 17:08

mkn


2 Answers

You can use the -L option to du if you want to follow symbolic links (that is, calculate the size of the link target, not of the link itself). You can also use the -c option to display a grand total at the end.

Armed with those options, try du -skLc *.tch.

For more details on du, see this manpage.

like image 151
bta Avatar answered Nov 12 '22 22:11

bta


Look at the specific man page for your version of du as they vary considerably in how they count.

"Approximate" can be because:

  1. Blocks used or Bytes used can be reported with Blocks over-stating file sizes that aren't exact multiples of the block size but more accurately represents "space used that I can't use for other stuffs"
  2. Unix files can have "holes" created by seeking a long way and writing. The OS doesn't actually allocate space for the skipped holes.
  3. Symbolic links may or may not be dereferenced to the real file they point to.

If you just want the bytecount use wc -c *.tcb

like image 43
msw Avatar answered Nov 12 '22 21:11

msw