Count number of files in a directory:
$ ls -1 targetdir | wc -l
The above method will count symbolic links as well as subdirectories in targetdir (but not recursively into subdirectories).
If you want to exclude subdirectories, you need a heavier duty tool than ls.
$ find targetdir -type f -maxdepth 1 | wc -l
type f ensures that the find command only returns regular files for counting (no subdirectories). By default, the find command traverses into subdirectories for searching. -maxdepth 1 prevents find from traversing into subdirectories. If you do want to count files in the subdirectories, just remove -maxdepth 1 from the command line. Note that the find command does NOT classify a symbolic link as a regular file. Therefore, the above find -type f command does not return symbolic links. As a result, the final count excludes all symbolic links. To include symbolic links, add the -follow option to find.
$ find targetdir -type f -follow -maxdepth 1 | wc -l
No comments:
Post a Comment