In Linux, file size is an important attribute that determines the amount of disk space that a file occupies. Checking the size of a file is a simple yet essential task that can help you manage your system’s resources effectively. In this article, we will explore different methods to check the file size in Linux.
Using ls Command
The ls
command is a widely used command in Linux that lists the files and directories in a specified directory. It also provides information about the size of the files. To check the file size using the ls
command, open the terminal and navigate to the directory where the file is located. Then, run the following command:
ls -lh filename
Here, filename
refers to the name of the file whose size you want to check. The -lh
option is used to display the file size in a human-readable format, which makes it easier to read and understand. The output will display the size of the file in bytes, kilobytes, megabytes, or gigabytes, depending on its size.
For example, let’s say we want to check the size of a file named example.txt
located in the Documents
directory. We would run the following command:
ls -lh ~/Documents/example.txt
The output would look something like this:
-rw-r--r-- 1 user user 1.2K Sep 1 15:42 /home/user/Documents/example.txt
Here, the size of the file is displayed as 1.2K
, which means 1.2 kilobytes.
Using du Command
The du
command is another useful command in Linux that is used to estimate the disk space used by a file or directory. To check the file size using the du
command, open the terminal and navigate to the directory where the file is located. Then, run the following command:
du -h filename
Here, filename
refers to the name of the file whose size you want to check. The -h
option is used to display the file size in a human-readable format, which makes it easier to read and understand. The output will display the size of the file in bytes, kilobytes, megabytes, or gigabytes, depending on its size.
For example, let’s say we want to check the size of a file named example.txt
located in the Documents
directory. We would run the following command:
du -h ~/Documents/example.txt
The output would look something like this:
1.2K /home/user/Documents/example.txt
Here, the size of the file is displayed as 1.2K
, which means 1.2 kilobytes.
Using stat Command
The stat
command is a powerful command in Linux that displays detailed information about a file or directory. To check the file size using the stat
command, open the terminal and navigate to the directory where the file is located. Then, run the following command:
stat filename
Here, filename
refers to the name of the file whose size you want to check. The output will display various information about the file, including its size in bytes.
For example, let’s say we want to check the size of a file named example.txt
located in the Documents
directory. We would run the following command:
stat ~/Documents/example.txt
The output would look something like this:
File: /home/user/Documents/example.txt
Size: 1234 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 548324 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ user) Gid: ( 1000/ user)
Access: 2022-09-01 15:42:00.000000000 +0530
Modify: 2022-09-01 15:42:00.000000000 +0530
Change: 2022-09-01 15:42:00.000000000 +0530
Birth: -
Here, the size of the file is displayed as 1234
, which means 1234 bytes.
Conclusion
Checking the file size in Linux is a simple yet essential task that can help you manage your system’s resources effectively. In this article, we explored different methods to check the file size using the ls
, du
, and stat
commands. These commands provide various options to display the file size in a human-readable format, making it easier to read and understand. By using these commands, you can quickly check the size of a file and take appropriate actions to manage your system’s resources.