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:
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:
For example:
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:
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:
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:
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:
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:
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:
For example:
Get size human readable form with -h flag:
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.