Linux Tactic

Mastering File Size in Linux: The Ultimate Guide

The Ultimate Guide to File Size in Linux

As a Linux user, you are probably familiar with the ls command, which is used to list all files and directories in a particular location. However, did you know that you can use the ls command to display file size in human-readable format?

In this article, we will show you how to display file size in Linux, using the ls command and other related commands.

Displaying File Size with ls Command

The ls command is one of the most commonly used commands in Linux. It allows you to list all files and directories in a particular location.

However, by default, it only displays the names of the files and directories. If you want to see the size of the files, you can use the following command:

$ ls -lh

The -l option is used to display detailed information about each file, including the size, permissions, owner, and creation date.

The -h option is used to display the file size in a human-readable format. For example, instead of displaying the file size in bytes, it will display it in kilobytes (KB), megabytes (MB), or gigabytes (GB) depending on the size.

Here’s an example output:

“`

-rw-r–r– 1 user user 1.2M Oct 19 13:00 file.txt

drwxr-xr-x 2 user user 4.0K Oct 19 13:16 directory

“`

The first line shows the size of the file “file.txt” as 1.2 MB. The second line shows the size of the directory “directory” as 4 KB.

Checking Size of Directory with du Command

What if you want to check the size of a directory and all its subdirectories? For this, you can use the du command.

It stands for “disk usage” and is used to display the size of files and directories on disk. The default output of du is in bytes, but you can use the -h option to display the output in human-readable format.

Here’s an example command:

“`

$ du -h /home/user/

“`

The above command will display the size of all files and directories under the /home/user/ directory in human-readable format.

Additional Options for Displaying File Size

Apart from the ls and du commands, there are other options available to display file size in Linux.

Displaying File Size in MB with –block-size Flag

By default, ls displays the file size in bytes. However, you can use the –block-size flag to display the size in MB or other units.

Here’s an example command:

“`

$ ls -l –block-size=M

“`

The above command will display the size of each file in MB.

Using -s Option to Display Size in Human-Readable Form

Another option is to use the -s option with the ls command. The -s option shows the file size in blocks.

To display the output in human-readable format, you can use the -h option as well. Here’s an example command:

“`

$ ls -sh

“`

The above command will display the size of each file in human-readable format.

Sorting Files by Size with -S Option

The ls command also allows you to sort files by size using the -S option. This option sorts the files in descending order of size.

Here’s an example command:

“`

$ ls -lS

“`

The above command will sort the files in the current directory by size (largest file first).

Checking File Size with stat Command

If you want to check the size of a specific file, you can use the stat command. The stat command is used to display information about a file or directory, including its size.

Here’s an example command:

“`

$ stat file.txt

“`

The above command will display detailed information about the file.txt file, including its size.

Conclusion

In this article, we have shown you how to display file size in Linux using various commands. The ls command is the most commonly used command to display file size in human-readable format.

However, there are other options available such as the du command, which can be used to check the size of a directory and all its subdirectories. Additionally, the –block-size flag, -s option, -S option, and stat command can also be used to display file size in Linux.

By learning these commands, you can get a better understanding of the size of your files and directories, which can be helpful for managing disk space on your computer. In this article, we explored the various ways to display file size in Linux using the ls command, du command, and other related commands.

We learned how to use the ls command to display file size in human-readable format, and the du command to check the size of a directory and all its subdirectories. We also discussed other options such as the –block-size flag, -s option, -S option, and stat command that can be used to display file size in Linux.

By understanding these commands, we can manage disk space on our computers more efficiently and effectively. Overall, knowing how to display file size in Linux is crucial for working with files and directories, and this article provides a comprehensive guide to do so.

Popular Posts