4 Commands to Check File Size in Linux
Whether you need to manage disk space, transfer files over the network, or optimize system performance, knowing the file sizes in a Linux system plays a critical role.
In this blog post, we’ll explore four different methods you can use to determine a file's size in Linux. Let’s get started!
What is File System in Linux?
The Linux file system is the blueprint for how files are organized and stored on a Linux operating system. It defines the hierarchical structure of directories and files, as we as the rules for accessing and managing them.
At the top of the hierarchy is the root directory (/
): No other directories can be created above it. Under /
there are subdirectories like bin, etc, home, usr, and so on. These subdirectories may also contain other subdirectories themselves.
To access a file or directory on our command line, we must specify its file path or directory path. The path can be written in two ways: absolute or relative.
To learn more about this, check out this blog: Linux - Create, Delete, Copy, and Move Files and Directories.
Prerequisite
To follow along with the examples in this blog post, you'll need access to a Linux operating system.
For the purpose of this blog post, I'll be using Ubuntu as my Linux distribution of choice. Specifically, I will use KodeKloud’s Ubuntu playground, which lets you access a pre-installed Ubuntu operating system in just one click. Best of all, you won't need to go through the hassle of installing any additional software — everything you need is already set up and ready to use.
Create a File
Before we can find a file’s size, let’s create the file first. Go to the directory where you want to store the file, then run the following command:
yes 'This is a test file.' | head -c 10MB > testfile.txt
This command essentially creates a 10MB file named testfile.txt
, filled with the repeating string This is a test file.
. Here’s the detailed breakdown:
- yes 'This is a test file.': The
yes
command takes the stringThis is a test file.
as an argument and prints it to the terminal until it is forcibly stopped. - |: The pipe takes the output from the command on its left (in this case, the
yes
command) and uses it as the input for the command on its right. It's a way of "piping" the output of one command into another. - head -c 10MB: This limits the output of the
yes
command to the first 10MB of data, rather than allowing it to run indefinitely. - > testfile.txt: The ">" character is used to send the output of the `head` command to a file named `testfile.txt`. If "testfile.txt" already exists, it will be overwritten. If it doesn't exist, it will be created.
After running the above command, you’ll see an output like this:
Notice that the output says: yes: standard output: Broken pipe
. Looks like an error, right? Don’t worry! The error doesn’t interfere with the creation of our file. Our file has been created successfully. To confirm this, you can run the ls
command, and you will see the desired output as shown below:
So, what’s this "broken pipe" error message in the output all about? Let me explain.
When the head
command receives the desired amount of data (10MB) and terminates; it closes the write end of the pipe. As a result, the yes
command, which is still generating output, tries to write to a pipe that has been closed, resulting in the "Broken pipe" error message.
Looking to gain or polish your Linux file management skills? Check out our Linux Basics Course & Labs
How to Find File Size in Linux
With the test file created, let’s now learn about the four methods we can use to find its size.
1. Find File Size in Linux Using the du Command
The du
(Disk Usage) command in Linux is one of the most common tools to find the size of a file. Let's apply this command to find the size of our test file.
Run the following command:
du -sh testfile.txt
In this command, the -s
option tells the du
command to provide the summary of file usage only for the specified file. The -h
option stands for 'human-readable', and it displays the file size in a more understandable format.
After running this command, you should see an output similar to the following:
As you can see, the file size is reported as 9.6M
. Here, the M
stands for Mebibytes, not Megabytes. While both Mebibytes and Megabytes serve as units of storage, they are different in the number of bytes they represent.
A Megabyte (MB) is based on the decimal system, and it represents 10^6, or 1,000,000 bytes. A Mebibyte (MiB), on the other hand, is based on the binary system, and it represents 2^20, or 1,048,576 bytes.
To understand how 10MB is approximately 9.6MiB, let's convert the units.
1 Megabyte (MB) = 1,000,000 bytes
1 Mebibyte (MiB) = 1,048,576 bytes
So, if we have 10MB, that would be 10,000,000 bytes. To convert this to Mebibytes, we divide by the number of bytes in a MiB:
10,000,000 bytes / 1,048,576 bytes = 9.53674 MiB
9.53674 MiB is rounded up to 9.6MiB for display purposes. Hence, a 10MB file is approximately equivalent to a 9.6MiB file.
2. Find File Size in Linux Using the ls Command
The ls
(list) command in Linux is a standard command used to list files and directories. It also has options to display the file size.
Run the following command:
ls -lh testfile.txt
In this command, the -l
option tells the ls
command to use a long listing format, which includes detailed information about the files: file type, permissions, number of hard links, owner, group, size, and time of last modification. And the -h
option stands for "human-readable", and it displays the file size in a more understandable format.
Once you run this command, you should see an output that looks like this:
As highlighted above, the file size is reported as 9.6M
. Note that the M
suffix represents Mebibytes (MiB), not Megabytes (MB).
3. Find File Size in Linux Using the stat Command
The stat
command is used to display file or filesystem status. We can use it along with the -c
option and the %s
format specifier to find the size of a file.
Run the following command:
stat -c %s testfile.txt
In this command, the -c
flag allows you to specify the format that stat
will use when it displays information. The format sequences are represented by %
characters followed by another character, which determines what information will be displayed. %s
represents the total size (in bytes) of the file.
After running the above command, you’ll see the file size displayed on your terminal like this:
As you can see, the file size is displayed as 10000000 bytes, which translates to 10MB.
Note that stat
does not provide a built-in way to directly convert this size to MB or any other human-readable format.
4. Find File Size in Linux Using the wc Command
Another useful command in Linux for determining the size of a file is the wc
(word count) command. Typically, wc
is used to count the number of lines, words, and bytes or characters in a file, but with the right options, it can also be used to find the size of a file.
Run the following command:
wc -c testfile.txt
In this command, the -c
option prompts the wc
command to count the number of bytes in the specified file.
After running the command, you should see an output like this:
This output shows the size of the file in bytes. The 10000000 indicates that the testfile.txt
is composed of 10,000,000 bytes, equal to 10MB.
PRACTICE the concepts discussed here in a real-world live environment for FREE with KodeKloud Engineer. Join thousands of students gaining real, hands-on experience by working on actual project tasks on a live system.
Conclusion
In this blog post, we learned how to use four different commands - du
, ls
, stat
, and wc
- to find file size in Linux. Among the four options, the outputs of stat
and wc
aren't particularly user-friendly. They output file size in bytes, which is not easily readable or intuitive for most users, making them less suitable for our case. Therefore, it might be more practical to use either ls
or du
. These commands provide output in a more human-readable format.
TEST your Linux expertise with our FREE, fun, and challenging set of hands-on challenges.
Find all our Linux courses and certification exam preparation courses in our Linux Learning Path.