I want to print the size of a file from some folder /etc/*.conf in this case.

When I use:

cd /etc

du -ch $(ls | grep .conf) | tail -1 | cut -f1 

I get 120K.

When I use:

du -bch $(ls | grep .conf) | tail -1 | cut -f1

I get 46K. and this should be the same size but in bytes right? so it should be some kind of 120000, right?

When i use:

du -bsh $(ls | grep .conf) | tail -1 | cut -f1

I get 1.3K, what is this man?

  • Why you do not use the answers from your previous question? unix.stackexchange.com/questions/483135/… – Romeo Ninov Nov 21 '18 at 10:42
  • @RomeoNinov it's not like that, if I use that I don't think it gives me the right size amount, and I need just an explanation here – C. Cristi Nov 21 '18 at 10:46
  • This seems like xyproblem.info . Please create new question with the real problem, not the way you think you can resolve it. – Romeo Ninov Nov 21 '18 at 10:50

du -hc shows the size of the files as multiples of the block size of the filesystem, which is typically 4K. This is actual disk usage of the listed files. The -h option prints sizes using a human-readable format (K/M/G) and the -c option prints the grand total size at the end of the list.

[root@testvm1 etc]# du -hc *.conf
4.0K    asound.conf
4.0K    chrony.conf
4.0K    dracut.conf
4.0K    vconsole.conf
4.0K    yum.conf
104K    total

With du -bch, the -b parameter lists the file sizes in bytes. However, this option also implies the --apparent-size option, which shows the apparent sizes of files as opposed to their disk usage. This usually results in a lower total:

[root@testvm1 etc]# du -bch *.conf
55      asound.conf
1.1K    chrony.conf
1.3K    dracut.conf
41      vconsole.conf
970     yum.conf
32K     total

du -sh uses the -s option, which displays the total size for each argument. This option is typically used with directories. When used against a list of files, the -s option does not produce any special output. The output of your du -bsh command will most likely be the size of the last file in list.

The -b option is equivalent to --apparent-size --block-size=1. To use block-size calculations while keeping the unit as bytes, use only the --block-size option.

[root@testvm1 etc]# du --block-size=1 -c *.conf
4096    asound.conf
4096    chrony.conf
4096    dracut.conf
4096    vconsole.conf
4096    yum.conf
106496  total
  • I see and what do I do if I have to display du -hc in bytes? – C. Cristi Nov 21 '18 at 10:55
  • du -bc will show all of the file sizes and the grand total in bytes. – Haxiel Nov 21 '18 at 10:59
  • I want the rounded size, with hc in bytes, how can I do that – C. Cristi Nov 21 '18 at 11:22
  • @C.Cristi Please see the edit. – Haxiel Nov 21 '18 at 12:15
  • It has nothing to do with rounding. Haxiel, good edit! @Haxiel – Michael Prokopec Nov 21 '18 at 12:35

You should check the result of du without the | tail -1 | cut -1

(ignoring the -h option which just add the k, M...)

(based on http://man7.org/linux/man-pages/man1/du.1.html)

-c will print the disk usage of all files plus a total. note that depending of your filesystem format disk usage of a file will be bigger than its real size)

-bc will print the "real" size rather than the size its use on the disk.

-bs will only print the total "real" size of each file/folder given to du. Since you dive du each file it will calculate the size of each *.conf file and your last line is the size of the last *.conf file you've given him.

PS: you can probably do: du -bch *.conf rather than the grep on ls result.

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.