Note, I got to this thought process while getting this answer. Which means list one file per line and then pipe it to word count command with parameter switching to count lines.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Is there a bash command which counts files? Ask Question. Asked 9 years, 6 months ago. Active 1 year, 2 months ago. Viewed k times. Is there a bash command which counts the number of files that match a pattern? Improve this question. Add a comment. Active Oldest Votes. The output is piped to wc -l, which counts the number of lines.
Improve this answer. Daniel Daniel 3, 2 2 gold badges 18 18 silver badges 26 26 bronze badges. I would not use -l , since that requires stat 2 on each file and for the purposes of counting adds nothing. I would not use ls , since it creates a child process. Except an echo will not work if you have file names with spaces or special characters. WalterTross That's true not that efficiency was a requirement of the original question. I also just found that -q takes care of files with newlines, even when the output is not the terminal.
And these flags are supported by all the platforms and shells I've tested on. Updating the answer, thanks to you and camh for the input! If there's a directory called logs in the directory in question, then the contents of that logs directory will be counted too. This is probably not intentional. Show 11 more comments. Mat Mat k 39 39 gold badges silver badges bronze badges. Perhaps explicitly point out that this is a Bash- only answer, especially for new visitors who are not yet entirely up to speed on the Difference between sh and bash — tripleee.
Also, the final shopt -u nullglob should be skipped if nullglob wasn't unset then you started. If the files you wish to enumerate have the traditional naming convention of name.
An explanation of what the [ ] means would be helpful — Andy Preston. Note that shopt -s nullglob would cause ls to list the entire working directory instead. Nevertheless, the original answer is extremely helpful. For a recursive search: find. For a non-recursive search, do this: find. Will Vousden Will Vousden Even if you don't have files with spaces, some other user of your script might encounter a maliciously named file, causing the scripts to fail.
Also, other people encountering this on StackOverflow might have files with newlines, and need to know the pitfalls. Is it a file that you can open up and view its contents? Yes, character special: Code :. Shell Programming and Scripting. Find: filename in every subdirectory matching a pattern. Find pattern suffix matching pattern.
Hi, I am trying to get a result out of this but fails please help. Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction.
Find all matching words in text according to pattern. Hello dear Unix shell professionals, I am desperately trying to get a seemingly simple logic to work. I need to extract words from a text line and save them in an array. I am handicapped FIND matching pattern of lines in a file. I need to search for two patterns in a file and find number of matching lines. However I am looking at finding a commnd that will simplify as on a singe command Help me to find files in a shell script with any matching pattern.
Hi friends.. I have many dirs in my working directory. Every dir have thousands of files. So I am working with an script to find every file recursively within those directories and subdirectories ending with. Hi, I want to find all directories matching given pattern in current directory and zip those files.
Pretty interesting, Petesh, didn't know about it. I have tested the performance and to me with ls -1f it was even faster than find. If you use the -printf '. Alternatively, add a newline after the dot -printf '. How about using --count -c for grep and skipping wc? I would expect performance gain. And also a simpler expression. Then again, for the same reasons, I would expect find with -name to be faster than ls grep while apparently it is not Show 2 more comments.
Odobenus Rosmarus Odobenus Rosmarus 5, 2 2 gold badges 17 17 silver badges 21 21 bronze badges. Dale Dale 57 1 1 silver badge 6 6 bronze badges. To accomplish the same result as the solution proposed by fedorqui-supports-monica try this: find.
To limit your search depth to the current directory, add -maxdepth 1 to the command like so: find. Tal Ater Tal Ater 1, 1 1 gold badge 10 10 silver badges 16 16 bronze badges. Jas Jas 11 3 3 bronze badges. This is basically a repeat of a previous answer plus it won't work.
This is confusing shell wildcards and regular expressions. I support the idea that you should generally use quoting around your regexes, but in this particular case, it's not necessary, and the incorrect regex ruins the answer. Sign up or log in Sign up using Google. Sign up using Facebook.
0コメント