How To Get File Size In Linux

Introduction

When working with Linux, understanding how to examine file sizes is a crucial aspect. For programmers, system admins, or even ordinary users, this knowledge plays a vital role in managing storage and optimizing the system’s performance. This blog post is a step-by-step guide on achieving this task, specifically focusing on how to get file size in Linux formatted as HTML.

Check File Size Using the ‘ls’ Command

Linux provides a wide range of commands for various functionalities, and one of them is the ls command. The ls command is one of the most versatile commands in Linux, typically used for listing directory contents. However, you can also use it to find out file sizes. Here’s how:

ls -lh filename

The -lh option stands for ‘long format with readable file size.’ Replace “filename” with the name of your file. The command will display the file size in a readable format (K for Kilobytes, M for Megabytes, etc.)

The ‘du’ Command

Another useful command to get the size of a file in Linux is the du command, which stands for ‘Disk Usage.’ Here’s the syntax:

du -sh filename

The -sh option stands for ‘summarize’ and ‘human-readable,’ respectively. Again, replace “filename” with the name of your file.

Output Formatting as HTML

To format the output as HTML, we can make use of the echo command and the command substitution feature in Linux. Here is how you can do it:

echo “

In this command, replace both instances of “filename” with the name of your file. The $(du -sh filename) part is a command substitution that will run the command and replace it with its output.

Conclusion

As we’ve seen, checking file sizes in Linux is quite straightforward, thanks to the versatile commands like ls and du. What’s more, with the ability to format the output as HTML through command substitution and the echo command, you can efficiently manage your files in a readable and presentable manner.