The --inodes option to df
will tell you how many inodes are reserved for use. For example:
$ df --inodes / /home
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3981312 641704 3339608 17% /
/dev/sda8 30588928 332207 30256721 2% /home
$ sudo find / -xdev -print | wc -l
642070
$ sudo find /home -print | wc -l
332158
$ sudo find /home -type f -print | wc -l
284204
Notice that the number of entries returned from find
is greater than IUsed
for the root (/) filesystem, but is less for /home. But both are within 0.0005%. The reason for the discrepancies is because of hard links and similar situations.
Remember that directories, symlinks, UNIX domain sockets and named pipes are all 'files' as it relates to the filesystem. So using find -type f
flag is wildly inaccurate, from a statistical viewpoint.
With GNU du
(i.e. on non-embedded Linux or Cygwin), you can use the --exclude
option to exclude the files you don't want to match.
du -s --exclude='*.html' /var/foo
If you want to positively match *.pdf
files, you'll need to use some other method to list the files, and du
will at least display one output line per argument, plus a grand total with the option -c
. You can call tail
to remove all but the last line, or sed to remove the word “total” as well. To enumerate the files in that one directory, use wildcards in the shell.
du -sc /var/foo/*.pdf | tail -n1
du -sc /var/foo/*.pdf | sed -n '$s/\t.*//p'
If you need to traverse files in subdirectories as well, use find
, or use a **/
pattern if your shell supports that. For **/
, in bash, first run shopt -s extglob
, and note that bash versions up to 4.2 will traverse symbolic links to directories; in zsh, this works out of the box.
du -sc /var/foo/**/*.pdf | tail -n1
An added complication with the find version is that if there are too many files, find
will run du
more than once, to keep under the command line length limit. With the wildcard method, you'll get an error if that happens (“command line length limit exceeded”). The following code assumes that you don't have any matching file name containing a newline.
find /var/foo -name '*.pdf' -exec du -sc {} + |
awk '$2 == "total" {total += $1} END {print total}'
Best Answer
You can do it without the
grep
:df
accepts as argument the mount point; you can tell toawk
to print both the second line only (NR==2) , and the 2nd argument, $2.Or better yet, cut the target as you are not outputting it, and it becomes:
When I was a begginer, I also did manage to get around
cut
limitations usingtr -s " "
(squeeze) to cut redundant spaces as in: