Sometime you copy a lot of files from a directory or another media to some other directory and you wish to know how many files are copied. Here is how you can get to know how many files you have in your directory.
Note : If you use ls -l instead of ls -1 as in ls -l | wc -l instead of ls -1 | wc -l. You will get count 1 more than the the actual number of files as this will also print one more line like
total 116
drwxr-xr-x 3 aniket aniket 4096 May 31 22:08 Desktop
drwxr-xr-x 2 aniket aniket 4096 May 1 20:25 Downloads
...
Total line tells you the total size of your directory in blocks (1024 bytes) on the hard disk, which is 116 in our example.
Screen shot:
he above method will count symbolic links as well as subdirectories in targetdir (but not recursively into subdirectories).
If you want to exclude subdirectories, you can do.
-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.
- Navigate to the directory you wish to count files from.
- Type in the following command ls -1 | wc -l
- You must get the file count as the output
ls command
ls command is used to list contents of the directory. You can see the manual of the command by typing man ls in your terminal. Frequently used options with ls command are
- -a, --all
do not ignore entries starting with . - -d, --directory
list directory entries instead of contents, and do not dereference symbolic links - -l use a long listing format
- -1 list one file per line
wc command
wc command is used for word count.
You can see the manual of the command by typing man wc in your terminal. Frequently used options with ls command are
You can see the manual of the command by typing man wc in your terminal. Frequently used options with ls command are
- -l, --lines
print the newline counts - -w, --words
print the word count
Understanding ls -1 | wc -l
We have used ls -1 in the command to find number of files in a directory. This will return the directory contents listed one file per line. This we pipe it to wc -l command which will print the newline counts.Note : If you use ls -l instead of ls -1 as in ls -l | wc -l instead of ls -1 | wc -l. You will get count 1 more than the the actual number of files as this will also print one more line like
total 116
drwxr-xr-x 3 aniket aniket 4096 May 31 22:08 Desktop
drwxr-xr-x 2 aniket aniket 4096 May 1 20:25 Downloads
...
Total line tells you the total size of your directory in blocks (1024 bytes) on the hard disk, which is 116 in our example.
Screen shot:
he above method will count symbolic links as well as subdirectories in targetdir (but not recursively into subdirectories).
Excluding subdirectories
If you want to exclude subdirectories, you can do.
find yourDir -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