Editing
Solaris 10 Associate
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Search Files & Directories == === Searching for contents in files === Search contents of files for string patterns with <code>grep</code>, <code>egrep</code>, <code>fgrep</code> commands ==== grep command ==== * <code>grep</code> command searches contents one or more file names for a specific character pattern * grep means globally search for a regular expression & print all lines containing regular expression * grep does not change file contents * Syntax: <code>grep options pattern filename</code> From grep man page: <pre> DESCRIPTION The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses a com- pact non-deterministic algorithm. Be careful using the characters $, *, [, ^, |, (, ), and \ in the pattern_list because they are also meaningful to the shell. It is safest to enclose the entire pattern_list in single quotes '... '. If no files are specified, grep assumes standard input. Nor- mally, each line found is copied to standard output. The file name is printed before each line found if there is more than one input file. OPTIONS The following options are supported for both /usr/bin/grep and /usr/xpg4/bin/grep: -b Precede each line by the block number on which it was found. This can be useful in locating block numbers by context (first block is 0). -c Print only a count of the lines that contain the pat- tern. -h Prevents the name of the file containing the matching line from being appended to that line. Used when searching multiple files. -i Ignore upper/lower case distinction during comparis- ons. -l Print only the names of files with matching lines, separated by NEWLINE characters. Does not repeat the names of files when the pattern is found more than once. -n Precede each line by its line number in the file (first line is 1). -s Suppress error messages about nonexistent or unread- able files. -v Print all lines except those that contain the pattern. -w Search for the expression as a word as if surrounded by \< and \>. </pre> <table style="text-align: left; width: 50%;" align="center" border="1" cellpadding="0" cellspacing="0"> <tr> <td></td> <td>Definition</td> </tr> <tr> <td>-i</td> <td>ignore case sensitivies</td> </tr> <tr> <td>-l</td> <td>print only the names of files with matching lines, does not repeat the names of files when pattern is found >1</td> </tr> <tr> <td>-n</td> <td>precedes each line by its line number in the file</td> </tr> <tr> <td>-v</td> <td>prints all lines except those that contain the pattern</td> </tr> <tr> <td>-c</td> <td>prints only a count of lines that contain the pattern</td> </tr> <tr> <td>-w</td> <td>search for the expression as a word as if surrounded by \< and \>, ignoring matches that are substrings of larger words</td> </tr> </table> <insert examples using grep> ===== Regular expression metacharacters ===== <table style="text-align: center; width: 50%;" align="center" border="1" cellpadding="0" cellspacing="0"> <tr> <td>Metacharacter</td> <td>Purpose</td> <td>Example</td> <td>Result</td> </tr> <tr> <td>^</td> <td>begining of line anchor</td> <td>'^pattern'</td> <td>matches all lines beginning with pattern</td> </tr> <tr> <td>$</td> <td>end of line anchor</td> <td>'pattern$'</td> <td>matches all lines ending with pattern</td> </tr> <tr> <td>.</td> <td>matches one character</td> <td>'p.....n'</td> <td>matches lines containing a "p", followed by five characters, and followed by an "n"</td> </tr> <tr> <td>*</td> <td>matches the preceding item zero or more times</td> <td>'[a-z]*'</td> <td>matches lowercase alphabet characters only</td> </tr> <tr> <td>[ ]</td> <td>matches one character in a pattern</td> <td>'[Pp]attern'</td> <td>matches lines containing Pattern or pattern</td> </tr> <tr> <td>[^]</td> <td>matches one character not in the pattern</td> <td>'[^a-m]attern'</td> <td>matches lines that do not contain a-m and followed by attern</td> </tr> </table> <insert examples regular expression metacharacters> ==== egrep command ==== <code>egrep</code> command searches the contents of one or more files for a pattern using extended regular expression metacharacters (regular expression characters plus more) Syntax: <code>egrep options pattern filename</code> <table style="text-align: center; width: 50%;" align="center" border="1" cellpadding="0" cellspacing="0"> <tr> <td>Metacharacter</td> <td>Purpose</td> <td>Example</td> <td>Result</td> </tr> <tr> <td>+</td> <td>matches one or more of the preceding characters</td> <td>'[a-z]+ark'</td> <td>matches one or more lowercase letters followed by ark</td> </tr> <tr> <td>x|y</td> <td>matches either x or y</td> <td>'boat|airplane'</td> <td>matches for either expresssion</td> </tr> <tr> <td>(|)</td> <td>group characters</td> <td>'(1|2)+'<br>'gam(es|ing)+'</td> <td>matches for one or more occurrences (1 or 2, games or gaming)</td> </tr> </table> ===== examples using egrep ===== <insert examples> search for all lines containing one or more lowercase alphabets followed by the pattern 'body' one or more times, perform the command: <code>$ egrep '[a-z]+body' /etc/passwd</code> to search for lines containing the pattern Network Admin or uucp Admin, perform the command: <code>$ egrep '(Network|uucp) Admin' /etc/passwd</code> ==== fgrep command ==== use fgrep command to search a file for a literal string or group of characters fgrep commands reads all characters as text (no metacharacters) syntax: <code>fgrep options string filename</code> ===== examples using fgrep ===== search for all lines in the file containing an * character, use the command: <code>$ fgrep '*' /etc/system</code> search for a string adm in all files in the current directory with the names ending with .sh string, use the command: <code>$ fgrep adm *.sh</code> === searching for files and directories === ==== find command ==== use <code>find</code> command to locate files or directories in the directory hierarchy <code>find</code> command recursively descends the directory tree in the path name list, looking for files that match search criteria as <code>find</code> command matches files matching search criteria the absolute path for each file is displayed on screen syntax: <code>find pathname expression action</code> <code>find</code> command arguments: * pathname = absolute or relative path where the search originates * expression = search criteria specified by one or more options. specifying multiple options causes the find command to use the boolean operator <code>and</code>, so all listed expressions must be verified as true * action = action required after the files have been located. the default print action is to print all path names matching the criteria to the screen ===== find expressions ===== some expressions for <code>find</code> command <table style="text-align: left; width: 50%;" border="1" cellpadding="0" cellspacing="0"> <tr> <td>expression</td> <td>definition</td> </tr> <tr> <td>-name filename</td> <td>finds files matching the specified filename, metacharacters are acceptable if placed inside quote marks " " or ' '</td> </tr> <tr> <td>-inum [integer]</td> <td>finds files by inode #</td> </tr> <tr> <td>-size [+|-]n</td> <td>finds files that are > +n or < -n or = n. n is 512-byte blocks</td> </tr> <tr> <td>-atime [+|-]n</td> <td>finds files that have been accessed > +n or < -n or = n. n is days</td> </tr> <tr> <td>-mtime [+|-]n</td> <td>find files that have been modified > +n or < -n or = n. n is days</td> </tr> <tr> <td>-user userID</td> <td>finds all files that are owned by the userID</td> </tr> <tr> <td>-type</td> <td>finds a file type, for example f = file and d = directory</td> </tr> <tr> <td>-perm</td> <td>finds files that have certain access permission bits</td> </tr> </table> ===== find actions ===== <table style="text-align: left; width: 50%;" border="1" cellpadding="0" cellspacing="0"> <tr> <td>action</td> <td>definition</td> </tr> <tr> <td>-exec command {} \;</td> <td>runs the specified command on each file located. a set of braces { } delimits where the file name is passed to the command from the preceding expressions. a space, backslash, and semicolon (\;) delimits the end of the command. There must be a space before the backslash (\)</td> </tr> <tr> <td>-ok command {} \;</td> <td>requires confirmation before find command applies the command to each file located. this is the interactive form of the -exec command</td> </tr> <tr> <td>-print</td> <td>instructs the find command to print the current path name to terminal session. this is the default</td> </tr> <tr> <td>-ls</td> <td>displays the current path name and associated statistics, such as the inode number, the size in kilobytes, protection mode, the number of hard links, and the user</td> </tr> </table> ===== find examples ===== <insert examples> search home directory looking for files or directories called deleteme and asking before deleting any matches perform this command: <code>$ find ~ -name deleteme -ok rm {} \;</code> to look for all files that have not been modified in the last two days starting in current directory perform this command: <code>$ find . -mtime +2</code> remove files without confirmation that have been modified greater than 30 days in current working directory <code>find . -mtime +30 -exec rm -f {} \;</code> to find files larger than 10 blocks (512-byte blocks X 10 = 5,120 bytes) starting in your home directory perform this command: <code>$ find ~ -size +10</code>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information