Linux view folder sizes

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 In unix/Linux, a new line is stored at the end of each line. and the file will end with a new line(an empty file is an exception to this rule). In any case od -c filename will give you exact picture of what’s in there..

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

Читайте также:  Linux scp copy folder

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

Читайте также:  Linux mint менеджер установки приложений

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

enter image description here

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 to Check Directory Size in Ubuntu Command Line

Sagar Sharma

Directory size in Ubuntu terminal

The ls command shows the file size in human-readable format with ls -lh. However, the directory size is always displayed as 4k.

I’ll explain the logic behind 4k directory size later. For now, the bigger question is how do you get the directory size in Ubuntu terminal?

Читайте также:  Linux debian файлы сервера

Like most things in Linux, there are several ways to do it. Let’s start with the ‘official’ command for checking the size of a directory.

Use the du command to check directory size in Ubuntu

The du (disk usage) command is a popular solution for checking directory sizes in Linux.

du [option] path_to_file_or_directory

For example, I will be getting details of Directory and use -h option to get output in human-readable form:

use du command to check size of directory in ubuntu

But how could a directory containing one sub-directory worth 1.6 Gb can sum around 16 gigs? Because by default, it only shows details of sub-directories.

And by using -a option, you can include files, making it a more sensible output:

use du command to get size of every file present in specified directory

Now, you can have a better idea of how it was sized around 16 gigs!

But the du command has a lot more to offer and if you want to utilize it to its max potential, I’d recommend checking out our detailed guide:

Use the tree command to check directory size in Ubuntu

You will find me using the tree command in various guides to show the file system in a hierarchy but it can also be used to get directory size.

By using —du with the tree command, it will get you the total size of a directory including the size of every file and sub-directory present inside.

use tree command to check size of directory in ubuntu

And if you’re curious why I used the -h option here, it is for getting output in the human-readable form!

Use the ncdu utility to check directory size in Ubuntu

Built on top of du (which I discussed earlier), the ncdu is an interactive and efficient way of checking disk space.

But it requires manual installation and can be done through the given command:

Now, you just have to append the directory with the ncdu command:

Use the dust utility to check directory size in Ubuntu

You can think of dust as an enhanced version of tree utility that is specifically made for checking disk space.

It has a fascinating way of presenting data with graph and shows how many percent of the total size is occupied by a single file!

But you’d need to use the cargo package manager for installation and to set up cargo in Ubuntu, you can check out our detailed guide:

And here are quick 3 commands to install and setup cargo:

sudo apt install curl curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source $HOME/.cargo/env

Once you are done with the setup, you can install dust by the given command:

Now pair the name of the directory with the dust command:

use dust utility to check directory size in ubuntu

Final Words

Through this guide, I explained multiple ways of checking directory size and the last two were my favorite ones as they went with a different approach.

Now coming back to the mystery of the 4K directory size the ls command shows. You know, a directory is simply a special file that has the details of where the files (inside the directory) are stored in the memory. The 4K is the block size of the system, and every file has this minimum block size.

And if you have any queries, make sure to leave a comment and I’ll be there with answers.

Источник

Оцените статью
Adblock
detector