Antonin GAVREL
Antonin GAVREL

Reputation: 11249

Find the number of regular files and directories in current directory and sub-directories INCLUDING current directory

Question is in the title,

Obviously I know that what I am asking in just equal to: 1 + find . -type f | wc -l

So my question is how you can combine both ?

Many thanks in advance !!

Best Regards,

Upvotes: 0

Views: 6366

Answers (1)

Vicente Olivert Riera
Vicente Olivert Riera

Reputation: 1220

Your question says "Find the number of regular files and directories", so using only -type f is not correct as it will find only regular files, not directories. You need to find both types: -type f -o -type d.

Another detail. find will also find the current directory and it will display it as the first result, so you don't need to do +1. It's already included!

Also use -prune to skip files and directories starting with . (hidden). In this case -o print is necessary. And since we don't want files or directories starting with ., we cannot use . as the search path, otherwise we will skip everything since all results will start with .. Use $PWD instead.

find $PWD \( -type f -or -type d \) -name ".*" -prune -o -print | wc -l

Note that this will exclude all files and directories included into hidden directories, even if those ones are not hidden. For instance, a file like this one will be exluded:

/my/path/.hidden_dir/not_hidden_file

Upvotes: 2

Related Questions