The number of items in a directory may be counted using
set -- *
This sets the positional parameters ($1
, $2
, etc.) to the names in the current directory.
The number of names that *
expands to is found in $#
. If you use the bash
shell and set the dotglob
shell option, this will additionally count hidden names.
Using this to find directories under /grid/sdh/hadoop/yarn/local/usercache/hdfs/appcache
that contain more than 1000 names:
find /grid/sdh/hadoop/yarn/local/usercache/hdfs/appcache \
-type d -exec bash -O dotglob -c '
for pathname do
set -- "$pathname"/*
if [ "$#" -gt 1000 ]; then
printf "%d\t%s\n" "$#" "$pathname"
fi
done' bash {} +
This expands the *
shell glob in each found directory and outputs the pathname of the directory if there are more than 1000 names in it, along with the number of names. It does this by executing a short bash
script for batches of directories. The script will loop over each batch of directories and for each, it will expand the *
glob inside it to count the number of entries. An if
statement then triggers printf
if appropriate.
Note that if a directory contains millions of names, then it may take a bit of time to actually expand the *
glob in that directory.
fsck
just to make sure that your filesystem is not corrupted? – John1024 Mar 26 '19 at 6:44ls
fails in the directory, one possible cause is a corrupted filesystem.fsck
checks for filesystem corruption. – John1024 Mar 26 '19 at 6:50