2
du -ch -- **/*.jpg | grep total

Especially, what do the -- (double dash) and ** (double asterisk) really mean?

Using the Z shell

Kusalananda
  • 333,661

2 Answers2

5

The ** in zsh matches just like *, but allows for matching across / in pathnames. The pattern **/*.jpg will therefore expand to the pathname of any file that has a filename suffix of .jpg anywhere in or below the current directory.

The ** pattern is available in bash as well, if enabled with shopt -s globstar. The ksh93 shell has it too, if enabled with set -o globstar.

The -- prevents any pathname (matching the above pattern) that starts with a dash from being interpreted by du as a command line option. The -- stops the command line parsing of du from looking for further options. This is not dependent on the shell but is a POSIX "utility guideline" for standard utilities.

The -- could be removed if the filename globbing pattern was changed to ./**/*.jpg.

The command would give you the total size of all *.jpg files in or below the current directory by extracting the line with the total from the output of du (run the command without | grep total to see what du produces).

Kusalananda
  • 333,661
0

From man-page of du usage

du [OPTION][PARAM]

du - lists disk space used by files

c - displays total 
h - human readable format (24M= meaning 24 MB)

-- - usually means end of option parameters
**/*.jpg- glob to go find all paths matching this path (foo/bar.jpg)
| -pipe

 grep total- this option is not required and is redundant, since -c[OPTION] is giving you grand total anyways.

TL;DR: This lists the total disk size of pictures in .jpg format from one step inside directory from current directory.

Eg- If your current Directory is ~/Pictures then running this command will list file size of all .jpg files inside recursively.(~/Pictures/EuropeTrip/pic134.jpg) [*] means all for regex.

Not sure what (grep total) is doing/ or adding to the output, and maybe unnecessary here.

  • **/*.jpg is not a regex, it is a glob. – ctrl-alt-delor Jul 20 '18 at 10:07
  • yes a glob , removed by apppending . at start, easier to visualise as regex – Pavan Kate Jul 20 '18 at 10:09
  • Sorry but I do not understand the phrase “removed by apppending . at start, easier to visualise as regex”. – ctrl-alt-delor Jul 20 '18 at 10:29
  • You are correct @ctrl-alt-delor, It is a glob, although filename glob and regex are completely different altogether, the output is same - All jpg files under current directory.See Kusalananda's answer mentioning about this. – Pavan Kate Jul 20 '18 at 10:31
  • While the output may be the same, as that produced by a regular expression, that produces the same output. The regular expression is different. .*/.*\.jpg (regex) vs **/*.jpg (glob). – ctrl-alt-delor Jul 20 '18 at 16:58