Chapter files and text search
Translation: hfzheng
E-mail: hfzheng@sohu.com
Reprinted Please keep this information!
Locate files using the find command
Use the find command to locate files in the directory tree, use the command can be based on specified rules to locate the file, for example, specify a file name, size, owner, modification time, type.
find command search path list recursive directory tree to find the rules to meet the specified file. When those who satisfy the conditions of the document orientation, the screen will display each file's path.
Command format
find pathnames expressions actions
The first command line argument for the start of the path name search, you can use an absolute path or relative path to specify. Search rules, and other parameters specified when searching files to the action to be taken. The following three sections describe the find command can be used in the parameters, expression and movement.
Parameter definition
pathname search the directory path beginning
expression of one or more options specified search rules, specify the number of search rules, find the command will execute and operate, that is, all listed in the search for rules that must be met.
Find command can be used in expressions
-Name filename search for files matching the specified file name, meta characters if placed inside the quotation marks can also be used
-Size [+|-] n search for the file size is greater than (+ n) or less than (-n) of the paper, n behalf of 512 byte blocks
-Atime [+|-] n search access to more than (+ n) or less (-n) days of the file
-Mtime [+|-] n search modified for more than (+ n) or less (-n) days of the file
-User loginID search for the name of all files belonging to loginID
-Type search for the file type specified, such as the d directory, f file
-Perm search with the specified file access permission
Find-print instructions to print the current path to the terminal screen, which is the default setting
find command can be used in action
-Exec command (); automatically find the file for each implementation of the specified command, must contain the string (); used to tell the find command for each file specified by the search rules for the implementation of command, must be a space before;
-Ok command (); exec interactive way, the results of the find command asks for confirmation when the command;
The following example find command functions:
$ Find /-name core
$ Find ~-name core-exec rm ();
$ Find.-Mtime +90
$ Find ~-size +57
$ Find / usr-name '* tif'
Compare the differences between files
Comparison of text using the cmp command differences between
Cmp command can be easily identified using the difference between the text. Only when the text file does not only print the results while cmp command, without any results, indicates that the file exactly the same.
cmp command for each file one byte a byte comparison, if the file had different, cmp command prints out the difference between the number of bytes of file and line number, then stopped in a different line. This command can compare binary files and ASCII files.
Command format
cmp filename1 filename2
$ Cmp fruit fruit2
fruit fruit2 differ: char 27, line 5
Output shows the two files the first time a different location, in the above example, line 5 in the first 27 characters.
Comparison of text using the diff command the difference between
Another diff is used to compare the differences between the command text. The command's result is a text file line by line show the differences between the two, as you edit a file to make the file and another file the same guidance.
Command format
diff-option filename1 filename2
Listed below can be used in the diff options:
-I Ignore case, for example, A is equal to a
-C generate a list of differences. Results file name and create date, the next is a group of stars, and then display the number of lines file1 file, file1 in the actual line, and file2 in front of different lines (-) label. Next is the file2 file lines, file2 in the actual line, with different line in front of file1 (+) tags.
Use with-c option of diff command
When using with the command diff-c option, the results shown in three parts.
The first part of the show to compare the file name, creation date, and then with a string of asterisks
The second part file1 file display, showing the number of lines the file file1, file1 in the actual line, and file2 different line in front of (-) label.
The third part file2 file shows the number of rows displayed file file2, file2 in the actual line, with a different line in front of file1 (+) tags.
For example, execute the following command to use diff to compare files:
$ Cat fruit
lemon
orange
apple
banana
pear
mango
tomato
pomegranate
$ Cat fruit2
lemon
orange
apple
banana
tomato
guava
mango
pomegranate
$ Diff-c fruit fruit2
backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp fruit Wed Jan 12 15:50:36 2000
--- Fruit2 Wed Jan 12 15:51:04 2000
backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp
backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp 2,8 backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp
orange
apple
banana
- Pear
- Mango
tomato
pomegranate
--- 2,8 ----
orange
apple
banana
tomato
+ Guava
+ Mango
pomegranate
$
Data sorting
sort command-line arrangement of text and print the file output to the screen. Sort command provides a quick alphabetical or numerical order organizational data. Default mode, sort order to use white space to separate different fields in the file.
Command format
sort-options filenames
Options
Available options include the definition of the type of order, starting from which the domain sort.
Digitally sort (+|-) n to n-separator start (+ n), end (-n) of the domain order
-R reverse sort
-F Ignore case
+ NM arranged the first three characters in domain name of the month as an abbreviation
-D arranged in directory order, consider only letters, numbers and spaces. Other characters ignore
-O filename output file to the specified filename
-B start and end of a limited sort domain omit the space character
-T char Use char as the field separator character, if not specified, the space character is used as the default parameter
Use the sort command with different options
The following example shows how to use the sort command with different options:
$ Cat fileA
Annette 48486
Jamie 48481
Fred 48487
Sondra 48483
Janet 48482
$
$ Sort fileA
Annette 48486
Fred 48487
Jamie 48481
Janet 48482
Sondra 48483
$ Sort +1 n fileA
Jamie 48481
Janet 48482
Sondra 48483
Annette 48486
Fred 48487
The first command to use cat command to display the contents of the file, the first alphabetical sort order, from the first character of each line start.
The second sort order by number field on the second order (use +1 to skip a delimiter syntax).
In a file in the different domains using the sort command
The following example describes how a file using the sort command in different domains:
$ Ls-ld f *> list
$ Cat list
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers_6
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.1
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.2
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.3
-Rw-r - r - 1 user1 staff 1696 Feb 22 14:51 file1
-Rw-r - r - 1 user1 staff 156 Mar 1 14:48 file2
-Rw-r - r - 1 user1 staff 218 Feb 22 14:51 file3
-Rw-r - r - 1 user1 staff 137 Feb 22 14:51 file4
-Rw-r - r - 1 user1 staff 56 Feb 22 14:51 fruit
-Rw-r - r - 1 user1 staff 57 Feb 22 14:51 fruit2
$ Sort-rn +4 list-o num.list
$ Cat num.list
-Rw-r - r - 1 user1 staff 1696 Feb 22 14:51 file1
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers_6
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers
-Rw-r - r - 1 user1 staff 218 Feb 22 14:51 file3
-Rw-r - r - 1 user1 staff 156 Mar 1 14:48 file2
-Rw-r - r - 1 user1 staff 137 Feb 22 14:51 file4
-Rw-r - r - 1 user1 staff 57 Feb 22 14:51 fruit2
-Rw-r - r - 1 user1 staff 56 Feb 22 14:51 fruit
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.3
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.2
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.1
$ Sort +5 M +6 n list-o update.list
$ Cat update.list
-Rw-r - r - 1 user1 staff 56 Feb 22 14:51 fruit
-Rw-r - r - 1 user1 staff 57 Feb 22 14:51 fruit2
-Rw-r - r - 1 user1 staff 137 Feb 22 14:51 file4
-Rw-r - r - 1 user1 staff 218 Feb 22 14:51 file3
-Rw-r - r - 1 user1 staff 1696 Feb 22 14:51 file1
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.1
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.2
-Rw-r - r - 1 user1 staff 0 Feb 25 12:54 file.3
-Rw-r - r - 1 user1 staff 156 Mar 1 14:48 file2
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers
-Rw-r - r - 1 user1 staff 218 Mar 1 11:36 feathers_6
$
The first example of the ls command output stored in the file list, and then use the cat command to view the file contents.
The first sort order on the fifth domain to generate a reverse, digital sequencing, sorting the results saved to file num.list in.
The second sort order of the file list of domains 6 and 7 for multi-sort options +5 M domain on the first six month of entries alphabetically, +6 n on 7 field days and a secondary sort in numerical order, all Sort results stored in update.list file.
Search the text of the document
Solaris operating environment provides a series of commands in one or more files used to search for a specific character patterns. Model can be a single character, string, word or sentence.
By definition, a search for matching characters in the same pattern as regular expression characters. Used to search for regular expressions command called grep, egrep and fgrep.
Grep command in a file search for regular expressions, regular expressions all contain the line to send the standard output.
Egrep and fgrep command is a variant of grep command, known as the rapid expansion of grep and grep.
Using the grep command
grep command is used in one or more files to search for character mode or regular expression to each line contains the specified character mode line output to the screen, but this does not change the contents of the file.
Command format
grep-options pattern filenames
Options
-I Ignore case, uppercase and lowercase letters that are the same
-L just lists the file name with a matching line
-N added before each line line number
-V lists all lines that do not match
-C only contains the pattern of the lines listed
Regular expression meta-characters
grep command supports a few dollars to further define the search mode character. Here are a few basic element commonly used character
^ Line start identifier '^ pattern' matches all lines that begin with pattern
$ Line end of the identifier 'pattern $' pattern match to the end of the line for all
. Matching a character 'p ... .. n' match all contain the five characters followed by p at the beginning, followed with a line of characters
* [A-z] * matches lowercase characters
[] Matches the pattern of a single character '[pP] attern' Pattern matching including the pattern and the pattern of lines
[^] Is not pattern matching a single character '[a ^ m] attern' match to a to m at the end of all rows attern
Using the grep command to search for examples of regular expressions
The following example uses the grep command to search for regular expressions to search files / etc / group contains a pattern in the root of all the line, execute the following command:
$ Grep-n root / etc / group
1: root:: 0: root
3: bin:: 2: root, bin, daemon
4: sys:: 3: root, bin, sys, adm
5: adm:: 4: root, adm, daemon
6: uucp:: 5: root, uucp
7: mail:: 6: root
8: tty:: 7: root, tty, adm
9: lp:: 8: root, lp, adm
10: nuucp:: 9: root, nuucp
12: daemon:: 12: root, daemon
$
To search for a file / etc / group does not contain all of the root mode of the line, execute the following command:
$ Grep-v root / etc / group
other:: 1:
staff:: 10:
nobody:: 60001:
noaccess:: 60002:
sun4prog:: 100:
funda:: 105:
cprg:: 170:
csh:: 180:
sysadmin:: 14:
fire:: 20:
sl300:: 30:
java:: 190:
cplus:: 700:
sysint:: 1000:
internals:: 2000:
sunnetmgr:: 3000:
OpenWindows:: 5000:
SAE:: 5500:
guest:: 20000: guest, kbrown
$
Just search for file name contains the model root, execute the following command:
$ Cd / etc
$ Grep-l root group passwd hosts
group
passwd
$
Need in the directory / etc all the files in search mode the, only listed the matching pattern and The file name, perform the following example:
$ Cd / etc
$ Grep-li the backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp
$ Cd / etc
$ Grep-li the backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp
aliases
asppp.cf
dacf.conf
device.tab
devlink.tab
dgroup.tab
fmthard
format