Skip to Content

5 commands to check file size in Linux

In this blog post, we will discuss 5 commands to check the file size in Linux. File size is an important metric to consider when you are trying to optimize your website or application. If your files are too large, it can cause performance issues and slow down your site. In this article, we will show you how to check the file size of a file in Linux, as well as some tips to reduce the file size if necessary. Let’s get started!

Use the du Command to check file size in Linux

The du command is a Linux utility that allows you to see how much disk space a file or directory is taking up. This can be useful when you want to find out which files are taking up the most space on your hard drive. To use the du command, simply type “du” followed by the name of the file or directory you want to check. For example, if we wanted to check the file size of the “/etc/passwd” file, we would type: $ du /etc/passwd

This would return the following output: 40 /etc/passwd

Use the ls Command to check file size in Linux

The ls command is another Linux utility that can be used to check file sizes. The ls command will list all of the files in a directory, as well as their sizes. To use the ls command, simply type “ls” followed by the name of the directory you want to check. For example, if we wanted to check the file sizes in the “/etc/” directory, we would type:$ ls -l /etc/

This would return a list of all the files in the “/etc/” directory, as well as their sizes. Here is a sample of the output you might see:

-rw-r–r– root root 4096 Apr 18 etc/passwd
-rw-r–r– root root 4096 Apr etc/group
-rw-r–r– root root 4096 Apr etc/shadow

Use the stat Command to check file size in Linux

The stat command is another Linux utility that can be used to check file sizes. The stat command will give you detailed information about a file, including its size. To use the stat command, simply type “stat” followed by the name of the file you want to check. For example, if we wanted to check the file size of the “/etc/passwd” file, we would type: $ stat /etc/passwd

This would return the following output:
File: `/etc/passwd’
Size: 4096 Blocks: etcetera…

Use the find Command to check file size in Linux

The find command is a Linux utility that allows you to search for files based on certain criteria. One of the criteria you can search by is file size. To use the find command to find files based on file size, you need to use the “-size” option. For example, if we wanted to find all files in the “/etc/” directory that are larger than 1024 bytes, we would type:

$ find /etc/ -size +1024

This would return a list of all the files in the “/etc/” directory that are larger than 1024 bytes.

Use the du -sh Command to check file size in Linux

The “du -sh” command is a variation of the “du” command that we discussed earlier. The “-sh” option stands for “summary human-readable.” This means that the output of the “du -sh” command will be a summary of the file sizes, in human-readable format. For example, if we wanted to check the file size of the “/etc/passwd” file, we would type:

$ du -sh /etc/passwd

This would return the following output: 40K /etc/passwd

As you can see, the output of the “du -sh” command is much easier to read than the output of the “du” command.

best practice for file size in linux

There are a few best practices you can follow to reduce the file size of your files in Linux. Here are a few of them:

1. Use the correct file format.

Some file formats, such as JPEG and GIF, are designed for images and tend to be smaller than file formats such as PDF and Word. If you’re working with images, use an image file format. If you’re working with text, use a text file format.

Note: You can use the file command to determine the file format of a given file. For example, if we wanted to check the file format of the “/etc/passwd” file, we would type:$ file /etc/passwd

This would return the following output: /etc/passwd: ASCII text

As you can see, the “/etc/passwd” file is in the ASCII text file format.

2. Use a File Compression Utility

If you’re working with files that are in a text-based file format, such as HTML or CSS, you can use a file compression utility, such as gzip or bzip. File compression utilities work by reducing the size of the files they compress. This can be helpful if you’re working with a large number of files, or if you need to save space on your hard drive.

To use gzip to compress a file, simply type “gzip” followed by the name of the file you want to compress. For example, if we wanted to compress the “/etc/passwd” file, we would type: $ gzip /etc/passwd

This would create a new file, “/etc/passwd.gz”, which is the compressed version of “/etc/passwd”.To uncompress a file, you can use the gunzip command. For example, if we wanted to uncompress the “/etc/passwd.gz” file, we would type: $ gunzip /etc/passwd.gz

This would uncompress the file and create the “/etc/passwd” file.

3. Use a File Archiver Utility

If you’re working with a large number of files, or if you need to save space on your hard drive, you can use a file archiver utility, such as tar or zip. File archiver utilities work by compressing a group of files into a single file. This can be helpful if you need to transfer a large number of files, or if you need to save space on your hard drive.

To use tar to compress a group of files, simply type “tar” followed by the “-czf” options and the name of the file you want to create. For example, if we wanted to compress the “/etc” directory, we would type: $ tar -czf etc.tar.gz /etc

This would create a new file, “etc.tar.gz”, which is the compressed version of the “/etc” directory.To uncompress a tar file, you can use the tar command with the “-xzf” options. For example, if we wanted to uncompress the “etc.tar.gz” file, we would type: $ tar -xzf etc.tar.gz

We hope this article has been helpful and that you now know how to check file size in Linux. If you have any questions or comments, please leave them below. Thanks for reading!

Merial

Tuesday 14th of February 2023

It is very helpful. Thanks.