|
5.07 Location Utilities
grep, egrep,
fgrep: print lines matching a pattern (find
a string)
grep - is
a command for searching files for lines matching a given regular expression
(look in section 5.05 for regular expresions)
Examples: How
to use grep utility
|
[bash]$
grep Student personal.file
or
[bash]$
grep ^Bab personal.file
or
[bash]$
grep -c Chris *.file |
Example:
In this example the grep
will not work because the pipe ( |
) is not a regular expression.
|
[bash]$
grep 'Chris|Siedlecki' personal.file |
Examples:
Other useful examples of grep
utility
|
[bash]$
grep MemTotal /proc/meminfo
# (determine the physical RAM size)
or
[bash]$
grep SwapTotal /proc/meminfo
#(dtermine the size of the configured swap space)
or
[bash]$
grep -c /bin/bash /etc/passwd # (counts all
users who use Bash shell by searching the /etc/passwd file that
list what default shell is for each user.) |
egrep - is
extended version of grep, and it searched for regular and extended
regular expressions. (look sections 5.05, 5.06 for regular, and regular
extended expressions)
Example:
|
[bash]$
egrep 'Chris|Siedlecki' personal.file |
fgrep - is
a commands that searches only for fixed, uninterrupted strings rather
than regular expressions.
Options:
-c count
-d action Directories
-E Extended #same
as egrep
-F Fixed strings #same
as fgrep
-I Ignore Case
-n Line numbers
-r Recursive
-v Not match
-w Word
Examples:
|
[bash]$
fgrep Chris personal.file
[bash]$
fgrep Auto* personal.file
# (Will not work, because Wildcards are not supported by fgrep
command) |
locate - the
fastest way to search for a file or files in entire directory tree
find - searches
for a directory tree for files that meet criteria that you specified
( Syntax: find pathname -name filename)
Options:
-atime <+t, -t, t>
Searches for a file last accessed in a certain period of time.
-ctime <+t, -t, t>
Searches for a file last changed in a certain period of time.
-mtime <+t, -t, t >
Searches for a file last modified in a certain period of time.
-iname Searches for
a specific file ignoring case
-name Searches for a
specific file name
-print Some versions
of Unix/Linux will not display the output of the find command unless
–print is added
-type <type> Searches
for a files of specific type. Two popular choices are d for directory
and f for files
Example:
|
[bash]$ find
-name foo.name
[bash]$ find
-name fstab 2>/dev/null # (The part after fstab sends
an error massage to a null device) |
whereis - locate
the binary, source, and manual page files for a command (searches
for files related to a utility by looking in standard locations instead
of using your search path)
Example: Whereis
command verifies the location of the fdisk; fdformat and mkfs
|
[bash]$
whereis fdisk
[bash]$
whereis fdformat
mkfs |
which - displays
the full pathname to the file for the utility
whatis - search the whatis database
for complete words
apropos - search the whatis database
for string (used when not sure of a command)
locate - security
enhanced version of GNU locate
manpath
- determines users search path for man pages
|