010-150 · Question #65
How can the current directory and its subdirectories be searched for the file named MyFile.xml?
The correct answer is A. find . -name MyFile.xml. The find command with the -name predicate recursively searches a directory tree for files matching a given name.
Question
How can the current directory and its subdirectories be searched for the file named MyFile.xml?
Options
- Afind . -name MyFile.xml
- Bgrep MyFile.xml | find
- Cgrep -r MyFile.xml .
- Dless MyFile.xml
- Esearch Myfile.xml ./
How the community answered
(32 responses)- A72% (23)
- B3% (1)
- C3% (1)
- D16% (5)
- E6% (2)
Why each option
The find command with the -name predicate recursively searches a directory tree for files matching a given name.
The command 'find . -name MyFile.xml' begins its search at the current directory (.) and descends into all subdirectories, returning every path whose filename matches MyFile.xml exactly. This is the standard POSIX-compliant method for locating a file by name within a directory tree.
grep searches the contents of files for a text pattern and cannot locate files by name; piping a filename string into find is not valid syntax for this purpose.
grep -r recursively searches the contents of files for the pattern 'MyFile.xml', not for a file whose name is MyFile.xml.
less is a pager for viewing the contents of a known file; it requires the file path as an argument and cannot search for a file by name.
'search' is not a standard Linux command and would produce a 'command not found' error on typical systems.
Concept tested: Using find command to locate files by name
Source: https://man7.org/linux/man-pages/man1/find.1.html
Topics
Community Discussion
No community discussion yet for this question.