howtouselinux

4 Ways to Check File Size in Linux

Table of Contents

 

How to check File Size in Linux is a commonly asked question during a Linux job interview.

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 -sh filename

 

Get File size with ls command in Linux

ls -l (-l is a character, not one) shows file or directory, size, modified date and time, file or folder name and owner of the file, and its permission. With a combination of -lh option, shows sizes in a 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

 

Find File size with Du command in Linux

The best Linux command to check file size is using du command. What we need is to open the terminal and type du -sh 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 sizes in Bytes, Kilobytes, Megabytes, Gigabytes, etc.

The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine.

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

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
40 /home/howtouselinux/downloads
4 /home/howtouselinux/.mozilla/plugins
4 /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

File Size difference between ls and du 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 for files that match the arguments.
find command can be used in a variety of conditions like we can find files by permissions, users, groups, file types, date, size, and other possible criteria.

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

Stat command is used to get file system-related information about files and directories. Using stat with filename parameter will provide size information in byte mode too.

$ 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: –

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press