QuestionQ155

Essential Commands

Which command chain will count the number of regular files named foo.txt located within /home?

  • A ls -lR /home | grep foo.txt | wc -l
  • B find /home -type f -name foo.txt | wc -l
  • C find /home -name foo.txt -count
  • D find /home -name foo.txt | wc -l
  • E grep -R foo.txt /home | wc -l
Explanation

The find utility's -type f flag restricts results to regular files, and -name foo.txt matches files with that exact name. Piping the output to wc -l counts the resulting lines, giving an accurate count of regular files named foo.txt. This is documented behavior of the GNU findutils find command, where -type f is the standard way to filter for regular files as opposed to directories, symlinks, or other file types.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!