[TAG] [2 cents TIP] efficient use of "-exec" in find

Neil Youngman ny at youngman.org.uk
Mon Feb 1 11:59:12 MSK 2010


On Monday 01 February 2010 02:49:23 Ben Okopnik wrote:
> You're right - I missed that. What is the point of using 'du -k' at all,
> if all you're trying to do is get the size of the file? Seems to me
> that 'find ~ -type f -ls' or maybe 'find ~ -type f -printf "%k\t%f\n"'
> would do everything you need.

Well du -k and find -ls will usually give completely numbers, e.g.

$ echo > /tmp/junk.txt
 $ find /tmp -iname junk.txt -ls
981301    4 -rw-r--r--   1 neil     neil            1 Feb  1 
08:02 /tmp/junk.txt
$ du -k /tmp/junk.txt 
4       /tmp/junk.txt
neil ~ 08:02:45 505 $ 

In the above example "find -ls" shows that /tmp/junk.txt is a 1 byte file. On 
the other hand "du -k" reports that it's disk usage is 4kB. Both are correct, 
because they are measuring different things. If you want to know how much 
data is in your files, you need "ls -l" or equivalent. If you want to know 
how much disk space is taken up, you need "du". 

Experienced users will be aware that a file consists of a series of blocks and 
a whole block is allocated whenever there is insufficient space allocated for 
data written to the file. As a result, a whole block will be allocated on 
disk for a 1 byte file and as a block on my filesystem is 4kB that means that 
a single file uses up 4kB of disk space, as reported b "du -k", although it 
only contains 1 byte of data, as shown by "find -ls".

Neil





More information about the TAG mailing list