Tux

...making Linux just a little more fun!

Talkback:171/lg_tips.html

[ In reference to "2-Cent Tips" in LG#171 ]

Ben Okopnik [ben at linuxgazette.net]


Sun, 31 Jan 2010 12:37:42 -0500

On Sun, Jan 31, 2010 at 01:15:02PM +0000, Thomas Adam wrote:

> On Sun, Jan 31, 2010 at 04:39:55PM +0700, Mulyadi Santosa wrote:
> > Most CLI aficionados use this kind of construct when executing a
> > command over "find" results:
> > 
> > $ find ~ -type f -exec du -k {} \;
> > 
> > Nothing is wrong with that, except that "du" is repeatedly called with
> > single argument (that is the absolute path and the file name itself).
> > Fortunately, there is a way to cut down the number of execution:
> > 
> > $ find ~ -type f -exec du -k {} +
> 
> http://linuxgazette.net/111/tag/3.html

What would be wrong with just using 'du -sk ~'?

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Mon, 1 Feb 2010 09:27:01 +0700

Hi Ben :)

On Mon, Feb 1, 2010 at 12:37 AM, Ben Okopnik <ben@linuxgazette.net> wrote:

> What would be wrong with just using 'du -sk ~'?

The above command would show the total size of current user's home directory, while "find ~ -type f -exec du -k {} +" will show the size of each file inside current user's home directory.

-- 
regards,
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Sun, 31 Jan 2010 21:49:23 -0500

On Mon, Feb 01, 2010 at 09:27:01AM +0700, Mulyadi Santosa wrote:

> Hi Ben :)
> 
> On Mon, Feb 1, 2010 at 12:37 AM, Ben Okopnik <ben@linuxgazette.net> wrote:
> > What would be wrong with just using 'du -sk ~'?
> 
> The above command would show the total size of current user's home
> directory, while "find ~ -type f -exec du -k {} +" will show the size
> of each file inside current user's home directory.

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.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Neil Youngman [ny at youngman.org.uk]


Mon, 1 Feb 2010 08:59:12 +0000

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


Top    Back


Neil Youngman [ny at youngman.org.uk]


Mon, 1 Feb 2010 09:08:01 +0000

On Monday 01 February 2010 08:59:12 Neil Youngman wrote:

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

That should read "completely different numbers".

Neil


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Mon, 1 Feb 2010 19:18:32 +0700

On Mon, Feb 1, 2010 at 4:08 PM, Neil Youngman <ny@youngman.org.uk> wrote:

> On Monday 01 February 2010 08:59:12 Neil Youngman wrote:
>> Well du -k and find -ls will usually give completely numbers, e.g.
>
> That should read "completely different numbers".

Thanks a lot Neil.....you are faster than me on explaining that :)

-- 
regards,
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 1 Feb 2010 11:04:27 -0500

On Mon, Feb 01, 2010 at 08:59:12AM +0000, Neil Youngman wrote:

> 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.

I'm quite aware of that. That's why I gave the second solution - just in case Mulyadi wanted the number of blocks instead of the filesize.

> 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".

Please watch carefully; the special effects are quite expensive. :)

ben@Jotunheim:~$ mkdir /tmp/foo; echo > /tmp/foo/x
ben@Jotunheim:~$ du -k /tmp/foo/x
4	/tmp/foo/x
ben@Jotunheim:~$ find /tmp/foo -name x -printf "%k\t%h/%f\n"
4	/tmp/foo/x
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 1 Feb 2010 11:15:12 -0500

On Mon, Feb 01, 2010 at 11:04:27AM -0500, Benjamin Okopnik wrote:

> 
> Please watch carefully; the special effects are quite expensive. :)
> 
> ```
> ben@Jotunheim:~$ mkdir /tmp/foo; echo > /tmp/foo/x
> ben@Jotunheim:~$ du -k /tmp/foo/x
> 4	/tmp/foo/x
> ben@Jotunheim:~$ find /tmp/foo -name x -printf "%k\t%h/%f\n"
> 4	/tmp/foo/x
> '''

Oh, and - as a double-check:

ben@Jotunheim:~$ find ~/tmp -type f -printf "%k\n"|awk '{a+=$1}END{print a}'
3460256
ben@Jotunheim:~$ find ~/tmp -type f -exec du -k {} +|awk '{a+=$1}END{print a}'
3460256
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Neil Youngman [ny at youngman.org.uk]


Mon, 1 Feb 2010 16:18:57 +0000

On Monday 01 February 2010 16:04:27 Ben Okopnik wrote:

> > Well du -k and find -ls will usually give completely numbers, e.g.
>
> I'm quite aware of that. That's why I gave the second solution - just in
> case Mulyadi wanted the number of blocks instead of the filesize.

Yeah, I would have been surprised if you didn't know the difference, but I wouldn't say that your choice of words made it at all obvious for the casual reader. Hopefully I added a little clarity for our less experienced readers, if it gets into the Gazette.

I agree that I should have taken a closer look at your second option and acknowledged that it was a "simpler" way of getting the same result as "du -k".

Neil


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 1 Feb 2010 11:40:39 -0500

On Mon, Feb 01, 2010 at 04:18:57PM +0000, Neil Youngman wrote:

> On Monday 01 February 2010 16:04:27 Ben Okopnik wrote:
> > > Well du -k and find -ls will usually give completely numbers, e.g.
> >
> > I'm quite aware of that. That's why I gave the second solution - just in
> > case Mulyadi wanted the number of blocks instead of the filesize.
> 
> Yeah, I would have been surprised if you didn't know the difference, but I 
> wouldn't say that your choice of words made it at all obvious for the casual 
> reader.

You might be right. Thanks for clarifying it!

I guess we're following the best of USENET tradition by doing it this way: the best way to get a question answered there was always to make an emphatic statement rather than asking. That way, you'd immediately get several hundred opinions, in detail. You'd have to filter for flamage, though. :)

> Hopefully I added a little clarity for our less experienced readers, 
> if it gets into the Gazette.

Neil, other than filtering for flamage and spam, everything on this list is grist for the LG mill. We've got a bunch of smart, focused people, some of whom have been here for years, and most of what's said here definitely deserves to be preserved as a community resource. You're a big part of that.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Tue, 2 Feb 2010 01:13:17 +0700

On Mon, Feb 1, 2010 at 11:18 PM, Neil Youngman <ny@youngman.org.uk> wrote:

> I agree that I should have taken a closer look at your second option and
> acknowledged that it was a "simpler" way of getting the same result
> as "du -k".

Thanks for sharing the thoughts, Ben. Yes, personally I think your solution is better than mine.

-- 
regards,
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 1 Feb 2010 14:11:59 -0500

On Tue, Feb 02, 2010 at 01:13:17AM +0700, Mulyadi Santosa wrote:

> On Mon, Feb 1, 2010 at 11:18 PM, Neil Youngman <ny@youngman.org.uk> wrote:
> > I agree that I should have taken a closer look at your second option and
> > acknowledged that it was a "simpler" way of getting the same result
> > as "du -k".
> 
> Thanks for sharing the thoughts, Ben. Yes, personally I think your
> solution is better than mine.

I didn't think that there was a contest in progress. :) Instead, as often happens in TAG, several people suggest solutions and you (and everyone else, which is the point) get to pick whatever is most applicable to their situation. I'm glad I could help, though.

In addition, that '{} +' trick is one that I'd managed to miss in 'find'. Thanks for the great tip!

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back