Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the most reliable command to find actual size of a file linux

Tags:

linux

unix

Recently I tried to find out the size of a file using various command and it showed huge differences.

ls -ltr showed its size around 34GB (bytes rounded off by me ) while du -sh filename showed it to be around 11GB. while stat command showed the same to be around 34GB . Any idea which is the most reliable command to find actual size of the file ?

There was some copy operation performed on it and we are unsure of if this was appropriately done as after a certain time source file from where copy was being performed was removed by a job of ours.

like image 905
Invictus Avatar asked Jul 15 '15 17:07

Invictus


People also ask

How do you find a size of a file in Linux?

Using the ls Command –l – displays a list of files and directories in long format and shows the sizes in bytes. –h – scales file sizes and directory sizes into KB, MB, GB, or TB when the file or directory size is larger than 1024 bytes. –s – displays a list of the files and directories and shows the sizes in blocks.

How do you find the actual file size?

Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

What is size command in Linux?

The size command in Linux is a very important command that will allow to list the section size and the total size of the object files or the archived files in its argument list. When we do not specify the object file in the parameter list, then by default, 'a. out' file is used.

What is the command to check file size in Unix?

don't worry we have a got a UNIX command to do that for you and command is "df" which displays the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory.


1 Answers

There is no inaccuracy or reliability issue here, you're just comparing two different numbers: logical size vs physical size.

Here's Wikipedia's illustration for sparse files:

Elaborate illustration better explained by the Wikipedia Article

ls shows the gray+green areas, the logical length of the file. du (without --apparent-size) shows only the green areas, since those are the ones that take up space.

You can create a sparse file with dd count=0 bs=1M seek=100 of=myfile.

ls shows 100MiB because that's how long the file is:

$ ls -lh myfile
-rw-r----- 1 me me 100M Jul 15 10:57 myfile

du shows 0, because that's how much data it's allocated:

$ du myfile
0 myfile
like image 154
that other guy Avatar answered Oct 26 '22 08:10

that other guy