Меню Рубрики

Get directory size linux

How to Get the Size of a Directory in Linux

Updated Nov 13, 2019

When listing the contents of a directory using the ls command, you may have noticed that the size of the directories is almost always 4096 bytes (4 KB). That’s the size of space on the disk that is used to store the meta-information for the directory, not what it contains.

The command you’ll want to use to get the actual size of a directory is du , which is short for “disk usage”.

Getting the Size of a Directory #

The du command displays the amount of file space used by the specified files or directories. If the specified path is a directory, du summarizes disk usage of each subdirectory in that directory. If no path is specified, du reports the disk usage of the current working directory .

When invoked without any options, du displays the disk usage of the given directory and each of its subdirectories in bytes.

Typically, you would want to display the space occupied by the directory in a human-readable format. For example, to get the total size of the /var directory, you would run the following command:

The output will look something like this:

Let’s explain the command and its arguments:

  • The command starts with sudo because most of the files and directories inside the /var directory are owned by the root user and are not readable by the regular users. If you omit sudo the du command will print “du: cannot read directory”.
  • s — Display only the total size of the specified directory, do not display file size totals for subdirectories.
  • h — Print sizes in a human-readable format ( h ).
  • /var — The path to the directory you want to get the size.

What if you want to display the disk usage of the first-level subdirectories? You have two options. The first one is to use the asterisk symbol ( * ) as shown below, which means “match everything that doesn’t start with a period ( . )”. The -c option tells du to print a grand total of all sizes:

Another way to get a report about the disk usage of the first-level subdirectories is to use the —max-depth option:

By default, the du command shows the disk space used by the directory or file. To find the apparent size of a directory, use the —apparent-size option. The “apparent size” of a file is how much data is actually in the file.

When you transfer a directory via SCP , Rsync ., or SFTP the amount of data that is transferred over the network is the apparent size of the files. This is why the size of space on the disk used on the source when displayed with du (without —apparent-size ) is not the same as the size on the target.

The du command can also be combined with other commands with pipes.

For example, to print the 5 largest directories within the /var directory, you would pipe the output of du to the sort command to sort the directories by their size and then pipe the output to the head command that will print only the top 5 directories:

Conclusion #

In Linux, you can get the size of a directory using the du command.

If you have any questions or remarks, leave a comment below.

Источник

How do I get the size of a directory on the command line?

I tried to obtain the size of a directory (containing directories and sub directories) by using the ls command with option l . It seems to work for files ( ls -l file name ), but if I try to get the size of a directory (for instance, ls -l /home ), I get only 4096 bytes, although altogether it is much bigger.

16 Answers 16

Explanation

    du (disc usage) command estimates file_path space usage

The options -sh are (from man du ):

To check more than one directory and see the total, use du -sch :

Just use the du command:

will give you the cumulative disk usage of all non-hidden directories, files etc in the current directory in human-readable format.

You can use the df command to know the free space in the filesystem containing the directory:

du is your friend. If you just want to know the total size of a directory then jump into it and run:

If you also would like to know which sub-folders take up how much disk space?! You could extend this command to:

which will give you the size of all sub-folders (level 1). The output will be sorted (largest folder on top).

du can be complicated to use since you have to seemingly pass 100 arguments to get decent output. And figuring out the size of hidden folders is even tougher.

Make your life easy and use ncdu .

You get per folder summaries that are easily browsable.

Others have mentioned du , but I would also like to mention Ncdu — which is an ncurses version of du and provides interactivity: You can explore the directory hierarchy directly and see the sizes of subdirectories.

The du command shows the disk usage of the file.

The -h option shows results in human-readable form (e.g., 4k, 5M, 3G).

All of the above examples will tell you the size of the data on disk (i.e. the amount of disk space a particular file is using, which is usually larger than the actual file size). There are some situations where these will not give you an accurate report, if the data is not actually stored on this particular disk and only inode references exist.

In your example, you have used ls -l on a single file, which will have returned the file’s actual size, NOT its size on disk.

If you want to know the actual file sizes, add the -b option to du.

personally I think this is best, if you don’t want to use ncdu

This shows how much disk space you have left on the current drive and then tells you how much every file/directory takes up. e.g.,

Here is a function for your .bash_aliases

find all files under current directory recursively and sum up their size:

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible, then du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Same if there are sparse files.

if there are hard links in the directory, then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system):

It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one. Note that for symlinks, it reports the size of the symlink (not of the file the symlink points to).

Источник

3 Simple Ways to Get the Size of Directories in Linux

by Magesh Maruthamuthu · Last Updated: November 5, 2019

Two days ago I got a mail from our regular reader, who asked me how to get a summary of directories in Linux.

I know this can be achieved by the disk usage (Do) command, which I didn’t even find on my first attempt.

So I used the possible combination by digging into the Du Command Man page and finally got the best result.

Whenever a Linux administrator receives such a request, they can immediately assume that this is achieved using the du command (Disk Usage) and df (Disk FileSystem) command.

But alternatively, you can use the ncdu command or the tree command to achieve the same results.

By default the du command displays the size of the current directory files, which does not display the directory and its sub-directory size.

Read the article below to quickly summarize the size of each directory and their sub-directory.

Method-1: How to Get the Size of a Directory in Linux Using the Disk Usage (du) Command

The du command refers to disk usage. It is a standard Unix program that is used to estimate file space usage in the present working directory.

It recursively summarizes the disk usage to obtain a directory and its subdirectory size.

As I said at the beginning of the article, we are going to use the Disk Usage (Do) command with some options to achieve this. So use the following disk usage command combination to get the summary of folders and their sub-folders.

Use the below du command format to get the total size of each directory, including sub-directories.

The above command will print the size of each file and the actual size of each directory, including their subdirectory and total size.

Details of the above command:

  • du: It’s a command
  • -h: Print sizes in human readable format (e.g., 1K, 234M, 2G)
  • -c: Produce a grand total
  • /home/daygeek/Documents/: The path of directory
  • sort -rh: Sort the results with numerical value
  • head -20: Output the first 20 lines result

Use the below du command format to get the total size of a given directory.

If you want to get the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the du command format below.

Method-2: How to Get the Size of a Directory in Linux Using the ncdu (NCurses Disk Usage) Command

The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’, and provides a fast way to see what directories are using your disk space.

The ncdu command scans the given directory and displays the files and folder size recursively.

Method-3: How to Get the Size of a Directory in Linux Using the tree Command

The tree command is a recursive directory listing program that produces a depth indented listing of files and directories in a tree-like format.

The two commands above show directory summary, but the tree command will tell you every file size inside the directory and their subdirectory, and print the summary of the directory.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Mac os cdr редактировать
  • Mac os canon lbp 810
  • Mac os cannot execute binary file
  • Mac os calendar google calendar
  • Mac os brew mysql