Linux Size Of Directory

Linux Size Of Directory Rating: 6,8/10 537 votes

Apr 16, 2019  The size of a folder or directory in Linux can be found using the du command. Du here stands for disk usage. I’ll explain the logic behind the 4.0K size for the directories later in this tutorial. For the moment, let’s focus on getting the directory size.

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.

SDsolar
8063 gold badges9 silver badges21 bronze badges
Abdul Al HazredAbdul Al Hazred
8,03221 gold badges44 silver badges73 bronze badges

16 Answers

du -sh file_path

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:

samsam
14.9k3 gold badges16 silver badges27 bronze badges

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:

Stéphane Chazelas
324k57 gold badges627 silver badges996 bronze badges
Vineet SharmaVineet Sharma
3,3621 gold badge5 silver badges10 bronze badges

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:

Linux size of directory tree

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

ChrispieChrispie
2,1781 gold badge9 silver badges14 bronze badges

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.

Stephen Rauch
3,32810 gold badges15 silver badges29 bronze badges
Teque5Teque5

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.

TblueTblue
2,3891 gold badge6 silver badges6 bronze badges

The du command shows the disk usage of the file.

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

jackotonyejackotonye

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.

Andy FosterAndy Foster

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

ConnerManConnerMan
Get size of folder in linux

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.,

Linux Size Of Directory

mpenmpen
Eduard FlorinescuEduard Florinescu
3,74610 gold badges39 silver badges57 bronze badges

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

Leon ChangLeon Chang

Here is a function for your .bash_aliases

sample output:

for subdirs:

feldversuchfeldversuch
0x8BADF00D0x8BADF00D

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):

or even shorter:

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).

Stéphane Chazelas
324k57 gold badges627 silver badges996 bronze badges
anton_rhanton_rh

You can use

du -sh directory/

and

du -sh filename

to know the space occupied by the folder or file.

df -h

will show the disk usage in human readable format -h does that.

Linux Check Size Of Directory

There is also a gui based program called Disk Usage Analyzer.

arupgsharupgsh

Here is a POSIX script that will work with:

  • A file
  • Files
  • A directory
  • Directories
Steven PennySteven Penny

protected by KusalanandaAug 31 '17 at 11:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged command-linelsdisk-usage or ask your own question.

Linux find command

The Linux find command is a very useful and handy command to search for files from the command line. It can be used to find files based on various search criterias like permissions, user ownership, modification date/time, size etc. In this post we shall learn to use the find command along with various options that it supports.

The find command is available on most linux distros by default so you do not have to install any package. The find command is an essential one to learn, if you want to get super productive with the command line on linux.

The basic syntax of the find command looks like this

1. List all files in current and sub directories

This command lists out all the files in the current directory as well as the subdirectories in the current directory.

The command is same as the following

2. Search specific directory or path

The following command will look for files in the test directory in the current directory. Lists out all files by default.

The following command searches for files by their name.

We can also use wildcards

Linux

Note that all sub directories are searched recursively. So this is a very powerful way to find all files of a given extension.

Trying to search the '/' directory which is the root, would search the entire file system including mounted devices and network storage devices. So be careful. Of course you can press Ctrl + c anytime to stop the command.

Ignore the case

It is often useful to ignore the case when searching for file names. To ignore the case, just use the 'iname' option instead of the 'name' option.

3. Limit depth of directory traversal

The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory travesal can be specified. For example we don't want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.

The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in the current directory.

This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories and not the entire directory tree which would take more time.

Just like maxdepth there is an option called mindepth which does what the name suggests, that is, it will go atleast N level deep before searching for the files.

4. Invert match

It is also possible to search for files that do no match a given name or pattern. This is helpful when we know which files to exclude from the search.

So in the above example we found all files that do not have the extension of php, either non-php files. The find command also supports the exclamation mark inplace of not.

5. Combine multiple search criterias

It is possible to use multiple criterias when specifying name and inverting. For example

The above find command looks for files that begin with abc in their names and do not have a php extension. This is an example of how powerful search expressions can be build with the find command.

OR operator

When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. However if we need to perform an OR based matching then the find command has the 'o' switch.

The above command search for files ending in either the php extension or the txt extension.

6. Search only files or only directories

Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.

Quite useful and handy!

7. Search multiple directories together

So lets say you want to search inside 2 separate directories. Again, the command is very simple

Check, that it listed files from 2 separate directories.

8. Find hidden files

Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.

9. Find files with certain permissions

The find command can be used to find files with a specific permission using the 'perm' option. The following command searches for files with the permission 0664

This can be useful to find files with wrong permissions which can lead to security issues. Inversion can also be applied to permission checking.

10. Find files with sgid/suid bits set

The 'perm' option of find command accepts the same mode string like chmod. The following command finds all files with permission 644 and sgid bit set.

Similarly use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal numbers.

Note that the '2>/dev/null' removes those entries that have an error of 'Permission Denied'

11. Find readonly files

Find all Read Only files.

12. Find executable files

The following command will find executable files

13. Find files owned to particular user

To find all or single file called tecmint.txt under /root directory of owner root.

We could also specify the name of the file or any name related criteria along with user criteria

Its very easy to see, how we can build up criteria after criteria to narrow down our search for matching files.

14. Search files belonging to group

Find all files that belong to a particular group.

Did you know you could search your home directory by using the ~ symbol ?

Easy!!

Search file and directories based on modification date and time

Another great search criteria that the find command supports is modification and accessed date/times. This is very handy when we want to find out which files were modified as a certain time or date range. Lets take a few examples

15. Find files modified N days back

To find all the files which are modified 50 days back.

16. Find files accessed in last N days

Find all files that were accessed in the last 50 days.

17. Find files modified in a range of days

Find all files that were modified between 50 to 100 days ago.

18. Find files changed in last N minutes.

Find files modified within the last 1 hour.

19. Files modified in last hour

To find all the files which are modified in last 1 hour.

20. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.

21. Find files of given size

Search files and directories based on size. To find all 50MB files, use.

22. Find files in a size range

To find all the files which are greater than 50MB and less than 100MB.

23. Find largest and smallest files

The find command when used in combination with the ls and sort command can be used to list out the largest files.
The following command will display the 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.

Similary when sorted in ascending order, it would show the smallest files first

24. Find empty files and directories

The following command uses the 'empty' option of the find command, which finds all files that are empty.

To file all empty directories use the type 'd'.

Really very simple and easy

Some advanced operations

The find command not only finds files based on a certain criteria, it can also act upon those files using any linux command. For example, we might want to delete some files.

Here are some quick examples

25. List out the found files

Lets say we found files using find command, and now want to list them out as the ls command would have done. This is very easy.

26. Delete all matching files or directories

The following command will remove all text files in the tmp directory.

Steam prison architect. The same operating can be carried out with directories, just put type d, instead of type f.

Lets take another example where we want to delete files larger than 100MB

Summary

So that was a quick tutorial on the linux find command. The find command is one of the most essential commands on the linux terminal, that enables searching of files very easy. Its a must of all system administrators. So learn it up. Have any questions ? Leave a comment below.

Last Updated On : 17th February 2018