Linux System Administrator: Searching for Executable Files in Non-$PATH Directories

Linux System Administrator Certification Exam LFCS Practice Question

Question

Which of the following commands can be used to search for the executable file foo when it has been placed in a directory not included in $PATH?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

C

The correct answer is C. find.

Explanation:

In Linux, when a command is entered in the terminal, the shell searches for the executable file in a set of directories specified in the environment variable $PATH. If the executable is not found in any of those directories, then the command will fail.

However, it's possible that an executable file may be located in a directory that is not included in $PATH. In this case, we can use the find command to search for the file.

The find command is used to search for files and directories in a given directory hierarchy. It can search for files based on various criteria, such as name, type, size, and date modified.

To use find to search for an executable file named foo in a specific directory, we can use the following command:

bash
find /path/to/directory -name foo -type f -perm /a+x

Here, /path/to/directory is the path to the directory where we want to search for the file foo. The -name option specifies the name of the file we want to find, and the -type f option specifies that we are searching for a regular file. The -perm /a+x option specifies that we are searching for a file that is executable by anyone.

Once find locates the file, we can run it by specifying its full path in the command line, for example:

bash
/path/to/directory/foo

Alternatively, we can add the directory containing the file to $PATH, so that we can run the file without specifying its full path each time. However, this may not always be desirable, especially if the file is located in a directory that is not meant to be used for general system-wide executables. In such cases, using find to locate the file is a more secure and controlled approach.