How to Make ls Command to Show File Sizes in Megabytes in Ubuntu
Topic: Ubuntu / LinuxPrev|Next
Answer: Use the
If you strictly want
ls command to show the file sizes in MB or KB you can use the '--block-size=SIZE' option. It scale file sizes by SIZE before printing them; e.g.,
--block-size=M prints sizes in units of 1,048,576 bytes. When dealing with memory 1 MB = 1,024 KB = 1,048,576 bytes.
The SIZE argument is an integer and optional unit (for example: 10K is 10*1024). Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
The above command however also shows 1M for any file that has size less than 1 MB.
Alternatively, if you simply want to scale the file sizes for readability purpose, rather than specifically showing it in megabytes or gigabytes, you can use the option
-l. This will print human readable sizes, i.e., reduce the number of digits to three or less (e.g., 1K, 20M, 5G, etc.).
Here are some more FAQ related to this topic:
- How to change current working directory in Ubuntu terminal
- How to get the size of a directory from the command line in Ubuntu
- How to create an empty file from command line in Ubuntu