Apart from cd, rm, cp, and a handful of other commands, ls is the most basic command any Linux user should know. Its primary use is to show files and directories within a file system and giving detailed information. It is available in all systems that use the Linux kernel.
This tutorial will use the ls command to show you how to get information such as file and directory size in a human-readable format.
Basic ls Command Usage
The ls command is pretty straightforward. The general syntax is:
By default, the ls command shows details about files and directories in the current working directory.
For example, run the ls command with no arguments in the home directory as:
Ls arranges all the files and directories in ascending alphabetical order.
To list files outside your current working directory, you can pass an absolute path to the ls command as:
Ls also allows you to pass multiple directories at once. All you have to do is pass the path separated by space. For example:
The command above shows both the files and directories inside the home directory (.) and the /var directory.
Using the ls Command to Show Detailed Information
Using the ls command without any arguments shows only the names of files and directories.
To display more information using the ls command, we can use the -l flag, which shows a comprehensive list of file information.
For example:
When we use the -l flag (long list format), it shows information about the files and directories in a list format.
The information displayed includes the file and directory permissions, user and group ownership, file size, date and time modified, and the actual file and directory name.
Using ls to Show File Size in Human-Readable Format
When we use the ls -l command, it shows the file size of the files and directories. However, it makes much sense to us to tell ls to show file size in human-readable format; we can use the -h command as:
Using the -h flag shows the total size of files and directories and the individual size of each file and directory in a human-readable format.
You can also specify the block size for displaying the file size. By default, the file size is in bytes.
To show in Megabytes, we use the –block-size=M
For example,
However, specifying the block size in Megabytes is unreliable because ls will show 1M for files below 1MB.
You can also set the block size to be Gigabytes using the –block-size=G. Similarly, this will show the smallest size as 1G.
Conclusion
That is all for this one. In this tutorial, we quickly went over the basics of listing files and directories using the ls command and showing the file size in a human-readable format.
Thank you for reading.
from Linux Hint https://ift.tt/3BoKDIu
0 Comments