Using Ls to List Directories and Their Total Sizes

Using ls to list directories and their total sizes

Try something like:

du -sh *

short version of:

du --summarize --human-readable *

Explanation:

du: Disk Usage

-s: Display a summary for each specified file. (Equivalent to -d 0)

-h: "Human-readable" output. Use unit suffixes: Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB) and Pebibyte (PiB). (BASE2)

How to list the files and folders in a directory with its total size in Linux?

You can use "du" command to achieve that.
Go to the right directory and type

du -sh *

It will list all files and directories in the current directory like

123G data-1
115G data-2
12K test.txt
14K readme.txt

List all directories sorted by size in descending order

I prefer to just go straight to comparing bytes.

du -b * | sort -nr

du -b reports bytes.

sort -n sorts numerically. Obviously, -r reverses.

My /tmp before I clean it -

104857600       wbxtra_RESIDENT_07202018_075931.wbt
815372 wbxtra_RESIDENT_07192018_075744.wbt
215310 Slack Crashes
148028 wbxtra_RESIDENT_07182018_162525.wbt
144496 wbxtra_RESIDENT_07182018_163507.wbt
141688 wbxtra_RESIDENT_07182018_161957.wbt
56617 Notification Cache
20480 ~DFFA6E4895E749B423.TMP
16384 ~DF543949D7B4DF074A.TMP
13254 AdobeARM.log
3614 PhishMeOutlookReporterLoader.log
3448 msohtmlclip1/01
3448 msohtmlclip1
512 ~DF92FFF2C02995D884.TMP
28 ExchangePerflog_8484fa311d504d0fdcd6c672.dat
0 WPDNSE
0 VPMECTMP
0 VBE

How to list directory size of all child directories?

The simplest is:

du -h --max-depth=1 parent

This will show all sizes of the children of parent If you also want the grandchildren, you can do

du -h --max-depth=2 parent

If you want the whole family

du -h parent

All these will just summarize the total directory size of each subdirectory up to a given level (except the last, it will give for all)

If you don't want the content of the subdirectories, add the -S flag.

How to get the summarized sizes of directories and their subdirectories?

This does what you're looking for:

du -sh /*

What this means:

  • -s to give only the total for each command line argument.
  • -h for human-readable suffixes like M for megabytes and G for gigabytes (optional).
  • /* simply expands to all directories (and files) in /.

    Note: dotfiles are not included; run shopt -s dotglob to include those too.

Also useful is sorting by size:

du -sh /* | sort -h

Here:

  • -h ensures that sort interprets the human-readable suffixes correctly.

linux show size of folder contents in ls or some other command

du --max-depth=1 -h should show how much space the folders use

How to list the size of each file and directory and sort by descending size in Bash?

Simply navigate to directory and run following command:

du -a --max-depth=1 | sort -n

OR add -h for human readable sizes and -r to print bigger directories/files first.

du -a -h --max-depth=1 | sort -hr

List files over a specific size in current directory and all subdirectories

find . -size +10k -exec ls -lh {} \+

the first part of this is identical to @sputnicks answer, and sucesffully finds all files in the directory over 10k (don't confuse k with K), my addition, the second part then executes ls -lh or ls that lists(-l) the files by human readable size(-h). negate the h if you prefer. of course the {} is the file itself, and the \+ is simply an alternative to \;

which in practice \; would repeat or:

ls -l found.file; ls -l found.file.2; ls -l found.file.3

where \+ display it as one statement or:

ls -l found.file found.file.2 found.file.3

more on \; vs + with find

Additionaly, you may want the listing ordered by size. Which is relatively easy to accomplish. I would at the -s option to ls, so ls -ls and then pipe it to sort -n to sort numerically

which would become:

find . -size +10k -exec ls -ls {} \+ | sort -n

or in reverse order add an -r :

find . -size +10k -exec ls -ls {} \+ | sort -nr

finally, your title says find biggest file in directory. You can do that by then piping the code to tail

find . -size +10k -exec ls -ls {} \+ | sort -n | tail -1
would find you the largest file in the directory and its sub directories.

note you could also sort files by size by using -S, and negate the need for sort. but to find the largest file you would need to use head so

find . -size +10k -exec ls -lS {} \+ | head -1

the benefit of doing it with -S and not sort is one, you don't have to type sort -n and two you can also use -h the human readable size option. which is one of my favorite to use, but is not available with older versisions of ls, for example we have an old centOs 4 server at work that doesn't have -h

Calculate the total size of all files from a generated folders list with full PATH

Do not use backticks `. Use $(..) instead.

Do not use:

command $(cat something)

this is a common anti-pattern. It works for simple cases, fails for many more, because the result of $(...) undergoes word splitting and filename expansion.

Check your scripts with http://shellcheck.net

If you want to "run a command with argument from a file" use xargs or write a loop. Read https://mywiki.wooledge.org/BashFAQ/001 . Also xargs will handle too many arguments by itself. And I would also add -s to du. Try:

xargs -d'\n' du -sch < file_list.txt | tail -1 | cut -f 1

test on repl bash



Related Topics



Leave a reply



Submit