Introduction
Ubuntu is one of the widely used operating systems in the world. It is mostly used by developers and system administrators. One of the important tasks of a system administrator is to check the file size of different files in the system. In this blog post, we will discuss different ways to check file size in Ubuntu.
Using ‘ls’ Command to get file size
One of the easiest ways to check file size in Ubuntu is to use the ‘ls’ command. This command is used to list files in a directory. To check the size of a file using the ‘ls’ command, open the terminal and navigate to the directory where the file is located. Now, run the following command:
ls -lh filename.txt
This command will display the details of this file in Linux, including its size, permissions, owner, group, and modification date and time. Here’s what each part of the command means:
- ls: This is the command used to list files and directories in Linux.
- -l: This option tells the ls command to use a long listing format, which displays additional information about each file or directory.
- -h: This option tells the ls command to display file sizes in “human-readable” format (e.g. using units like KB, MB, GB instead of just bytes).
- filename.ext: This is the name of the file you want to display information for. Replace “filename.ext” with the actual name of your file.
Here’s an example of how to use this command:
$ ls -lh filename.txt
-rw-r--r-- 1 user group 2.3K Sep 20 11:30 filename.txt
The output shows that filename.txt has a size of 2.3 kilobytes (2.3K) and was last modified on September 20th at 11:30 am. It also shows that the file is owned by “user” and belongs to the “group” group.
Using ‘du’ Command to check file size
Another way to check file size in Ubuntu is to use the ‘du’ command. This command is used to estimate the file space usage. To check the size of a file using the ‘du’ command, open the terminal and navigate to the directory where the file is located. Now, run the following command:
du -sh filename.txt
In this command, ‘s’ stands for summarize, and ‘h’ stands for human-readable format. ‘filename.ext’ is the name of the file whose size you want to check.
du -sh filename.txt
2.3K filename.txt
Using ‘stat’ Command to check file size
The ‘stat’ command is another way to check file size in Ubuntu. This command is used to display file or file system status. To check the size of a file using the ‘stat’ command, open the terminal and navigate to the directory where the file is located. Now, run the following command:
stat filename.txt
In this command, ‘filename.ext’ is the name of the file whose size you want to check. The output of this command will display various details about the file, including the size of the file in bytes.
stat filename.txt
File: filename.txt
Size: 2350 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 131073 Links: 1
Access: (0644/-rw-r--r--) Uid: (1000/username) Gid: (1000/username)
Access: 2022-01-01 12:34:56.789012345 +0000
Modify: 2022-01-01 12:34:56.789012345 +0000
Change: 2022-01-01 12:34:56.789012345 +0000
Birth: -
The stat command displays detailed information about a file, including its size, permissions, ownership, timestamps, and more.
The output shows that filename.txt is a regular file with a size of 2350 bytes and is made up of 8 blocks. It also displays the device and inode number for the file, as well as the number of links to it.
Conclusion
Checking file size is an important task for system administrators. In this blog post, we discussed three different ways to check file size in Ubuntu. These methods are using the ‘ls’ command, the ‘du’ command, and the ‘stat’ command. All these commands are easy to use and provide accurate information about the file size.