1.11 Locating and Searching Files Flashcards
What is the advantage of using the locate utility over the find utility? What are the disadvantages?
- Advantage: the locate utility is much faster because it searches through a pre-built index of all the files in the filesystem, whereas the find utility physically walks through each directory looking for the criteria you specify
- Disadvantages: (a)the index has to be built and/or updated with the latest information before the command can be used, (b)index creation can take a long time and consume a lot of system resources, (c)the locate command is not as versatile or flexible as the find command, (d)the locate command is not installed by default on most systems.
When would you use the which command to locate a file?
Use the which command to display the full path to a particular shell command or utilty. It is not use to locate other kinds of files.
Which utilities can you use to locate a file?
- find
- locate
- whereis
- which
Which utilities can you use to locate and get information about a command?
Use the whereis command to locate and get information about a command.
What does it mean when a command is hashed?
When a command is hashed, it is in the shell’s hash table, which means the command command has been used recently.
How can you discover the category of a command?
Use the type command to discover the category of a command - what kind of command you are dealing with.
What commands can you use to search for text within files?
Use the grep (or egrep or fgrep) command to search for text within files.
How do you configure the system to use the locate command?
- Install the package containing the locate utility - findutils-locate
- Generate the index, /var/log/locatedb, using the updatedb command
What does the find command do?
Searches through all files (based on the file system) by name, file size, time created, and other options.
What does the -name option of the find command do?
The -name option locates a file or directory by name in a specific path. When using this option, enclose string arguments in single quotes and use wildcards for partial names.
What does the -iname option of the find command do?
The -iname option locates a file or directory by name in a specific path, like the -name option, but matching is not case-sensitive.
What does the -user option of the find command do?
The -user option finds files owned by a specific user.
What does the -size option of the find command do?
The -size option finds files of a specific size. Use the following options:
Which argument modifiers can be used with the -size option of the find command?
- c specifies the file size in bytes
- k specifies the file size in kilobytes
- M specifies the file size in megabytes
What does the -mtime option of the find command do?
The -mtime option finds files last modified before or after a specified number of days ago.
What does the -type option of the find command do?
The -type option specifies whether to find files or directories. The f argument specifies files, and the d argument specifies directories.
What does the -maxdepth option of the find command do?
The -maxdepth option specifies how many directory levels down to search.
What does the -print0 option of the find command do?
The -print0 option finds filenames with spaces.
What does the -o parameter of the find command do?
The -o parameter specifies a logical OR between options when searching with multiple criteria.
What does specifying . as the directory argument of the find command do?
Specifying . as the directory argument causes find to search the current directory and all subdirectories.
What does the locate command do?
Search an index file for specific parameters.
What are the main features of the locate command?
- Much faster than find.
- Uses /var/log/locatedb as the index file for searches.
- Uses the updatedb command to update the file index. The configuration file for updatedb is /etc/updatedb.conf
- Searches from the root (/) directory if no path is specified.
- Finds all files that contain the specified string without using wildcards.
- Does not, by default, verify that the file exists if its file index is outdated.
- Does not display files created after the last time the file index was updated.
- Does not search for files by attribute.
Where is the index file used by the locate command to conduct searches?
The locate command uses /var/log/locatedb as the index file when conducting searches.
How do you update the index file used by the locate command after file system changes?
The updatedb command updates the index used by locate to conduct searches.
How does the -coption modify the behavior of the locate command?
locate -c counts the number of entries rather than list them.
How does the -eoption modify the behavior of the locate command?
locate -e lists files only after verifying that they exist.
How does the -ioption modify the behavior of the locate command?
locate -i ignores case.
How does the -loption modify the behavior of the locate command?
locate -l limits the number of files listed.
How does the -boption modify the behavior of the locate command?
locate -b searches for the string in only file or directory base names.
What does the which command do?
Displays the path to a command and determines whether a package is installed.
What does the whereis command do?
Displays the path to the binary files, the manual pages, and the source code. With no options, whereis show all available data.
What does the command whereis -b do?
whereis -b lists the path to the binary file (similar to which).
What does the command whereis -m do?
whereis -m lists the path to the man page files.
What does the command whereis -s do?
whereis -s lists the location of the source code.
What does the command whereis -u do?
whereis -u lists entries that do not have source code, binary file, and man page locations.
What does the type command do?
type displays the category of the command. Possible categories include:
What are possible categories for the type command?
- A built-in shell command
- A command that the shell calls (external command)
- An aliased command
- A function
- *Note**: If a called command has been used recently, the output says that the command is hashed, which means that it is in the shell’s hash table.
What does the comand find /user/home -name ‘*.txt’ do?
find /user/home -name ‘*.txt’ finds all plain text files in the /user/home directory.
What does the comand find / -name ‘*paper*‘ do?
find / -name ‘*paper*‘ looks through the entire directory for any folder or directory name with the term paper in it, such as termpaper.odt or wallpaper.jpg.
What does the comand find /user/home -size -300k do?
find /user/home -size -300k finds all files in the /user/home directory smaller than 300K.
What does the comand find /user/home -size +300k do?
find /user/home -size +300k finds all files in the /user/home directory larger than 300K.
What does the comand find /user/home -mtime -5 do?
find /user/home -mtime -5 finds all files in the /user/home directory modified within the last five days.
What does the comand find / -type f -name ‘*paper*‘ do?
find / -type f -name ‘*paper*‘ finds only files with the string paper in the name.
What does the comand find / -type d -name ‘*paper*‘ do?
find / -type d -name ‘*paper*‘ finds only directories with the string paper in the name.
What does the comand find -maxdepth 3 / -name ‘*.txt’ do?
find -maxdepth 3 / -name ‘*.txt’ finds text files three directory levels down from the root directory.
What does the comand find -print0 -name ‘*.txt’ do?
find -print0 -name ‘*.txt’ finds myreport.txt and my report.txt. Without the -print0 option, my report.txt is not listed.