Linux CLI: 5 Ways to Get File Size in Bytes
Knowing how to check file sizes in Linux is a key skill. Knowing the size of a file aids disk space management, file transfer, and server optimization. It's easy enough to find resources on retrieving this information in gigabytes or megabytes — this guide will instead cover how to get file size in bytes using the Linux CLI.
Why get file size in bytes in Linux?
While most users prefer to see a read-out in GB or MB, such a read-out is usually shortened to two decimal points. Getting a file size in bytes allows for increased accuracy, which has several use cases:
- Managing files programmatically: Sometimes, you'll have an application that outputs a file that is the exact same size every time, which needs to be managed. In other cases, you may need to provide an API/program with the exact size to avoid rounding errors or ensure compliance with file size limits.
- File validation: File size is one way to verify that the file you downloaded matches the original and is not corrupt or malicious.
- Compression or encryption metrics: An exact file size in bytes can be very useful if you're comparing compression or encryption algorithms. The exact input/output sizes in bytes to calculate overhead, efficiency ratios, or data expansion.
- Digital forensics: Every byte counts in legal or forensic use cases. Knowing the exact file size in bytes is essential for documenting evidence, calculating hashes, and verifying file authenticity for legal or archival purposes.
Linux: Portable ways to get file size in bytes
Linux distros provide dozens of ways to get file sizes in bytes, but we'll focus on five commands that are portable (they work on any Linux distro).
1 - Using ls to show file size
ls
is the go-to command to check files for many Linux users, so it will be a natural choice for many. Finding a file's size in bytes using ls
is simple. Just run:
ls -l /path/to/yourfile
This will give you an output that looks something like this:
-rw-r--r-- 1 user user 100192 Jul 14 07:20 yourfile.txt
The value 100192
is our file's size in bytes.
But this output isn't particularly clear. What if you need only the file size in bytes as the output to feed into another script or API? With ls
, this requires some fanangling. ls
does not natively allow you to limit output to just the file size, so we have to combine it with a tool such as awk
:
ls -l /path/to/yourfile | awk '{print $5}'
We are now passing the output of ls
to awk
, which prints only the 5th field. In this case, that's the file size. Unfortunately, this may not always get you accurate results. awk
determines the field by looking for tab, whitespace, or file separators. As a result, a space or other delimiter in one of the preceding fields could throw off the output.
To avoid this situation, we can ensure that ls
returns user IDs instead of names using its long list format (-n). The final command would look something like this:
ls -l -n /path/to/yourfile | awk '{print $5}'
This is workable, but it represents a quite cumbersome solution to do something as simple as retrieving the file size. Let's look at some other options.
2 - Check file size in bytes using du
The disk usage command (du
) is another useful and universal tool for checking file sizes. It provides a handy -b
flag that outputs our file size in bytes rather than its default disk blocks:
du -b /path/to/yourfile
Output:
100192 yourfile.txt
Uh oh. You see the issue here. Du also outputs that pesky filename. Thankfully, we can limit its output more easily than ls
, as our bytes are at least in the first column.du -b /path/to/yourfile | awk '{print $1}
or du -b /path/to/yourfile | cut -f -1
should do it.
3 - Check file size in bytes with wc (word count)
The word count tool, wc
, can surprisingly be of great help here. It can be utilized on almost all platforms and gives us a clean output in bytes with no extra fluff:
wc --bytes < /path/to/yourfile
OUTPUT:
100192
There is one catch, however. Though wc
gives us a clean output, its method of obtaining that output is not ideal. It sometimes manually counts the bytes from scratch rather than performing a system call to retrieve the information, which has already been calculated. Whether it does so will depend on your distro and its version, so check this before using it in production. At scale, doing things this way is likely to degrade performance.
4 - Check file size in bytes with stat
stat
works almost perfectly for our purpose. We can use it to efficiently retrieve file size in bytes with the following command:
stat --format%s /path/to/yourfile
OUTPUT:
100192
As you can see, the --format=%s
flag outputs just the bytes. That said, it's worth noting that you may need to use a different flag on other platforms, so if you're looking for a universal, one-size-fits-all solution, this isn't it.
5 - Check file size in bytes using find
find
isn't the natural choice for retrieving file size, but it can make a lot of sense in certain scenarios. Chiefly, if you need to search for particular files and then retrieve their file size, find
lets you do it in a single command.
If you want to find the size of a file you already know, then your command would look something like this:
find /path/to/ -name "yourfile.txt" -printf "%s\n"
OUTPUT:
100192
However, this gets much more powerful when utilizing the more advanced functionality of find. For example, let's say that we know that a .log file will be generated somewhere in our /home/wordpress
folder, but its name is randomly generated. We could use:
find -O3 -L /home/wordpress/ -name "*.log" -printf "%s\n"
OUTPUT:
100192
Of course, you could get a lot more complex than this, but we'll leave explaining that to our existing find guide.
Closing words
This list is not exhaustive, but it should provide you with some idea of the options available across various Linux distributions. In the interest of remaining brief and beginner-friendly, one major omission is the use of programming languages. Many scripting languages, including Python, Perl, and Ruby, will allow you to perform basic system tasks such as retrieving file size. These may be a good option for advanced users who prioritize portability.
Each method in this guide has its strengths. stat
and wc
are friendly, find
flexible, and even ls
has its uses. The approach for you will depend on context, but these five should leave the average user well-equipped to retrieve accurate file sizes across virtually any Linux environment.
Need a Linux environment to test in? Sign up for BitLaunch and talk to our support for a free trial.