117-010 · 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. See the full explanation below for the reasoning.
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
(40 responses)- A5% (2)
- B85% (34)
- C3% (1)
- D8% (3)
Community Discussion
12The answer is B, find /home -type f -name foo.txt | wc -l. The key piece is -type f, which tells find to only return regular files, so if there happened to be a directory or symlink also named foo.txt it would not get counted. My senior showed me that D trips a lot of people because it looks almost right, but dropping -type f means find returns everything with that name, not just regular files. Option C is just made up, there is no -count flag in find, and A and E are searching file contents or listing directories in ways that can produce false matches or miss things entirely. Pipe the find output into wc -l and each result is one line, so the line count equals your file count.
B is it. On my own LPI exam I almost picked D, then caught that -type f locks it to regular files only, no symlinks sneaking in.
Good catch, and a useful pair to memorize alongside it is -type l, which targets symbolic links themselves, so if a future question flips the scenario and asks which flag isolates only symlinks rather than excluding them, you already have the answer.
B is the move, and here is why D almost got me. On my actual exam I nearly bubbled D without thinking, then caught myself: find without -type f pulls in directories named foo.txt too, and wc -l would overcount. Adding -type f locks it to regular files only, which is exactly what the question asks. Glad I slowed down that last pass.
Good catch on the directory trap, but worth knowing -type f also silently skips symlinks to files, so if the environment has any symlinked logs or whatever, your count could still be off without -L on the find or a -follow flag.
D is the trap that'll get you if you skim. It looks identical to B but it drops the -type f, so it catches directories and symlinks named foo.txt too, which the question specifically says to exclude. B is correct because -type f restricts matches to regular files before you even pipe anything.
Viktor nailed the core of it, and worth adding for the objective mapping crowd, this exact trap lives squarely in LPIC-1 104.7 weight territory where they love to test whether you know -type f excludes not just directories but also symlinks, sockets, and device nodes, so if you see "regular files only" anywhere in the stem, -type f is your lock.
D is the sneaky trap here, because find without -type f will also catch directories, symlinks, or anything else named foo.txt, so your count would be wrong if any of those exist. B locks it down to regular files only with -type f, which is exactly what the question asks for.
My first read had me leaning toward D because it uses find and name together, but reading the stem a second time, the word "regular files" is the trigger, and only B adds -type f to actually restrict the results to regular files instead of also catching directories named foo.txt.
I almost picked D because it looks almost identical to B and I thought the pipe to wc -l was the main thing that mattered, but then I noticed D is missing -type f, which means it would also count directories, symlinks, or anything else named foo.txt, not just regular files. The question specifically says "regular files," so -type f is required, and B is the only one that includes it.
I am going with D because find /home -name foo.txt already scopes the search to files matching that exact name and piping to wc -l gives you the count, which is exactly what the question asks for. The -type f flag in option B is redundant in practice since foo.txt is clearly a file name, not a directory.
The -type f flag is not about the file name, it is about making sure find only counts actual files and skips anything else like directories or symlinks that happen to be named foo.txt. Without it, you could get a higher count than expected in an environment that has non-file entries with that name.