As a software developer or system administrator, it is essential to know how to check the size of files and directories on your Linux system. This knowledge helps you manage your system’s storage space and optimize your workflows. Fortunately, Linux provides several commands that you can use to check the size of files and directories. In this article, we will explore these commands and how to use them effectively.
Checking File Size in Linux
To check the size of a file in Linux, you can use the ls
command with the -lh
or -ls
option. The -lh
option displays the file size in a human-readable format, while the -ls
option shows the file size in bytes. Here is an example of how to use the ls
command to check the size of a file:
ls -lh filename
The output of this command will display the size of the file in a human-readable format, such as KB, MB, or GB, depending on the file size.
If you want to display the size of all files in a directory, you can use the ls
command with the -lh
or -ls
option and specify the directory’s path. Here is an example:
ls -lh /path/to/directory
This command will display the size of all files in the specified directory, along with their file names and permissions.
Another useful command for checking file size in Linux is the du
command. The du
command stands for “disk usage” and shows the disk space used by files and directories. By default, the du
command displays the size of all files and directories in the current directory. However, you can specify a file or directory path to display the size of a specific file or directory. Here is an example:
du -sh filename
This command will display the size of the file in a human-readable format, similar to the ls
command with the -lh
option.
Checking Directory Size in Linux
To check the size of a directory in Linux, you can use the du
command with the -sh
option. The -s
option shows the total size of the directory, while the -h
option displays the size in a human-readable format. Here is an example of how to use the du
command to check the size of a directory:
du -sh /path/to/directory
This command will display the total size of the directory in a human-readable format.
If you want to display the size of each file and subdirectory within a directory, you can use the du
command with the -h
option and the -a
option. The -a
option shows the size of each file and directory within the specified directory. Here is an example:
du -ha /path/to/directory
This command will display the size of each file and subdirectory within the specified directory, along with their file names and permissions.
Using Wildcards with the ls and du Commands
When checking the size of multiple files or directories, it can be tedious to type out each file or directory path individually. Luckily, you can use wildcards with the ls
and du
commands to match multiple files or directories with a single command.
For example, you can use the *
wildcard to match all files or directories with a particular extension. Here is an example of how to use the ls
command with the *
wildcard:
ls -lh /path/to/*.txt
This command will display the size of all files with a .txt
extension in the specified directory.
Similarly, you can use the *
wildcard with the du
command to match multiple directories with a single command. Here is an example:
du -sh /path/to/*/directory
This command will display the total size of all directories named directory
within the specified parent directory.
Checking File and Directory Size in GUI Applications
If you prefer to use a graphical user interface (GUI) to check file and directory size, many Linux file managers provide this functionality. For example, the Nautilus file manager in Ubuntu and other GNOME-based Linux distributions displays the size of files and directories in the file list view. To display the size of a file or directory, select the file or directory in the file manager, and its size will be displayed in the status bar at the bottom of the window.
Similarly, the KDE-based Dolphin file manager provides a size column in the file list view, which displays the size of files and directories. To enable this column, right-click the column header in the file list view and select “Size” from the drop-down menu.
While using a GUI application can be more convenient for some users, it’s important to note that the command-line methods outlined in this article provide more flexibility and can be automated using scripts and other tools.
This article provides a comprehensive guide on how to check file and directory size in Linux. It covers several command-line methods, including the **ls
**and **du
**commands, as well as how to use wildcards and GUI applications to check file and directory size. This article is aimed at software developers and system administrators who want to effectively manage their system’s storage space and optimize their workflows.
Conclusion
In summary, Linux provides several commands that you can use to check the size of files and directories. The ls
command is helpful for quickly checking the size of a file or all files in a directory, while the du
command helps show the disk space used by files and directories. You can effectively manage your system’s storage space and optimize your workflows using these commands.
📕 Related articles about Linux
- How to Remove a Directory in Linux
- How to Manage Passphrase of an SSH Key: Best Practices and Tips
- Understanding GSSAPI Authentication in SSH
- How to speed up SSH authentication
- How to Use SSH Service
- How to Get CPU Speed on Linux