010-100 · Question #70
Which command chain will count the number of regular files with the name of foo.txt within /home?
The correct answer is B. find /home -type f -name foo.txt | wc -l. To count only regular files named foo.txt under /home, you must use find with both -type f and -name, then pipe to wc -l.
Question
Which command chain will count the number of regular files with the name of foo.txt within /home?
Options
- Als -lR /home | grep foo.txt | wc -l
- Bfind /home -type f -name foo.txt | wc -l
- Cfind /home -name foo.txt -count
- Dfind /home -name foo.txt | wc -l
- Egrep -R foo.txt /home | wc -l
How the community answered
(20 responses)- A10% (2)
- B80% (16)
- D5% (1)
- E5% (1)
Why each option
To count only regular files named foo.txt under /home, you must use find with both -type f and -name, then pipe to wc -l.
ls -lR lists directory contents but its output includes header lines and formatting that can cause grep to produce false matches, making the count unreliable.
The find command with -type f restricts results to regular files only, excluding directories, symlinks, or other special files that might also be named foo.txt. Piping to wc -l counts each matching path as one line, giving an accurate count of only regular files with that exact name.
find has no -count option; this is not a valid find predicate and the command will fail with an error.
Omitting -type f means find will also match directories, symlinks, or other non-regular file types named foo.txt, potentially inflating the count.
grep -R searches file contents for the string foo.txt, not filenames, so it returns lines containing that text rather than files with that name.
Concept tested: Using find with -type f and wc -l to count files
Source: https://www.gnu.org/software/findutils/manual/html_mono/find.html
Topics
Community Discussion
No community discussion yet for this question.