How to find file and directory size in linux: Complete guide

  • Last updated: Nov 24, 2024
  • Views: 38
  • Author: Admin

Working with files and directories is one of the main tasks of any system administrator or regular Linux user. Knowing how to determine the size of a file and directory will help you manage disk space effectively, especially on servers or devices with limited memory. Linux has many tools and commands that allow you to quickly find out the size of individual files or entire directories.

In this article we will look at popular commands such as ls, du.

 

The first option for viewing the file size is through the ls command.

$. ls -lh test.sql

linux file size

The command displays information about the test.sql file in a human-readable format.

  • -l - shows a detailed list including permissions, owner, group, size and modification date.
  • -h - displays the file size in "readable" format (e.g. KB, MB, instead of bytes).

As a result, the console tells us that the size of the test.sql file is 100 megabytes.

 

You can add the --block-size parameter to the ls command to explicitly specify in which unit of change the file size should be shown.

$. ls -lh --block-size=K test.sql

Or

$. ls -lh --block-size=M test.sql

linux file size


 

For the following example, we will use the du command. The du command is specifically designed to view the size of files in a directory.

$. du -h test.sql

linux file size

  • -h - displays the size in "readable" format (KB, MB, GB).

As a result, the command will show how much disk space the test.sql file actually occupies.

 

In the next example we find out the size of the directory itself, for this we use the same command, but with the -s parameter.

$. du -sh /app

linux file size

  • -s - displays only the total size without a detailed list of nested files and directories.
  • -h - displays the size in "readable" format (KB, MB, GB).

As a result, the command will display the total size of the /app directory, including all nested files and directories.


 

Now you know how to determine the size of files and directories in Linux using various tools such as ls, du. These commands will help you not only analyze disk space usage, but also optimize your work with it.

By regularly checking the size of files and folders, you can quickly find large or unnecessary items and keep your disk tidy. Don't forget to combine commands with other Linux utilities to automate and improve your work efficiency.

 

SIMILAR ARTICLES