Size of a directory or a file

Hello,

Here is my code:

:~$ truncate -s 16M MyTestFile.txt 
:~$ du -h MyTestFile.txt 
    4,0K    MyTestFile.txt

Q1: Please why du -h does not work in this case ?
Q2: Other than "du -h", how can i get the size of a directory (using linux command)

Thanks a lot.
Best Regards.

Q1: "du" works, you created a sparse file, i.e. a file that doesn't use disk space for (some or all of) its data.

Q2: To get the value you expect, use:

$ du --apparent-size -h MyTestFile.txt
16M	MyTestFile.txt

To create a non-sparse file, you can use :

$ dd if=/dev/zero of=MyTestFile.txt bs=1M count=16
1 Like

I'm going to guess that truncate sets up a sparse file... so it has "size" but it's doesn't occupy space :slight_smile:

Try dd instead to create a file.

dd of=MytestFile.txt bs=1M count=16 if=/dev/zero

@jliagre, sorry, I was in mid post before I saw your post... but great minds think alike.

1 Like