Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use stat to show human readable size of file?

Tags:

bash

I'm trying to use stat command to display a list of files in the current directory, sorted by largest to smallest. my script is

filelist=$(ls -p | grep -v/)
filesize=$(stat -c "%s : %n" $filelist | sort -nr)

It works however it displays the size in bytes. Is there a way to show it in a human readable format using stat? or do i have to try something else. thank you for the help.

like image 989
comearound Avatar asked Feb 14 '20 15:02

comearound


People also ask

How can I tell the size of a file in human readable format Linux?

The ls command also has -s option to display size. You should combine with -h to show the file size in human readable form. You can use the -S option of the ls command to sort files by size. This is also helpful in knowing which files take the most space on your system.

Does ls show file size?

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 check the size of a file?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

What does STAT command do?

The stat is a command which gives information about the file and filesystem. Stat command gives information such as the size of the file, access permissions and the user ID and group ID, birth time access time of the file. Stat command has another feature, by which it can also provide the file system information.


1 Answers

As far as I know, the stat program cannot display human readable sizes by itself. But you can always pipe it to another program that does it, such as numfmt:

stat -c %s /path/to/file | numfmt --to=iec

Applied to your example, it would be:

filelist=$(ls -p | grep -v/)
filesize=$(
    stat -c "%s %n" $filelist | sort -nr -k1 | while read filesize filename; do
        printf '%s : %s\n' "$(numfmt --to=iec <<< $filesize)" "$filename"
    done
)

Please note I added the -k1 option when calling sort because I assume you want to sort using the size, not the name.

numfmt has the advantage that you can choose how to want to display the human readable size. I suggested --to=iec because this is the most common for file sizes, but you may want to use other conversions. Please refer to the numfmt man page.

As a last note, I would advise you against storing files directly out of the $() capture because it will not work when a filename contains a space character. You could use find to list the files and get the size at the same time, e.g.:

find . -mindepth 1 -maxdepth 1 -not -type d -printf '%s %f\n' |
    sort -nr -k1 |
    while read filesize filename
do
    printf '%s : %s\n' "$(numfmt --to=iec <<< $filesize)" "$filename"
done
like image 153
vdavid Avatar answered Oct 20 '22 20:10

vdavid