1.11 Locating and Searching Files Flashcards

2
Q

What is the advantage of using the locate utility over the find utility? What are the disadvantages?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When would you use the which command to locate a file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which utilities can you use to locate a file?

A
  • find
  • locate
  • whereis
  • which
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which utilities can you use to locate and get information about a command?

A

Use the whereis command to locate and get information about a command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does it mean when a command is hashed?

A

When a command is hashed, it is in the shell’s hash table, which means the command command has been used recently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you discover the category of a command?

A

Use the type command to discover the category of a command - what kind of command you are dealing with.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What commands can you use to search for text within files?

A

Use the grep (or egrep or fgrep) command to search for text within files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you configure the system to use the locate command?

A
  1. Install the package containing the locate utility - findutils-locate
  2. Generate the index, /var/log/locatedb, using the updatedb command
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the find command do?

A

Searches through all files (based on the file system) by name, file size, time created, and other options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the -name option of the find command do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the -iname option of the find command do?

A

The -iname option locates a file or directory by name in a specific path, like the -name option, but matching is not case-sensitive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the -user option of the find command do?

A

The -user option finds files owned by a specific user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the -size option of the find command do?

A

The -size option finds files of a specific size. Use the following options:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which argument modifiers can be used with the -size option of the find command?

A
  • c specifies the file size in bytes
  • k specifies the file size in kilobytes
  • M specifies the file size in megabytes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the -mtime option of the find command do?

A

The -mtime option finds files last modified before or after a specified number of days ago.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the -type option of the find command do?

A

The -type option specifies whether to find files or directories. The f argument specifies files, and the d argument specifies directories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the -maxdepth option of the find command do?

A

The -maxdepth option specifies how many directory levels down to search.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does the -print0 option of the find command do?

A

The -print0 option finds filenames with spaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the -o parameter of the find command do?

A

The -o parameter specifies a logical OR between options when searching with multiple criteria.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does specifying . as the directory argument of the find command do?

A

Specifying . as the directory argument causes find to search the current directory and all subdirectories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does the locate command do?

A

Search an index file for specific parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the main features of the locate command?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Where is the index file used by the locate command to conduct searches?

A

The locate command uses /var/log/locatedb as the index file when conducting searches.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you update the index file used by the locate command after file system changes?

A

The updatedb command updates the index used by locate to conduct searches.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
How does the **-c**option modify the behavior of the **locate** command?
**locate -c** counts the number of entries rather than list them.
27
How does the **-e**option modify the behavior of the **locate** command?
**locate -e** lists files only after verifying that they exist.
28
How does the **-i**option modify the behavior of the **locate** command?
**locate -i** ignores case.
29
How does the **-l**option modify the behavior of the **locate** command?
**locate -l** limits the number of files listed.
30
How does the **-b**option modify the behavior of the **locate** command?
**locate -b** searches for the string in only file or directory base names.
31
What does the **which** command do?
Displays the path to a command and determines whether a package is installed.
32
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.
33
What does the command **whereis -b** do?
**whereis -b** lists the path to the binary file (similar to **which**).
34
What does the command **whereis -m** do?
**whereis -m** lists the path to the man page files.
35
What does the command **whereis -s** do?
**whereis -s** lists the location of the source code.
36
What does the command **whereis -u** do?
**whereis -u** lists entries that do not have source code, binary file, and **man** page locations.
37
What does the **type** command do?
**type** displays the category of the command. Possible categories include:
38
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.
39
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.
40
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**.
41
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.
42
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.
43
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.
44
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.
45
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.
46
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.
47
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.
48
What does the comand **updatedb** do?
**updatedb** updates the index file, **/var/log/locatedb**.
49
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.
50
What does the comand **locate lib** do?
**locate lib** locates all files with the string *lib* anywhere in the file name or directory path.
51
What does the comand **locate -c lib** do?
**locate -c lib** counts the number of files with the string *lib*.
52
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.**
53
What does the comand **locate -i LibraryFines.csv** do?
**locate -i LibraryFines.csv** finds the **libraryfines.csv** file regardless of case.
54
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*.
55
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**.
56
What does the comand **which ls** do?
**which ls** shows the path to the **ls** binary (executable) file.
57
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.
58
What does the comand **whereis -m -u \*** do?
**whereis -m -u \*** lists all entries that have no **man** page location.
59
What does the comand **type cd** do?
**type cd** displays **cd** is a shell built-in.
60
What does the comand **type more** do?
**type more** displays the path to the binary file for **more**.
61
What does the **grep** command do?
The **grep** command searches through file text for specific words or character patterns.
62
What does the **-A *number*** option of the **grep** command do?
**grep -A *number*** prints a specified number of lines following the matching lines.
63
What does the **-a** option of the **grep** command do?
**grep -a** searches binary (executable) files as though they were text files.
64
What does the **-B *number*** option of the **grep** command do?
**grep -B *number*** prints a specified number of lines before the matching lines.
65
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.
66
What does the **-c** option of the **grep** command do?
**grep -c** shows the number of matches of the string for the file.
67
What does the **-E** option of the **grep** command do?
**grep -E** uses regular expressions for the text pattern.
68
What does the **-e *pattern*** option of the **grep** command do?
**grep -e *pattern*** specifies a literal pattern.
69
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.
70
What does the **-F** option of the **grep** command do?
**grep -F** uses a file as the source for the string patterns.
71
What does the **-i** option of the **grep** command do?
**grep -i** ignores the case of the string.
72
What does the **-l** option of the **grep** command do?
**grep -l** lists just the names of the files with a match.
73
What does the **-m *number*** option of the **grep** command do?
**grep -m *number*** shows only a specified number of matches for a file.
74
What does the **-n** option of the **grep** command do?
**grep -n** displays the line number of the lines containing the term.
75
What does the **-r** option of the **grep** command do?
**grep -r** searches the directory and all sub-directories for files containing the term.
76
What does the **-v** option of the **grep** command do?
**grep -v** displays non-matching lines.
77
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.
78
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.
79
What does the **-w** option of the **grep** command do?
**grep -w** searches for whole words only.
80
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**.
81
What does the regular expression pattern **^** do?
**^** matches terms that occur at the beginning of a line.
82
What does the regular expression pattern **$** do?
**$** matches terms that occur at the end of a line.
83
What does the regular expression pattern **\\<** do?
**\\<** matches words that begin with the term.
84
What does the regular expression pattern **\\>** do?
**\\>** matches words that end with the term.
85
What does the regular expression pattern **[asdf]** do?
**[asdf]** matches any one of the characters in the brackets.
86
What does the regular expression pattern **[0-9]** do?
**[0-9]** matches any of the range of numbers 0-9.
87
What does the regular expression pattern **[^xyz]** do?
**[^xyz]** omits any one of the letters in the list
88
What does the regular expression pattern **.** do?
**.** matches any single character.
89
What does the regular expression pattern **[asdf]+** do?
**[asdf]+** matches one or more of the characters in the list.
90
What does the regular expression pattern **\*** do?
**\*** matches any number, or none of the preceding single character
91
What does the regular expression pattern **|** do?
**|** matches either of the terms.
92
What does the regular expression pattern **\*c*** do?
**\** displays the literal value of a character used for expressions.
93
What does the regular expression pattern **()** do?
**()** groups expressions.
94
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**.
95
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.
96
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*.
97
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.
98
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.
99
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*.
100
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.
101
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.
102
What does the command **grep -r battle ~/docs/** do?
**grep -r battle ~/docs/** searches the directory and all sub-directories for the term *battle*.
103
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.
104
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**.
105
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.
106
What does the command **egrep \** do?
**egrep \** matches all words or strings that begin with *are*. This includes *are*, *area*, and *arena*.
107
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*.
108
What does the command **egrep watche[ds] ~/myfile** do?
**egrep watche[ds] ~/myfile** matches either *watched* or *watches*.
109
What does the command **egrep exhibit[0-9] ~/myfile** do?
**egrep exhibit[0-9] ~/myfile** matches *exhibit1*, *exhibit3*, or *exhibit8*.
110
What does the command **egrep [^Xx]mas ~/myfile** do?
**egrep [^Xx]mas ~/myfile** matches *Christmas* but not *xmas* or *Xmas*.
111
What does the command **egrep .are ~/myfile** do?
**egrep .are ~/myfile** matches *hare* and *care*, but not *aware* or *are*.
112
What does the command **egrep file[0-9]+ ~/myfile** do?
**egrep file[0-9]+ ~/myfile** matches *file0*, *file10*, and *file15636*.
113
What does the command **egrep fil\* ~/myfile** do?
**egrep fil\* ~/myfile** matches *fil*, *filll*, and *fillllllllllllllll*.
114
What does the command **egrep fil.\* ~/myfile** do?
**egrep fil.\* ~/myfile** matches *file*, *fill*, *file102*, and *filings*.
115
What does the command **egrep men|women ~/myfile** do?
**egrep men|women ~/myfile** matches *men* or *women*.
116
What does the command **egrep Hello\? ~/myfile** do?
**egrep Hello\? ~/myfile** matches *Hello?*.
117
What does the command **fgrep Midway Nimitz ~/docs/myfile** do?
**fgrep Midway Nimitz ~/docs/myfile** searches **myfile** for lines containing *Midway* or *Nimitz*.