linux command to get size of files and directories present in a particular folder?

LinuxFile

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

Use ls command for files and du command for directories.

Checking File Sizes

ls -l filename   #Displays Size of the specified file
ls -l *          #Displays Size of All the files in the current directory
ls -al *         #Displays Size of All the files including hidden files in the current directory
ls -al dir/      #Displays Size of All the files including hidden files in the 'dir' directory

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

Checking Directory sizes

du -sh directory_name    #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh *                #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format

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

Solution 2 - Linux

There is du command.

Size of a directory and/or file, in a human-friendly way:

$ du -sh .bashrc /tmp

I memorised it as a non-existent English word dush.


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

Solution 3 - Linux

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.

Solution 4 - Linux

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.

Solution 5 - Linux

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

Installation

Ubuntu:

$ sudo apt-get install ncdu

Usage

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

$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
.  96,1 GiB [##########] /home
.  17,7 GiB [#         ] /usr
.   4,5 GiB [          ] /var
    1,1 GiB [          ] /lib
  732,1 MiB [          ] /opt
. 275,6 MiB [          ] /boot
  198,0 MiB [          ] /storage
. 153,5 MiB [          ] /run
.  16,6 MiB [          ] /etc
   13,5 MiB [          ] /bin
   11,3 MiB [          ] /sbin
.   8,8 MiB [          ] /tmp
.   2,2 MiB [          ] /dev
!  16,0 KiB [          ] /lost+found
    8,0 KiB [          ] /media
    8,0 KiB [          ] /snap
    4,0 KiB [          ] /lib64
e   4,0 KiB [          ] /srv
!   4,0 KiB [          ] /root
e   4,0 KiB [          ] /mnt
e   4,0 KiB [          ] /cdrom
.   0,0   B [          ] /proc
.   0,0   B [          ] /sys
@   0,0   B [          ]  initrd.img.old
@   0,0   B [          ]  initrd.img
@   0,0   B [          ]  vmlinuz.old
@   0,0   B [          ]  vmlinuz

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

Solution 6 - Linux

File Size in MB

ls -l --b=M  filename | cut -d " " -f5
        

File Size in GB

ls -l --b=G  filename | cut -d " " -f5

Solution 7 - Linux

Go to the chosen directory and execute:

$ du -d 1 -h

where:

-d 1 is the depth of the directories

-h is the human-readable option

You'll see like that:

0	./proc
8.5M	./run
0	./sys
56M	./etc
12G	./root
33G	./var
23M	./tmp
3.2G	./usr
154M	./boot
26G	./home
0	./media
0	./mnt
421M	./opt
0	./srv
2.6G	./backups
80G	.

Solution 8 - Linux

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.

Solution 9 - Linux

All you need is -l and --block-size flags

Size of all files and directories under working directory (in MBs)

ls -l --block-size=M

Size of all files and directories under working directory (in GBs)

ls -l --block-size=G

Size of a specific file or directory

ls -l --block-size=M my_file.txt
ls -l --block-size=M my_dir/

> ls --help

> -l use a long listing format > > > > --block-size=SIZE: scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below > > > SIZE is an integer and optional unit (example: 10M is 1010241024). > Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... > (powers of 1000).


Solution 10 - Linux

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

$ date | tee /tmp/foo
Wed Mar 13 05:36:31 UTC 2019

$ stat -c %s /tmp/foo
29

$ ls -l /tmp/foo
-rw-r--r--  1 bruno  wheel  29 Mar 13 05:36 /tmp/foo

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

The OSX/BSD equivalent is:

$ date | tee /tmp/foo
Wed Mar 13 00:54:16 EDT 2019

$ stat -f %z /tmp/foo
29

$ ls -l /tmp/foo
-rw-r--r--  1 bruno  wheel  29 Mar 13 00:54 /tmp/foo

Solution 11 - Linux

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

> ls -lrtsh

Solution 12 - Linux

I do the following all the time:

$ du -sh backup-lr-May-02-2017-1493723588.tar.gz

NB:

-s, --summarize
            display only a total for each argument
-h, --human-readable
            print sizes in human readable format (e.g., 1K 234M 2G)

Solution 13 - Linux

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

Exemple: 
    [root@CIEYY1Z3 etc]# ls -lh
    total 1.4M
    -rw-r--r--.  1 root   root      44M Sep 15  2015 adjtime
    -rw-r--r--.  1 root   root     1.5K Jun  7  2013 aliases
    -rw-r--r--   1 root   root      12K Nov 25  2015 aliases.db
    drwxr-xr-x.  2 root   root     4.0K Jan 11  2018 alternatives
    -rw-------.  1 root   root      541 Jul  8  2014 anacrontab
    -rw-r--r--.  1 root   root      55M Sep 16  2014 asound.conf
    -rw-r--r--.  1 root   root       1G Oct  6  2014 at.deny

Solution 14 - Linux

> du -sh [file_name]

works perfectly to get size of a particular file.

Solution 15 - Linux

You can use ncdu disk usage analyzer here. It displays the size of the files and directories in an ncurses interface. You can navigate to each directory and see the files sizes from the same interface.

To install

$ sudo apt-get install ncdu

To analyze

$ ncdu <directory>

enter image description here

Solution 16 - Linux

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

du -csh <directory or filename*> | grep total

Solution 17 - Linux

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:

user@user-XPS-15-9560:/$ ll
total 188
drwxr-xr-x  27 root root  4096 Jan 26 09:13 ./
drwxr-xr-x  27 root root  4096 Jan 26 09:13 ../
drwxr-xr-x   2 root root  4096 Jan 22 15:13 bin/
drwxr-xr-x   4 root root 12288 Jan 29 11:35 boot/
drwxr-xr-x   2 root root  4096 Sep  3 18:14 cdrom/
drwxr-xr-x  20 root root  4440 Feb  5 08:43 dev/
drwxr-xr-x 153 root root 12288 Feb  2 15:17 etc/
drwxr-xr-x   4 root root  4096 Sep  3 18:15 home/
...

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:

sudo nano ~/.bashrc
...add line described above and save file by pressing Ctrl+X and Y...
source ~/.bashrc

Solution 18 - Linux

ls -sh video.mp4 | sed s/video.mp4//g

output, 5.6M

Solution 19 - Linux

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.

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.

ls -ls dir

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

Example: usage of a 1 byte file

$ echo "" > file.c
 
$ ls -l file.c
-rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c
 
$ ls -ls file.c
4 -rw-r--r-- 1 nacho nacho 1 Apr 30 20:42 file.c
 
$ du file.c
4 file.c
 
$ dutree file.c
[ file.c 1 B ]
 
$ dutree -u file.c
[ file.c 4.00 KiB ]
 
$ stat file.c
 File: file.c
 Size: 1 Blocks: 8 IO Block: 4096 regular file
Device: 2fh/47d Inode: 2185244 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ nacho) Gid: ( 1000/ nacho)
Access: 2018-04-30 20:41:58.002124411 +0200
Modify: 2018-04-30 20:42:24.835458383 +0200
Change: 2018-04-30 20:42:24.835458383 +0200
 Birth: -

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

Solution 20 - Linux

I prefer this command ll -sha.

Solution 21 - Linux

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

Solution 22 - Linux

go to specific directory then run below command

# du -sh * 

4.0K    1
4.0K    anadb.sh --> Shell file
4.0K    db.sh/    --> shell file
24K     backup4/  --> Directory
8.0K    backup6/  --> Directory 
1.9G    backup.sql.gz  --> sql file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdvaitView Question on Stackoverflow
Solution 1 - LinuxSandeepView Answer on Stackoverflow
Solution 2 - LinuxMaxim EgorushkinView Answer on Stackoverflow
Solution 3 - LinuxThorView Answer on Stackoverflow
Solution 4 - LinuxYenier TorresView Answer on Stackoverflow
Solution 5 - LinuxavtomatonView Answer on Stackoverflow
Solution 6 - LinuxRavichandraView Answer on Stackoverflow
Solution 7 - LinuxClaudio GuirunasView Answer on Stackoverflow
Solution 8 - LinuxAdiView Answer on Stackoverflow
Solution 9 - LinuxGiorgos MyrianthousView Answer on Stackoverflow
Solution 10 - LinuxBruno BronoskyView Answer on Stackoverflow
Solution 11 - LinuxAmitView Answer on Stackoverflow
Solution 12 - LinuxFokwa BestView Answer on Stackoverflow
Solution 13 - LinuxmkebriView Answer on Stackoverflow
Solution 14 - LinuxEyshikaView Answer on Stackoverflow
Solution 15 - LinuxsumedheView Answer on Stackoverflow
Solution 16 - Linuxsuhail ahamed FView Answer on Stackoverflow
Solution 17 - LinuxMatteusView Answer on Stackoverflow
Solution 18 - LinuxPhilipView Answer on Stackoverflow
Solution 19 - LinuxnachoparkerView Answer on Stackoverflow
Solution 20 - Linuxdarth vaderView Answer on Stackoverflow
Solution 21 - LinuxPrem SView Answer on Stackoverflow
Solution 22 - Linuxsachin_urView Answer on Stackoverflow