I can use ls -l
to get the logical size of a file, but is there a way to get the physical size of a file?
Linux – How to get the physical size of a file in Linux
fileslinuxsize;
Related Question
- Linux – Physical and logical extents size
- Linux – Why is the size of “/proc/kcore” file so bigger than the physical memory size
- Linux – How to view the size of the binary files using linux command
- Linux – Why does “ls -l” report the physical size for a directory while it reports the logical size for a file
- Linux – How to get only the name of the physical ethernet interface
- Linux – Check If File Size Is Greater Than 1MB Using IF Condition
Best Answer
ls -l
will give you the apparent size of the file, which is the number of bytes a program would read if it read the file from start to finish.du
would give you the size of the file "on disk".By default,
du
gives you the size of the file in number of disk blocks, but you may use-h
to get a human readable unit instead. See also the manual fordu
on your system.Note that with GNU coreutil's
du
(which is probably what you have on Linux), using-b
to get bytes implies the--apparent-size
option. This is not what you want to use to get number of bytes actually used on disk. Instead, use--block-size=1
or-B 1
.With GNU
ls
, you may also dols -s --block-size=1
on the file. This will give the same number asdu -B 1
for the file.Example:
This means that this is a 512 MB file that takes about 24 KB on disk. It is a sparse file (mostly zeros that are not actually written to disk but represented as logical "holes" in the file). Sparse files are common when working with pre-allocated large files, e.g. disk images for virtual machines or swap files etc. Creating a sparse file is quick, while filling it with zeros is slow (and unnecessary).
See also the manual for
fallocate
on your Linux system.