Which Command is Used to Check the Size of a File in Linux?

In Linux OS, file management plays a vital role in enhancing the system performance. In this management, the file size varies due to updating or upgrading packages. Considering its importance, this guide provides various methods to check the size of a file. It is quite useful to balance the space on the hard drive. The content that illustrates this guide is as follows:

Let’s dig into it one by one.

Method 1: Check File Size in Linux Using the “du” Command

The “du” command is utilized to compute the file sizes. It tells how much a particular file or directory occupies space in the disk. The “du” command syntax is given below:

Syntax:

$ du [Options] [File]

For more details on the “du” utility, use the “–help” flag of the du command as follows:

$ du --help

Let’s get into the usage of this utility.

Example 1: Check Size of All Files in Linux

The “du” command works with the “-h” flag to make the file readable for humans. It displays the hard disk memory usage of different directories and files as shown below:  

$ du -h

Example 2: Check Size of a Particular File in Linux

In Linux, you can compute a particular file size by specifying the name of the file through the “du” command as below:

$ du -h UnitySetup-2022.1.21f1

The specified file contains memory “744K” in the hard drive.

Similarly, the users can provide the file’s path to get its space usage. 

Let’s explore another method.

Method 2: Check File Size in Linux Using the “ls” Command

An alternative method can be adapted through the “ls” command to check the file size in Linux. The syntax of the “ls” utility is provided below:

Syntax:

$ ls [Option] [File]

Furthermore, you can display all the mandatory arguments of the “ls” command through the below script:

$ ls - help

Let’s get into the examples of the “ls” command.

Example 1: Check Size of All Files/Directories

In Linux, the “ls” command displays the list of information about files. Using this command to the “-lh” flag helps get the file size of the current directory file and make it presentable for the human eye:

$ ls -lh

Now, you can check different file sizes highlighted in the above figure.

Example 2: Check Size of a Particular File

To check out the disk space for a particular file, the “ls” command is written with the “-l” flag specifying the name of the file as below:

$ ls -l UnitySetup-2022.1.21f1

After executing the command, users can check the total memory of “UnitySetup-2022.1.21f1” file has “758432” bytes.

Let’s explore another method.

Method 3: Check File Size in Linux Using the “ncdu” Command

In Linux, the “stat” command displays the file system’s status by mentioning the file size. The stat command depends on the syntax provided below:

$ stat [Option] [File]

To explore various arguments through the below script, you can use the “–help” flag as follows:

$ stat --help

Example: Check the Size of a File in Linux

For computing the size of a particular file, the “stat” command can be utilized to display the complete status that requires the file name:

$ stat UnitySetup-2022.1.21f1

After executing the command, you can see complete information of the specified file, particularly 758432 file size.

Method 4: Check File Size in Linux using the “ncdu” Command

Another method is followed to check the file size using the “ncdu” command from the Linux terminal. This command is utilized to display the disk usage in the operating system. Let’s install the “ncdu” command

Install ncdu Command in Ubuntu Linux

To explore the features of the “ncdu” command, install this command through the root privilege by the following script:

$ sudo apt install ncdu

Example 1: Check Size of All Directories

After the installation of “ncdu” command, the size of all directories can be checked through executing the below script:

$ ncdu

After executing the command, you can visualize the disk space of all directories and temporary files in the Home directory.

Example 2: Check the Disk Usage of a Particular File

An example is considered for computing the disk space in Linux. To do so, press the “i” key from the keyboard that specifies the “Item info” in a terminal window:

After pressing the “i” key, you can verify the “Disk usage” of the “Templates” file is “4.0 KiB”.

That is all from this guide.

Conclusion

In Linux, the “du”, “ls”, “stat” and “ncdu” commands are used to check the file size from the terminal (command line interface). All these commands require a particular file name for computing the hard drive’s memory. Users can use these commands to remove unnecessary large files to boost the operating system. This guide has explained all possible methods to calculate the size of a file in Linux.