LS is a useful command to display details of folder. It displays file names, permissions, size, date time of creation and modification and more. However, by default LS command displays file size in bytes which can be difficult to understand. Often you may need to read file size in kb or Mb. In this article, we will look at how to display file size in kB, Mb.
LS File Size in kB, MB
Here is how to display file size in kb in ls command.
1. Display file size in kb
You can easily display file size in kb using -h option. Here is an example.
$ ls -lh total 96K -rw-r--r-- 1 root root 4 Jul 8 05:06 abc.txt -rwxr-xr-x 1 ubuntu ubuntu 278 Sep 11 2020 gpush.sh -rw-rw-r-- 1 ubuntu ubuntu 22K Feb 8 2020 logo-transparent.png -rwxr-xr-x 1 ubuntu ubuntu 1.3K Sep 14 2020 mysql_backup.sh -rwxr-xr-x 1 ubuntu ubuntu 64 Jul 2 2015 postit.sh -rwxr-xr-x 1 root root 262 Mar 14 04:02 remove-old-snaps -rw-rw-r-- 1 ubuntu ubuntu 110 Sep 8 2020 t -rw-rw-r-- 1 ubuntu ubuntu 476 Jul 4 2015 tweet.py -rw-rw-r-- 1 ubuntu ubuntu 39K Feb 29 2016 tweet_list
As you can see, ls command automatically calculates file size in kilobytes or human readable formats. If it is bigger than 1000kb then it will automatically display file size in Mb. If file size is larger than 1000Mb then it will display file size in GB and so on.
2. Using block-size
You can also display block size option in ls to specify the block size to be used to display file size. Here is an example where we set block size of 1Mb to display file sizes in megabytes.
$ ls -l --block-size=M total 1M -rw-r--r-- 1 root root 1M Jul 8 05:06 abc.txt -rwxr-xr-x 1 ubuntu ubuntu 1M Sep 11 2020 gpush.sh -rw-rw-r-- 1 ubuntu ubuntu 1M Feb 8 2020 logo-transparent.png -rwxr-xr-x 1 ubuntu ubuntu 1M Sep 14 2020 mysql_backup.sh -rwxr-xr-x 1 ubuntu ubuntu 1M Jul 2 2015 postit.sh -rwxr-xr-x 1 root root 1M Mar 14 04:02 remove-old-snaps -rw-rw-r-- 1 ubuntu ubuntu 1M Sep 8 2020 t -rw-rw-r-- 1 ubuntu ubuntu 1M Jul 4 2015 tweet.py -rw-rw-r-- 1 ubuntu ubuntu 1M Feb 29 2016 tweet_list
As you can see the above output displays files as megabytes
The main difference between using -h option and –block-list option is that in case of -h option, ls command will automatically display file size kb, mb, gb, depending on the actual file size. On the other hand, if you use –block-list option then ls command will display file size only in terms of the block size you specify.
3. Display only file size
If you want to display only file names and sizes in kb, mb without any other details, then you can use -sh option with ls command as shown below.
$ ls -sh total 96K 4.0K abc.txt 4.0K conf 4.0K gpush.sh 24K logo-transparent.png 4.0K mysql_backup.sh 4.0K postit.sh 4.0K remove-old-snaps 4.0K t 4.0K tweet.py 40K tweet_list
In the about output, ls command simply displays file name along with its size in kb, Mb, Gb depending on its size.
In this article, we have learnt how to display file size in kb, mb using ls command.
Also read:
How to Get User Input in Shell Script
Shell Script to Get CPU and Memory Usage
How to Check SSD Health in Linux
How to Set Default Gateway in Linux
How to Touch All Files in Directory
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.