This article will demonstrate how to use the linux ls command to show a human readable file size in GB, MB, and K. The same command and set of options is used for showing each classification of size, but we have documented each of them separately for clarity.
ls in gigabytes
GB is short for gigabytes. A GB is equal to 1024 megabytes (MB) is equal to 1024 kilobytes (K or KB). If a file size is in upwards of gigabytes, it will show up with GB after the number.
To show results of the ls command in GB, run the following command.
The -h is key, which stands for human readable.
The other options are defined as:
- -a – List all entries in the directory including the hidden files and sub directories
- -l – Format into a long list
ls in megabytes
The command to show file size in megabytes with ls is the same as for gigabytes.
ls in kilobytes
The command to show file size in kilobytes with ls is the same as for gigabytes and megabytes.
ls shortcut alias
If you have a set of preferred options when using the ls command you can create an alias to automatically use those options each time. In our case, we simply want to override the ls command with the ls -lah options. The quickest way to do so is to run the following alias command:
alias ls='ls -lah'
The alias command is a temporary and not persistent command. The alias will only be created for the open terminal session. If you want the command to persist you will need to edit the .bashrc file.
Conveniently, there is already a section with ls aliases. You should find the following section, at least for WSL2 on Windows using Ubuntu.
# some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF'
Simply add the previous alias to the list in the .bashrc file.
alias ls='ls -lah'
The change is saved, but you must either source the file or close and reopen your terminal.
With that, you’re on your way to quicker ls commands.
Conclusion – linux ls human readable
This article has demonstrated how to run ls to show human readable file sizes in gigabytes, megabytes, and kilobytes. Note that if no suffix is appended to the file size, it is less than a kilobyte and is being shown in the standard bytes format. Let us know in the comments if you have any questions or would like to see more examples of the ls command. Read our article on the du command for showing directory and file sizes or any other article.
Leave a Reply