nerdexam
LPI

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.

The Power of the Command Line

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)
  • A
    10% (2)
  • B
    80% (16)
  • D
    5% (1)
  • E
    5% (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.

Als -lR /home | grep foo.txt | 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.

Bfind /home -type f -name foo.txt | wc -lCorrect

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.

Cfind /home -name foo.txt -count

find has no -count option; this is not a valid find predicate and the command will fail with an error.

Dfind /home -name foo.txt | wc -l

Omitting -type f means find will also match directories, symlinks, or other non-regular file types named foo.txt, potentially inflating the count.

Egrep -R foo.txt /home | wc -l

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

#find command#wc#pipe#file counting

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice