How can I recursively count files in a Linux directory?
I found this:
find DIR_NAME -type f ¦ wc -l
But when I run this it returns the following error.
find: paths must precede expression: ¦
Use File Explorer Open the folder and select all the subfolders or files either manually or by pressing CTRL+A shortcut. If you choose manually, you can select and omit particular files. You can now see the total count near the left bottom of the window. Repeat the same for the files inside a folder and subfolder too.
To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1.
For the current directory:
find -type f | wc -l
This should work:
find DIR_NAME -type f | wc -l
Explanation:
-type f
to include only files.|
(and not ¦
) redirects find
command's standard output to wc
command's standard input.wc
(short for word count) counts newlines, words and bytes on its input (docs).-l
to count just newlines.Notes:
DIR_NAME
with .
to execute the command in the current folder.-type f
to include directories (and symlinks) in the count.Explanation of why your example does not work:
In the command you showed, you do not use the "Pipe" (|
) to kind-of connect two commands, but the broken bar (¦
) which the shell does not recognize as a command or something similar. That's why you get that error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With