Skip to main content
Don't use \n as delimiter as \n is perfectly valid in a file name
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than, then du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Same if there are sparse files.

if there are hard links in the directory, then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system):

find -not. ! -type d -print0 | xargs -d '\n'r0 stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one.

PS. And yes Note that for symlinks, it reports the shell commands above use GNU extensionssize of the symlink (not of the file the symlink points to).

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

PS. And yes, the shell commands above use GNU extensions.

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible, then du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Same if there are sparse files.

if there are hard links in the directory, then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system):

find . ! -type d -print0 | xargs -r0 stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one. Note that for symlinks, it reports the size of the symlink (not of the file the symlink points to).

added 65 characters in body
Source Link
anton_rh
  • 409
  • 3
  • 11

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

PS. And yes, the shell commands above use GNU extensions.

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

PS. And yes, the shell commands above use GNU extensions.

added 110 characters in body
Source Link
anton_rh
  • 409
  • 3
  • 11

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

(echo -n 0 && find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '+%s''%s\n' &&| echo)paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

(echo -n 0 && find -not -type d | xargs -d '\n' stat --printf '+%s' && echo) | bc

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.

If the file system is compressible than du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain.

if there are hard links in the directory then du may print smaller value as well because several different files in the directory refer the same data on the media.

To get the straightforward total size of all files in the directory, the following one-line shell expression can be used:

find -not -type d | xargs -d '\n' stat -c %s | paste -sd+ - | bc

or even shorter:

find . ! -type d -printf '%s\n' | paste -sd+ - | bc

(thanks Stéphane Chazelas for his comment)

It just sums sizes of all files in the directory (and its subdirectories recursively) one by one.

Source Link
anton_rh
  • 409
  • 3
  • 11
Loading