How to Calculate the Total Size of Certain Files Only, Recursive, in Linux

Output file size for all files of certain type in directory recursively?

Use stat -c %n,%s to get the file name and size of the individual files. Then use awk to sum the size and print.

$ find . -name '*.pdf' -exec stat -c %n,%s {} \; | awk -F, '{sum+=$2}END{print sum}'

In fact you don't need %n, since you want only the sum:

$ find . -name '*.pdf' -exec stat -c %s {} \; | awk '{sum+=$1}END{print sum}'

Short command to find total size of files matching a wild card

Try du to summarize disk usage:

du -csh *.jpg

Output (for example):

8.0K sane-logo.jpg
16K sane-umax-advanced.jpg
28K sane-umax-histogram.jpg
24K sane-umax.jpg
16K sane-umax-standard.jpg
4.0K sane-umax-text2.jpg
4.0K sane-umax-text4.jpg
4.0K sane-umax-text.jpg
104K total

du does not summarize the size of the files but summarizes the size of the used blocks in the file system. If a file has a size of 13K and the file system uses a block size of 4K, then 16K is shown for this file.

Get total size of a list of files in UNIX

You should simply be able to pass $file_list to du:

du -ch $file_list | tail -1 | cut -f 1

du options:

  • -c display a total
  • -h human readable (i.e. 17M)

du will print an entry for each file, followed by the total (with -c), so we use tail -1 to trim to only the last line and cut -f 1 to trim that line to only the first column.

Using du for Total File Size of Certain File Types

Pass a list of files to the du command. For example:

du -s /tmp/dir1/*.txt

A possible solution to getting only the total size is to get the last line of du's output, as in:

du -c /tmp/dir1/*.txt | tail -1

calculate total used disk space by files older than 180 days using find

@PeterT is right. Almost all these answers invoke a command (du) for each file, which is very resource intensive and slow and unnecessary. The simplest and fastest way is this:

find . -type f -mtime +356 -printf '%s\n' | awk '{total=total+$1}END{print total/1024}'

Recursively counting files in a Linux directory

This should work:

find DIR_NAME -type f | wc -l

Explanation:

  • -type f to include only files.
  • | (and not ¦) redirects find command's standard output to wc command's standard input.
  • wc (short for word count) counts newlines, words and bytes on its input (docs).
  • -l to count just newlines.

Notes:

  • Replace DIR_NAME with . to execute the command in the current folder.
  • You can also remove the -type f to include directories (and symlinks) in the count.
  • It's possible this command will overcount if filenames can contain newline characters.

Explanation of why your example does not work:

In the command you showed, you do not use the "Pipe" (|) to kind-of connect two commands, but the broken bar (¦) which the shell does not recognize as a command or something similar. That's why you get that error message.

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


Related Topics



Leave a reply



Submit