Last Updated on
Linux users often have to deal with a slight issue: how to check different file sizes. And the same goes for all the folders and files within a directory. Well, there are more than one ways to do that, and we will share the easiest four of those many ways, so read on.
Different Methods to Check File Size in Linux
You can use four different commands to check file size in Linux. These commands are “stat,” “ls,” “du,” and “find.”
- Stat command provides more details on a particular file and its size.
- ls command provides you with info on the file along with its size.
- The du command tells you how much space a particular directory or file takes on a system.
- The find command assists you in finding files of a specific size.
To put it in simple words, Linux OS considers everything as a file. It is not specifically limited to text files, compiled programs, or images. Linux considers hardware device drivers, partitions, and directories as files.
Using the “stat” command
You can use this command to get the file size in Linux OS. This command will help you get system-related info, file permissions, size, group, owner, and the last modification time. Using the command with the filename parameter will enable you to get your file size info.
You can use -c too in this command to only the file size without any other detail.
stat -c “%s” file.txt
It will give you a simple output of 4030.
Using the “ls” command
With this command, you can use the -lh option and get the file size in Linux OS. when you use this option, you will get your file size in a human-readable format. In other words, you can see the file size in GBs, MBs, KBs, bytes, etc.
But you must remember that this command is not useful for checking the directory size in Linux OS. Nevertheless, it is one of the most basic commands in this operating system.
It will print the contents of any directory on the screen. By default, it will print the file and directory names within a guide.
However, you can use different flags to get more info on your screen. This includes file owner, filer permissions, and other details.
- The -l option with the ls command will list all files in a long format. It includes info about the owner, permissions, group, size, etc.
- Using the -h option with the ls command will list all files in a human-readable format. So, interpreting this information will be much simpler.
- With the -S option in the ls command, you can sort files according to their size, from the largest ones to the smallest ones. It comes in handy when you’re looking for those larger files.
For instance, if you want the size of a file “xyz.txt” in bytes, the following is the command you should use,
ls -l xyz.txt
The result will be,
-rw-r–r– 1 user group 987 Apr 12 13:37 xyz.txt
Here, the number 987 is the file size in bytes.
Now, if you want the file size in KBs, you must use the -k option with the ls command.
ls -lk xyz.txt
The result will be,
-rw-r–r– 1 user group 1008k Apr 12 13:37 xyz.txt
Here, the number 1008k is the file size in KBs.
Now, if you want the file size in human-readable format, you will use the -h option with the ls command.
ls -lh xyz.txt
The result will be,
-rw-r–r– 1 user group 987M Apr 12 13:37 test.txt
Here, the number 987M is the file size in a human-readable format.
- ls -l xyz # will display the file size of xyz.txt.
- ls -l * # will show the total size of all files in the directory.
- ls -al * # will communicate the total size of all files in the directory, including hidden files.
- ls -al dir/ # will display the total size of all files in the ‘dir’ directory, including hidden files.
Using the “du” command
You can use the find command for searching and locating a list of directories and files with the help of conditions that you specify. The files in the result will match the conditions you mentioned in your argument.
Therefore, you can find your files by size, name, users, permission, file types, groups, date, and other criteria. The system will look for all the files meeting the criteria within the directory and subdirectories.
For instance, if you’re looking for all 100MB files, you will have to use the following:
# find / -size 100M
If you are you are looking for all files bigger than 100MB and smaller than 200MB, the command that you will use will be:
# find / -size +100M -size -200M
Using these four commands, you can conveniently find the file size in Linux. Moreover, you can also use these commands with different options to check the directory size and other information. To simplify things, you can get the information displayed on the screen in a human-readable format.