linux command to get size of files and directories present in a particular folder? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
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?
22 Answers 22
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
note that the du command shows the disk usage of the file, wich might be larger than the actual size of the file. You can use du -d to get the actual size as ls does. More info: unix.stackexchange.com/a/106278/155224
@Jun No. Thats not metadata. ls -l generally shows 1 byte extra than the number of characters you see in the file. This extra character is generally a new line character. You can check it with od -c
Size of a directory and/or file, in a human-friendly way:
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.
@FranciscoCorralesMorales -h flag should do what you ask: print sizes in human readable format (e.g., 1K 234M 2G)
I don’t think this is right. What du does is Summarize disk usage of the set of FILEs , that is if a file is very small (i.e. 2140 bytes) the output of «du» is (in my case) 4KB because that’s the size of the cluster
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.
There is also a great ncdu utility — it can show directory size with detailed info about subfolders and files.
Installation
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
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.
ls -l will give u file size including the metadata? as it seems to be slightly 4kb bigger for one of file that i tried with
Go to the chosen directory and execute:
-d 1 is the depth of the directories -h is the human-readable option
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 .
ls -l --b=M filename | cut -d " " -f5
ls -l --b=G filename | cut -d " " -f5
I recommend replacing repeating spaces with one with sed before using cut: ls -l filename | sed -E -e ‘s/ +/ /g’ | cut -d » » -f5
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.
All you need is -l and —block-size flags
Size of all files and directories under working directory (in MBs)
Size of all files and directories under working directory (in GBs)
Size of a specific file or directory
ls -l --block-size=M my_file.txt ls -l --block-size=M my_dir/
-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 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, . (powers of 1000).
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.
$ 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
You can use below command to get list of files in easily human readable format.
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.
I do the following all the time:
$ du -sh backup-lr-May-02-2017-1493723588.tar.gz
-s, --summarize display only a total for each argument -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
To get the total size of directory or the total size of file use,
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
works perfectly to get size of a particular file.
I prefer this command ll -sha .
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.
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
How do I determine the total size of a directory (folder) from the command line?
The -h flag on sort will consider «Human Readable» size values.
If want to avoid recursively listing all files and directories, you can supply the —max-depth parameter to limit how many items are displayed. Most commonly, —max-depth=1
du -h --max-depth=1 /path/to/directory
I use du -sh or DOOSH as a way to remember it (NOTE: the command is the same, just the organization of commandline flags for memory purposes)
There is a useful option to du called the —apparent-size. It can be used to find the actual size of a file or directory (as opposed to its footprint on the disk) eg, a text file with just 4 characters will occupy about 6 bytes, but will still show up as taking up ~4K in a regular du -sh output. However, if you pass the —apparent-size option, the output will be 6. man du says: —apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (‘sparse’) files, internal fragmentation, indirect blocks
This works for OS X too! Thanks, I was really looking for a way to clear up files, both on my local machine, and my server, but automated methods seemed not to work. So, I ran du -hs * and went into the largest directory and found out which files were so large. This is such a good method, and the best part is you don’t have to install anything! Definitely deserved my upvote
@BandaMuhammadAlHelal I think there are two reasons: rounding ( du has somewhat peculiar rounding, showing no decimals if the value has more than one digit in the chosen unit), and the classical 1024 vs. 1000 prefix issue. du has an option -B (or —block-size ) to change the units in which it displays values, or you could use -b instead of -h to get the «raw» value in bytes.