howtouselinux

4 Ways to Check File Size in Linux

Table of Contents

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?

 

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.

 

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.

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

  • ls -l filename #check size of the specified file
  • ls -l * #check size of all the files in the current directory
  • ls -al * #check size of all the files including hidden files in the current directory
  • ls -al dir/ #check size of all the files including hidden files in the ‘dir’ directory
  • du -h filename

 

 

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
  • 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.

  • du -s option will display only a total size
  • du -h option will print file size in human readable format (e.g., 1K 234M 2G)

 

Example to find the size of file in Linux:
#du -h test
4.0K test

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

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

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 directory in Linux, use du -sh command.

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

 

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

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. To use du, simply type “du” followed by the name of the file or directory you wish to check.

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

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 directory in Linux.

 

# 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.

 

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.

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

  • ls -l filename #displays Size of the specified file
  • ls -l * #displays Size of All the files in the current directory
  • ls -al * #displays Size of All the files including hidden files in the current directory
  • ls -al dir/ #displays Size of All the files including hidden files in the ‘dir’ directory

 

Online Training Courses to Learn Linux

We recommend this course to new Linux beginners.  This is one of the best online courses to learn Linux from Udemy in 2022. Created by Ziyad Hiya, this Udemy online course will teach you Linux Command-Line from scratch with a project-based approach.

In this course, you will learn what Linux commands are, and how they are structured so that you can get valuable insight into how working with the Linux command line works, rather than just memorizing command after command after command like other Linux courses will make you do.

You will learn important concepts such as:

  • The difference between the Linux terminal and the shell
  • Command Input and Output
  • Data Redirection.
  • Connecting Linux Commands together to Build Sophisticated Data Pipelines.

 

File Size difference between ls and du command in Linux

  • 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 for files that match the arguments. 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

Get Cloud server for your business website

Cloud server is a type of hosting service that uses virtual servers instead of physical servers. This means that you can get the same level of service as with a dedicated server, but without having to pay the high price tag. Cloudways is the perfect solution for your business website.

With cloud server, your website will be hosted on a cluster of servers, which are all connected to each other. This provides you with a high level of redundancy, meaning that if one server goes down, your website will still be up and running on the other servers in the cluster.

There are many reasons why you should use cloud server for your business website. First, it is a very cost-effective solution. If you were to host your website on a dedicated server, it would cost you a lot of money.

However, with cloud VPS, you can get the same level of service for a fraction of the price. Second, cloud VPS is very reliable. With so many servers in the cluster, if one server goes down, your website will still be up and running on the other servers. Finally, cloud VPS is very scalable. If you need to add more capacity to your website in the future, you can simply add more servers to the cluster.

If you’re looking for a cost-effective and reliable solution for your business website, then cloudways is the perfect solution.

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.