• TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Linux Size Of Directory

18.08.2019 
Linux Size Of Directory 8,7/10 2629 reviews

I recently had to delete and recreate an NFS directory to shrink its size after it had previously bloated. In the process, I found a fairly efficient way to do this using rsync and hard links. This method avoids copying the actual files, while still accomplishing the goal of recreating the directory with the same contents. Modern block size is 4KiB, so files will use disk space multiple of 4KiB, regardless of how small they are. If you use the command stat you can see both figures side by side. Stat file.c If you want a more compact view for a directory, you can use ls -ls, which will give you usage in 1KiB units.

How can I see the size of files and directories in Linux? If use df -m, then it shows the size of all the directory at the top level, but, for the directories and files inside the directory, how do I check the size?

nbro
6,07510 gold badges54 silver badges104 bronze badges
AdvaitAdvait
1,2802 gold badges11 silver badges16 bronze badges

17 Answers

It's simple. Use ls command for files and du command for directories.

Checking File Sizes

ls command will not list the actual size of directories(why?). Therefore, we use du for this purpose.

Checking Directory sizes

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, ..)

For more information see man ls and man du

mk.mk.
9,24311 gold badges49 silver badges80 bronze badges

There is du command.

Size of a directory and/or file:

--apparent-size command line switch makes it measure apparent sizes (what ls shows) rather than actual disk usage.

Maxim EgorushkinMaxim Egorushkin
94.1k11 gold badges107 silver badges198 bronze badges

Use ls -s to list file size, or if you prefer ls -sh for human readable sizes.

For directories use du, and again, du -h for human readable sizes.

ThorThor
32.1k7 gold badges85 silver badges99 bronze badges

You can use:

ls -lh

Using this command you'll see the apparent space of the directory and true space of the files and in details the names of the files displayed, besides the size and creation date of each.

Yenier TorresYenier Torres
Rishabh Agrahari
1,3181 gold badge14 silver badges18 bronze badges
RavichandraRavichandra
8892 gold badges15 silver badges25 bronze badges

There is also a great ncdu utility - it can show directory size with detailed info about subfolders and files.

Installation

Ubuntu:

Usage

Just type ncdu [path] in the command line. After a few seconds for analyzing the path, you will see something like this:

Delete the currently highlighted element with d, exit with CTRL + c

Martin Thoma
48.3k65 gold badges335 silver badges567 bronze badges
avtomatonavtomaton

ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.

If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.

This is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

Note that the --block-size parameter is a GNU extension on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support --block-size as described above.

AdiAdi
Fokwa BestFokwa Best
1,2103 gold badges17 silver badges36 bronze badges

You can use below command to get list of files in easily human readable format.

ls -lrtsh

AmitAmit
Linux size of directory with subdirectories

To get the total size of directory or the total size of file use,

Suraj Rao
24.9k8 gold badges63 silver badges74 bronze badges
suhail ahamed Fsuhail ahamed F

If you are using it in a script, use stat.

That will give you size in bytes. See man stat for more output format options.

The OSX/BSD equivalent is:

Bruno BronoskyBruno Bronosky
38.3k5 gold badges91 silver badges91 bronze badges

You have to differenciate between file size and disk usage. The main difference between the two comes from the fact that files are 'cut into pieces' and stored in blocks.

Modern block size is 4KiB, so files will use disk space multiple of 4KiB, regardless of how small they are.

If you use the command stat you can see both figures side by side.

If you want a more compact view for a directory, you can use ls -ls, which will give you usage in 1KiB units.

Also du will give you real disk usage, in 1KiB units, or dutree with the -u flag.

Example: usage of a 1 byte file

Linux Size Of Directory

In addition, in modern filesystems we can have snapshots, sparse files (files with holes in them) that further complicate the situation.

You can see more details in this article: understanding file size in Linux

nachoparkernachoparker

I'm a Ubuntu 16.04 user myself and I find that the ll command is by far the easiest way to see a directory's contents. I've noticed that not all Linux distributions support this command, but there's probably a workaround/install for each distro out there.

Example:

The biggest advantage for me is that it's quick and really intuitive to use.

UPDATE: what I didn't know was that on Ubuntu it's a pre-configured alias. You can easily set it yourself by executing alias ll='ls -la' on the command line, or by adding this entry in your .bashrc config file:

MatteusMatteus

Use ls command with -h argument: [root@hots19 etc]# ls -lh
h : for human readable.

mkebrimkebri

Go to the chosen directory and execute:

where:

You'll see like that:

Claudio GuirunasClaudio Guirunas

you can use ls -sh in linux you can do sort alsoyou need to go to dir where you want to check the size of files

Prem SPrem S
sachin_ursachin_ur

Not the answer you're looking for? Browse other questions tagged linuxfile or ask your own question.

This question already has an answer here:

  • Display each sub-directory size in a list format using one line command in Bash? 4 answers

I have a website on my server and I own it.

The size of the website is around a whopping 170GB. This shouldn't be this much. At most, it should be around 20GB. I don't know what makes it that big.

I am trying to figure out which directory has such a large size, but I am not able too.

I checked the DB size and it is around 500 MB.
Please help me to find out the main directory responsible for this.

Linux Size Of Directory Contents

SDsolar
9661 gold badge10 silver badges25 bronze badges
YogusYogus
1,4862 gold badges10 silver badges10 bronze badges

marked as duplicate by Hennes, slhckAug 14 '13 at 7:53

Get Size Of Directory Linux

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Under any linux system, you want to use the command du. (Disk Usage)

Sniper fury controls windows 10. Common usage is : du -sh file(s) name(s) or du -sh /path/to/dir/*

-s stand for 'summary' which will give you the size of each argument instead of detailing th size of each elements of the file tree underneath.

Replace 'h' by 'k','m' or 'g' for Kilobytes, Megabytes and Gigabytes instead of human-readable. With k/m/g switches, you can then pipe the output to sort -n (numeric sort instead of lexicographic) to get the sorted by size list of files in a directory. sort even has a -h filter on latest versions.

If you still have a big difference, you may want to try the --apparent-size switch to du which will allow you to diagnose sparse files. (files with empty space inside, to be simple)

For more information, see the manual page of du

mveroonemveroone

Not the answer you're looking for? Browse other questions tagged centosdu or ask your own question.