In this article, we’ll show how to display size human readable format command line and when to use them. We will also talk about why the ls command and the du command sometimes give different sizes for the same file. Most command line tools show numbers in bytes by default. This is fine for small text files, but when you start working with bigger things, it becomes hard to read and understand.
The answer is simple: pass a “human readable” flag with the command. This will change the bytes to kilobytes, which are much easier for people to understand. With the “ls” command, you can see all of the files and subdirectories in the present directory. By default, ls shows a list of all the files and directories in a directory, with each file or directory on its own line.
How to display size human readable format command line
- You probably already know that you can use ls command with long listing option -l to show file size in Linux.
ls -l
- But unfortunately, the long listing shows the file size in blocks and that’s not of much use to us humans.
- The good thing is that you can combine the option -l with -h to show the file size in a human-readable format.
ls -lh
- Did you notice the size of new_dir directory? It is 4 KB. If you use ls -lh command on directories, it always shows the size of directory as 4.0 K.
- By default, the block size in most Linux system is 4096 Bytes or 4 KB. A directory in Linux is simply a file with the information about the memory location of all the files in it.
- You can force ls command to display file size in MB with the –block-size flag.
ls -l --block-size=M
- The problem with this approach is that all the files with a size of less than 1 MB will also be displayed with file size of 1 MB.
- The ls command also has -s option to display size. You should combine with -h to show the file size in human readable form.
ls -sh
- 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.
FAQs
The size can also be shown with the -s option of the ls tool. You should use -h to show the file size in a way that people can understand. You can sort files by size with the -S option of the ls tool. This also lets you know which files take up the most space on your computer.
For files, use the ls command and for directories, use the du tool. Why won’t the ls tool show the real size of a directory? Because of this, we use du for this. If you add the -h option to any of the above tools (for example, ls -lh * or du -sh), the size will be shown in a way that humans can understand (kb, mb, gb, etc.