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.
What does the comand updatedb do?
updatedb updates the index file, /var/log/locatedb.
What does the comand locate /user/home paper do?
locate /user/home paper locates all files with the string paper as any part of the file name or directory path under the /user/home directory.
What does the comand locate lib do?
locate lib locates all files with the string lib anywhere in the file name or directory path.
What does the comand locate -c lib do?
locate -c lib counts the number of files with the string lib.
What does the comand locate -e .odt do?
locate -e .odt verifies that all .odt files listed in the file index actually exist before it lists them.
What does the comand locate -i LibraryFines.csv do?
locate -i LibraryFines.csv finds the libraryfines.csv file regardless of case.
What does the comand locate -l 25 lib do?
locate -l 25 lib lists only the first 25 files from the file index that contain the string lib.
What does the comand locate -b lib do?
locate -b lib displays /var/lib and /user/home/libraryfines.csv but not /var/lib/usbutils/usb.ids.
What does the comand which ls do?
which ls shows the path to the ls binary (executable) file.
What does the comand which photorec do?
which photorec shows the path to the photorec binary file if photorec is installed. If the command does not display a path, then the photorec utility is not installed.
What does the comand whereis -m -u * do?
whereis -m -u * lists all entries that have no man page location.
What does the comand type cd do?
type cd displays cd is a shell built-in.
What does the comand type more do?
type more displays the path to the binary file for more.
What does the grep command do?
The grep command searches through file text for specific words or character patterns.
What does the -A number option of the grep command do?
grep -A number prints a specified number of lines following the matching lines.
What does the -a option of the grep command do?
grep -a searches binary (executable) files as though they were text files.
What does the -B number option of the grep command do?
grep -B number prints a specified number of lines before the matching lines.
What does the -C number option of the grep command do?
grep -C number prints a specified number of lines of context around the matching lines.
What does the -c option of the grep command do?
grep -c shows the number of matches of the string for the file.
What does the -E option of the grep command do?
grep -E uses regular expressions for the text pattern.
What does the -e pattern option of the grep command do?
grep -e pattern specifies a literal pattern.
What does the -f file_name option of the grep command do?
grep -f file_name searches for multiple strings using a file that lists the string patterns.
What does the -F option of the grep command do?
grep -F uses a file as the source for the string patterns.
What does the -i option of the grep command do?
grep -i ignores the case of the string.
What does the -l option of the grep command do?
grep -l lists just the names of the files with a match.
What does the -m number option of the grep command do?
grep -m number shows only a specified number of matches for a file.
What does the -n option of the grep command do?
grep -n displays the line number of the lines containing the term.
What does the -r option of the grep command do?
grep -r searches the directory and all sub-directories for files containing the term.
What does the -v option of the grep command do?
grep -v displays non-matching lines.
What does the –include=file_name option of the grep command do?
grep –include=file_name searches only in files with names that match a specified string.
What does the –exclude=file_name option of the grep command do?
grep –exclude=file_name searches in files with names that do not match a specified string.
What does the -w option of the grep command do?
grep -w searches for whole words only.
What does the egrep command do?
The egrep command, like grep, searches through file text for specific words or character patterns, but allows regular expressions in the search strings. Uses the same options and syntax as grep and is identical to grep -E.
What does the regular expression pattern ^ do?
^ matches terms that occur at the beginning of a line.
What does the regular expression pattern $ do?
$ matches terms that occur at the end of a line.
What does the regular expression pattern \< do?
\< matches words that begin with the term.
What does the regular expression pattern \> do?
\> matches words that end with the term.
What does the regular expression pattern [asdf] do?
[asdf] matches any one of the characters in the brackets.
What does the regular expression pattern [0-9] do?
[0-9] matches any of the range of numbers 0-9.
What does the regular expression pattern [^xyz] do?
[^xyz] omits any one of the letters in the list
What does the regular expression pattern . do?
. matches any single character.
What does the regular expression pattern [asdf]+ do?
[asdf]+ matches one or more of the characters in the list.
What does the regular expression pattern * do?
* matches any number, or none of the preceding single character
What does the regular expression pattern | do?
| matches either of the terms.
What does the regular expression pattern *c* do?
**** displays the literal value of a character used for expressions.
What does the regular expression pattern () do?
() groups expressions.
What does the fgrep command do?
The fgrep command, like grep, searches through file text for specific words or character patterns, but searches only for fixed strings, not wildcards or regular expressions, and is much faster than grep. Uses the same options and syntax as grep and is identical to grep -F.
What does the command grep -A 3 Midway ~/docs/WWII-report do?
grep -A 3 Midway ~/docs/WWII-report searches WWII-report for the pattern Midway and prints the line and the next three lines.
What does the command grep -a var11 /bin do?
grep -a var11 /bin searches all files, including binary files, in the /bin directory for the pattern var11.
What does the command grep -c 3 Midway ~/docs/WWII-report do?
grep -c 3 Midway ~/docs/WWII-report shows only the number of times the pattern Midway is found in the WWII-report file.
What does the command grep -e ‘–count’ ~/docs/doc1 do?
grep -e ‘–count’ ~/docs/doc1 looks for the pattern –count in the doc1 file rather than interpreting it as an option.
What does the command grep -l -r Midway ~/docs do?
grep -l -r Midway ~/docs shows the name of all files in the /home/user/docs directory that contain the term Midway.
What does the command grep -m 2 battle ~/docs/WWII-report do?
grep -m 2 battle ~/docs/WWII-report shows only the first two times the term battle is found in the file.
What does the command grep -n -i customVariable1 ~/java/program1.java do?
grep -n -i customVariable1 ~/java/program1.java shows the line numbers of lines that have the term customVariable1 in the program1.java file. This is case insensitive.
What does the command grep -r battle ~/docs/ do?
grep -r battle ~/docs/ searches the directory and all sub-directories for the term battle.
What does the command grep -w tank ~/docs/WWII-report do?
grep -w tank ~/docs/WWII-report searches only for the whole word tank in the file.
What does the command egrep ^FAILURE ~/error_logs do?
egrep ^FAILURE ~/error_logs matches the term FAILURE when it is at the beginning of the line in error_logs.
What does the command egrep tty7$ ~/.bash_history do?
egrep tty7$ ~/.bash_history matches the term tty7 when it is at the end of the line.
What does the command **egrep ** do?
**egrep ** matches all words or strings that begin with are. This includes are, area, and arena.
What does the command egrep \>are ~/myfile do?
egrep \>are ~/myfile matches all words or strings that end with are. This includes areI, hare, and aware.
What does the command egrep watche[ds] ~/myfile do?
egrep watche[ds] ~/myfile matches either watched or watches.
What does the command egrep exhibit[0-9] ~/myfile do?
egrep exhibit[0-9] ~/myfile matches exhibit1, exhibit3, or exhibit8.
What does the command egrep [^Xx]mas ~/myfile do?
egrep [^Xx]mas ~/myfile matches Christmas but not xmas or Xmas.
What does the command egrep .are ~/myfile do?
egrep .are ~/myfile matches hare and care, but not aware or are.
What does the command egrep file[0-9]+ ~/myfile do?
egrep file[0-9]+ ~/myfile matches file0, file10, and file15636.
What does the command egrep fil* ~/myfile do?
egrep fil* ~/myfile matches fil, filll, and fillllllllllllllll.
What does the command egrep fil.* ~/myfile do?
egrep fil.* ~/myfile matches file, fill, file102, and filings.
What does the command egrep men|women ~/myfile do?
egrep men|women ~/myfile matches men or women.
What does the command egrep Hello\? ~/myfile do?
egrep Hello\? ~/myfile matches Hello?.
What does the command fgrep Midway Nimitz ~/docs/myfile do?
fgrep Midway Nimitz ~/docs/myfile searches myfile for lines containing Midway or Nimitz.