Linux Commands

Linux Command to Get Size of Files and Directories Present in a Particular Folder

Linux offers a wide range of command-line tools for managing files and directories. One such tool is the “du” (disk usage) command and the other one is “ls” (list) command, both of which can be used to get the size of files and directories present in a particular folder.

Linux Command to Get Size of Files and Directories Present in a Particular Folder

There are two commands to get the size of files and directories present in a particular folder:

ls: ls or list command give actual file size. The ls command lists the files and directories. It can display additional information, such as file permissions, timestamps, and sizes.

du: The du command can estimate the size and space used by a file or directory. It can display output in a specific format, such as human-readable sizes. By default, it displays file sizes in a hierarchical format.

1: How to Get Size of Files and Directories Using ls Command

The “ls” command is a commonly used command in Linux for listing the contents of a directory. However, it can give us files and directories actual size in a particular folder.

The general syntax to get the size of files and directories using ls command is mentioned below:

$ ls -l <filename>

How to Get Size of Files and Directories in a Particular Directory Using ls Command

To grab the size of files and directories in a particular folder use:

$ ls -l ./<directory-name>

For example:

$ ls -l ./Downloads

This will output a list of all files and directories in the defined directory, along with their sizes in bytes.

How to Get Size of All Files Using ls Command

To display the size of all the files in a present working directory asterisk (*) wildcard character will be used:

$ ls -l *

How to Get Size of All Files Including Hidden Files using ls Command

If you want to get file size of hidden files also -a flag will be used:

$ ls -al *

The name of hidden files in Linux usually starts with a dot, for example “.snap”. The -a flag contains directories that start with dot (“.”).

2: How to Get Size of Files and Directories using du Command

Another way of finding the size of directories is using du command. Here are few reasons why du command is better than others:

  • Speed: The “du” command is very fast in providing the size of files and directories, it performs the operation in a short amount of time, making it ideal for large directories or file systems.
  • Flexibility: The “du” command provides a wide range of options that can be used to customize the output, such as displaying sizes in human-readable format and providing a summary of the total size of a directory.
  • Recursive: The “du” command can recursively scan through a directory and its subdirectories, and will provide the size of all files and directories present in it.
  • Accurate: The “du” command provides an accurate representation of the size of a file or directory, it considers the block size of the file system and the space used by the file and directory metadata, which gives an accurate representation of the space used by the files and directories.

Other commands like “ls” and “stat” are also used to get file size information, but they do not provide the same level of flexibility and accuracy as the “du” command.

The syntax for du command to follow is mentioned below:

$ du ~/<directory-file-name>

Where “directory” is the name of the directory that you want to check, and “file-name” are any additional options that you want to include.

To get the size of the Downloads directory use:

$ du ~/Downloads

This will output a list of all files and directories in the home directory, along with their sizes in kilobytes.

To get the size of a particular directory in human readable form such as in bytes, kilobytes, or megabytes use:

$ du -h ~/Downloads

This will output the sizes in a format that is easier to read, such as “5.5M” for 5.5 megabytes.

Similarly, to find the size of a file with du utility, use:

$ du /<file-name>

For example:

$ du ./file.mp4

Get size human readable form with -h flag:

$ du -h ./file.mp4

Conclusion

To find file size in Linux the “ls” and “du” commands can be used. Both these commands help to easily get the size of files and directories present in a particular folder and make better decisions on how to manage the space on your Linux machine.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.