gsamaras gsamaras - 7 months ago 33
Linux Question

Find number of directories that start with a number

Following Number of subdirectories in a directory?, I am now trying to take into account only the directories that start with a number.

For example, if I do:

hadoop fs -ls -R foo_dir | grep "^d" | wc -l



foo_dir/98740802-Whitby Abbey.dat.bz2


I will get 3 as result. How to filter that directory to get 2 (idea: check if the first letter of the item is 1-9)?


I would try something like this:

... ls -R foo_dir | grep "/[0-9][^/]*$" -c

Regex Explanation:

/ is the directory separator on linux systems.
[0-9] matches any digit.
[^/]* matches zero or more characters that are not directory separators.
$ matches the end of a line.

Hence, we match any directory or file that starts with a digit and is the last entry in the filepath.

PS: The -c arguement to grep causes grep to print the number of matching lines.