1

I was trying to find out the total size of all files which are owned by a particular user.

While trying so, i get different sizes when executing different commands.

Which command is correct to find out the total size of all files owned by the particular user?

$ find . -type f -user silviya|ls -lh|head -1 
total 68K
$ find . -type f -user agalya|wc -c
284
$ find . -type f -user agalya|du -sk
120 .

What is the reason for this variation?

5

In:

find . -type f -user silviya|ls -lh|head -1 

you're piping the output of find to ls, but ls doesn't read its input. It takes the list of files to list as arguments. In the absence of arguments like here, it lists the non-hidden files in the current directory. So here, you get the disk usage of all the non-hidden files (of any type) in the current directory (with the size of a given file counted for each of its hard links).

In:

find . -type f -user agalya|wc -c

You're counting the number of bytes in the output of find, so that's the size of the file paths (and newline delimiters), not their disk usage nor file size.

In:

find . -type f -user agalya|du -sk

Like ls, du takes the file list as arguments, not from its input. So here, you get the disk usage of all the files and directories in the current directory (recursively).

To get the disk usage of all regular files owned by agalya, with GNU utilities, you'd do:

find . -type f -user agalya -print0 | du -hc --files0-from=- | tail -n 1

--files0-from tells du (GNU du only) to take the file list from standard input (represented by - here). -c gives the cumulative size (note that hard links of a same file are counted only once).

To get the file apparent size as opposed to disk usage, add the --apparent-size option to du (again, GNU specific). Add the -l option (also GNU-specific) to count hard links several times.

-1

Command #1 gives the total size of the "useful" bytes of the files, of their contents.

Command #3 gives used disk space to hold the files. Disks are divided into blocks (often 4 kB long) that can't be shared by several files. So a file of any size between 1 and 4096 B will used 4 kB of disk space e.g. [This is simplified. On some FS types like ext, very small files can be stored into the file table itself, using no data blocks at all.]

Command #2 just counts the number of characters of the file names. (Size of the output of the find command.)

  • 1
    That's missing the most important point, command 1 and 3 apply to the current directory, not the output of find. Also, the total line in ls -l output gives disk usage like du, not useful bytes. – Stéphane Chazelas Feb 19 '16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.