| by Arround The Web | No comments

How to Check the Size of a File in Linux

The information about the directories is essential as it helps a Linux user to check and maintain the storage. Furthermore, checking the file storage is necessary to maintain the system performance and disk storage.

Although there are multiple methods to check the file size in Linux, many beginners get confused in using them. So, in this short blog, we listed all the possible ways to check the size of a file in Linux without facing any error.

How to Check the Size of File in Linux

There are several commands in Linux to find the file size. Here, we will cover some of the commands to determine the file size. But first of all, go to the directory where your file exists and whose size you want to know.

In the “Documents” directory, we have a file named “linuxhint.sh” and we will check the size of this file using various commands.

1. The Du Command

The “du” command is already defined as “disk usage” which is one of the standard commands of Linux to determine the file size. First, locate the directory where your file exists whose size you want to find out.

The general syntax of the “du” command to find the file size is as follows:

du <file_name>

For example, the filename is “linuxhint.sh”. For this, we run the following command in the terminal:

du linuxhint.sh

You can also use the “-h” flag with the previous command which provides the output in human-readable format.

2. The Stat Command

As you can see from the command name, the “stat” command shows the file status including the file size. Run the following command to display the filesystem details:

stat linuxhint.sh

The previous command listed a detailed output including the size, last access, permissions, etc. You can only display the file size through this command using the “%s” (format specifier) and “-c” flag.

stat -c %s linuxhint.sh

Here, “-c →” specifies the format when the command displays the output and “%s →” shows the file size (in bytes).

3. The Ls Command

Generally, the “ls” command lists the directories and files. Meanwhile, this command is also used to determine the file size.

ls -lh linuxhint.sh

The “-l” flag indicates the long listing format (file type, file permissions, hard links) to the “ls” command.

4. The Wc Command

Generally, the “wc” command determines the file’s number of words, lines, and characters. To determine the file size through this command, you must use the “-c” option. In this command, the “-c” option flag prompts the “wc” command to count the file size that is specified in the command in bytes.

Conclusion

This is all about the simple methods that you can use to check the size of a file in Linux through several commands. If you face a performance issue in your system, we recommend you to check the storage and directory size and remove the unnecessary ones. Moreover, run the commands correctly or you will get errors while executing them.

Share Button

Source: linuxhint.com

Leave a Reply