Skip to Content

4 Ways to Check File Size in Linux

Recently we got some questions from our readers about how to check file size in Linux.

  • How can I check the size of a file in Linux?
  • How can I find the total size of all files and folders in a directory in Linux?
  • How can I find out how much space a directory takes up in Linux?
  • What is the best way to check file size in Linux?

 

Key takeaway: 

To check file size in Linux, you can use du, ls, stat, or find commands.

  • The du command tells you how much space a particular file or directory is taking up on your system.
  • The ls command shows you information about files, including their size.
  • The stat command gives you more details about a particular file, including its size. 
  • The find command helps you search for files of a certain size.

 

In this article, I will explain how to check the size of a file using these four commands and their use cases. I will also cover why the file size is different between the ls command and the du command in some cases.

Linux considers everything as a file. A file doesn’t include only text files, images, and compiled programs but also includes partitions, hardware device drivers, and directories.

I will use text files as examples in this article.  If you want to check your disk size, you can check this article.

The following Linux commands can be used to check file size.

  • du -h filename: Check size of the specified file.
  • du -sh dir: Check size of the specified directory including its subdirectories.
  • ls -lh filename: Check size of the specified file.
  • ls -lh *: Check size of all the files in the current directory.
  • ls -alh *: Check size of all the files including hidden files in the current directory.
  • ls -alh dir/: Check size of all the files including hidden files in the ‘dir’ directory.

 

 

Procedure to check file size in Linux

The procedure to check file size in Linux is as follows:

  • Open the terminal application
  • Change into the directory where the file is located with cd command
  • Type du -h file name
  • Press Enter to run the command.
  • The output will display the size of this file
  • du -h option will print file size in human readable format (e.g., 1K 234M 2G)

 

Check File size with du command in Linux

The most efficient way to check file size in Linux is using du command. Open the terminal. Change into the directory where the file is located.

Type du -h file name in the prompt. The file size will be listed on the first column.  The size will be displayed in Human Readable Format. This means we can see file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

Here’s an example from Redhat that highlights why we can use du command:

The du command summarizes disk usage of each file and recursively for each directory. It offers many helpful options individually or in the correct combinations.

du -h option will print file size in human readable format (e.g., 1K 234M 2G). This is one of my favorite option for du command.

For example, if we need to check the size of file test under our home directory,  we can use following commands.

cd ~
du -h test
4.0K test

Or we can use du command followed by the full path of the file.
du -h ~/test
4.0K test

Here ~ means the home directory in Linux.  From the command output in our example, we can see that the size of file test is 4.0K.

In bash, we can use the following code to get the file size only.

filesize=$(du -h /etc/passwd | awk '{ print $1}')
echo $filesize
4.0K

If you need to get the size for all the files under the current directory, you can use this command.

du -h *

Now we know that we can use du command to get the size of file . Can we use du command to check the size of a directory in Linux? The answer is yes.

The Linux du (Disk Usage) is a standard Unix/Linux command. It can be used to check the disk usage of files and directories on Linux system.

If you need to get the size of a directory in Linux, use du -sh command. 

This will give you a total size for the directory, rather than listing the size of each individual file and subdirectory within it.

This is the most efficient way to get the size of  a directory in Linux.

[root@howtouselinux]# du -sh /home/howtouselinux
48K /home/howtouselinux
[root@howtouselinux]# du -sh /etc/
46M /etc/

To find out the disk usage summary of a /home/howtouselinux directory tree and each of its subdirectories. Enter the command as:

[root@howtouselinux]# du /home/howtouselinux
40000 /home/howtouselinux/downloads
4096 /home/howtouselinux/.mozilla/plugins
4096 /home/howtouselinux/.mozilla/extensions

[root@howtouselinux]# du -h /home/howtouselinux
40K /home/howtouselinux/downloads
4.0K /home/howtouselinux/.mozilla/plugins
4.0K /home/howtouselinux/.mozilla/extensions

Let me summarize what we have got.

Du is a command line utility for estimating file space usage. It can be used to find out not only the size of a given file, but also the size of a directory. 

For example, to check the size of the file “myfile.txt”, you would type:

du -h myfile.txt

If you want du to check the sizes of all files and subdirectories within “mydir”, you can use the “-sh” option:

du -sh mydir

du -h /etc/lvm/lvm.conf
96K /etc/lvm/lvm.conf
du -sh /etc/
46M /etc/

 

Here are some advantages to use du command to check file size in Linux.

  • The du command can show the size of a file in human-readable format, which makes it easier to interpret the results.
  • The du command can be used to check the size of multiple files at once.
  • The du command can be used to check the size of directory.
  • The du command is faster than the ls command when checking the size of large numbers of files.

 

In case you are confusing about du command and df command,  we summarized all the differences here.

What is the next:

Get File size with ls command in Linux

We can use ls command with -lh option to get the size of a file in Linux. This will give you the file size in human-readable format.

This means we can see the file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

Note: we can not use ls command to check directory size in Linux. This is one of the main difference between ls command and du command.

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen.

By default, it prints the names of all of the files and directories in the directory, but you can use various flags to display more information like file permissions, file owner etc.

  • The -l option with ls command lists the files in long format, which includes information such as the file size, owner, group, permissions, etc.
  • The -h option with ls comma lists the file size in human-readable format, which makes it easier to interpret the results.
  • The -S option with ls command sorts the files by size, from largest to smallest. This can be helpful when trying to find large files.

 

For example, if you want to get the size of the file called test.txt, you would use the following command:

ls -l test.txt

This would give you the following output:

-rw-r--r-- 1 user group 987 Apr 12 13:37 test.txt

The number 987 in the output is the size of the file in bytes.

If you want to get the size of the file in kilobytes, use the -k option with ls. For example, to get the size of test.txt in kilobytes, use the following command:

ls -lk test.txt

This would give you the following output:

-rw-r--r-- 1 user group 1008k Apr 12 13:37 test.txt

The number 1008k in the output is the size of the file in kilobytes.

You can also use the -h option with ls to get the size of the file in human-readable format. For example, to get the size of test.txt in human-readable format, use the following command:

ls -lh test.txt

This would give you the following output:

-rw-r--r-- 1 user group 987M Apr 12 13:37 test.txt

The number 987M in the output is the size of the file in human-readable format.

ls -l
total 176
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop

ls -lh
total 176K
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop

 

File Size difference between ls and du command in Linux

In Linux, there is a kind of file type called sparse file. It is a type of file that has holes in it.

These holes don’t contain actual data. A spare file only allocates disk space for the data that are actually stored.

This allows a sparse file to appear to be much larger than it actually is, while taking up very little space on the disk.

For this type of file, the file size in ls command is much larger than the file size in du command.

So you want to get the exact disk space of one file, du is the right command.

  • ls shows size of the file (when you open and read it, that’s how many bytes you will get)
  • du shows actual disk usage which can be smaller than the file size due to holes.

 

Get File size with find command in Linux

The find command is used to search and locate the list of files and directories based on conditions you specify.

We can find files by name, permissions, users, groups, file types, date, size, and other possible criteria. It will search for files in a directory and its subdirectories.

For example, to find all 50MB files, use.
find / -size 50M

To find all the files which are greater than 50MB and less than 100MB.
find / -size +50M -size -100M

Check file size with stat Command in Linux

The stat command is another command that can be used to get the size of a file in Linux. It will display system-related information file permissions, owner, group, size, last modification time and other details about files and directories.

Using stat with filename parameter will provide file size information.

$ stat file.txt
File: file.txt
Size: 4030 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 13633379 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ howtouselinux) Gid: ( 1000/ howtouselinux)
Access: 2019-11-06 09:52:17.991979701 +0100
Modify: 2019-11-06 09:52:17.971979713 +0100
Change: 2019-11-06 09:52:17.971979713 +0100
Birth: -

The -c option in the stat command can be used to get the size of a file only.

stat -c "%s" file.txt

This will give you the following output:

4030

Daniel Liex

Thursday 30th of November 2023

Totally agree.

du command is the accurate way to check file size. It can give you the disk usage.

Willy Ed

Sunday 26th of November 2023

You are the Linux expert. Thanks for the detailed info.

David Cao

Tuesday 28th of November 2023

Thanks for the comment. It means a lot to me.

Hxm

Saturday 18th of November 2023

I copied a file to a remote server and noticed that the file size has changed, but the MD5 checksum remains the same. Any idea why this happened?

Hue Xi

Tuesday 7th of November 2023

I tried to check the size of a file in Linux using the 'ls -lh' command. Normally, this command would list the file details including the file size in a human-readable format.

However, on that particular occasion, the file size returned was much larger than expected, it showed several terabytes for a text file.

I still don't know what happened.

Samuel M

Friday 3rd of November 2023

what command would you use to sort all files by the sizes within a directory?

Hue Xi

Tuesday 7th of November 2023

@Samuel M,

du -sh * |sort -h