Linux Commands (ii)

Source: Internet
Author: User
Tags bz2 print format file permissions

1. cd command

This is a very basic, is also a common need to use the command, it is used to switch the current directory, its parameters are to switch to the path of the directory, can be an absolute path, can also be a relative path. Such as:

    1. Cd/root/docements # Switch to directory/root/docements
    2. cd./path # Switch to the path directory in the current directory, "." Represents the current directory
    3. Cd.. /PATH # Switch to the path directory in the upper directory, "..." Represents a previous level of directory
2. ls command

This is a very useful view of the file and directory commands, List of meaning, it has a lot of parameters, the following list some of my commonly used parameters, as follows:

    1. -L: Lists Long data strings, including file attributes and permission data, etc.
    2. -A: Lists all the files, together with hidden files (files that begin with.) (common)
    3. -D: Lists only the directory itself, not the file data for the directory
    4. -H: List the file capacity in a more readable way (GB,KB, etc.)
    5. -R: Listed along with the contents of the subdirectory (recursively listed), equal to all files in that directory will be displayed

Note: These parameters can also be used in combination, the following two examples:

    1. Ls-l #以长数据串的形式列出当前目录下的数据文件和目录
    2. LS-LR #以长数据串的形式列出当前目录下的所有文件
3. grep command

This command is often used to parse a row of information, if there is information we need, the row is displayed, the command is usually used with the pipeline command, for the output of some commands to filter processing, etc., its simple syntax is

    1. grep [-ACINV] [--color=auto] ' Find string ' filename

Its common parameters are as follows:

    1. -A: Find data in a binary file as a text file
    2. -C: Calculate the number of times to find ' find string '
    3. -I: Ignore case differences, that is, case is considered the same
    4. -V: Reverse selection, which shows the line without the ' Find string ' content
    5. For example
    6. # Remove the line containing Manpath in the file/etc/man.config and add color to the Found keyword
    7. grep--color=auto ' MANPATH '/etc/man.config
    8. # put the output of ls-l with the letter file (case-insensitive) content output
    9. Ls-l | Grep-i file

grep [Options]

[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

grep command to use a simple instance
$ grep ' test ' d*
Displays all rows that contain test in a file that begins with D.
$ grep ' test ' AA bb cc
Displays the line that matches test in the aa,bb,cc file.
$ grep ' [a-z]\{5\} ' AA
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.
$ grep ' w\ (es\) T.*\1′aa
If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have "\" number to escape, directly written as ' W (es) t.*\1′ on it.

grep command to use complex instances
Suppose you are searching for a file with the string ' magic ' in the '/usr/src/linux/doc ' directory:
$ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* How does I use the Magic sysrq key?
Where the file ' Sysrp.txt ' contains the string and discusses the functionality of SYSRQ.
By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Explicit Requirements Search subdirectories: grep-r
or Ignore subdirectories: grep-d Skip
If you have a lot of output, you can go through the pipe to read it on ' less ':
$ grep magic/usr/src/linux/documentation/* | Less
This makes it easier for you to read.

One thing to note is that you need to provide a way to filter the file (search for all files in *). If you forget, ' grep ' will wait until the program is interrupted. If you are experiencing such a situation, press <ctrl c>, and then try again.

Here are some interesting command-line arguments:
Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
Grep-l pattern Files: Lists only the matching file names,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts display [number] lines, respectively,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Grep-n Patternfiles to display line number information

Grep-c patternfiles to find total rows

Here are some special symbols for searching:
\< and \> each mark the beginning and end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep ' \<man ' matches ' manic ' and ' man ', but not ' Batman ',
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to a matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,

4. Find command

Find is a very powerful search-based command, relatively speaking, its use is relatively complex, parameters are also more, so here will be to classify them, its basic syntax is as follows:

  1. Find [PATH] [option] [action]
  2. # time-related parameters:
  3. -mtime N: = n is a number, meaning a file that has been changed in "One Day" before the nth day;
  4. -mtime +n: Lists the file names that were changed before n days (excluding the N-day itself);
  5. -mtime-n: Lists filenames that have been changed within n days (including n days themselves);
  6. -newer file: List file names that are newer than file
  7. For example
  8. Find/root-mtime 0 # Find files that have changed within the current directory today
  9. # parameters related to user or user group name:
  10. -user Name: List files with file owner name
  11. -group Name: List files that belong to the user group named name
  12. -uid N: List file owner as User ID n
  13. -gid N: Lists files that belong to the user group whose user group ID is n
  14. For example
  15. Find/home/ljianhui-user Ljianhui # Find the owner-Ljianhui file in the directory/home/ljianhui
  16. # Parameters related to file permissions and names:
  17. -name FileName: Find the file named filename
  18. -size [+-]size: Find a file larger than size (+) or small (-)
  19. -tpye Type: Find file of type of file, the value of type is mainly: General file (f), device file (b, C),
  20. Directory (d), connection file (L), socket (s), FIFO pipeline file (p);
  21. -perm mode: Find file permissions just equal to the mode file, mode is represented by a number, such as 0755;
  22. -perm-mode: Find file permissions must all include the mode permission of the file, mode is represented by a number
  23. -perm +mode: Find file Permissions file that contains the permissions of either mode, mode is represented by a number
  24. For example
  25. Find/-name passwd # Finding files with file name passwd
  26. Find. -perm 0755 # 0755 file to find file permissions in the current directory
  27. Find. -size +12k # Find files larger than 12KB in the current directory, note that C means byte
5. CP Command

This command is used to copy the file, copy the meaning, it can also copy multiple files to a directory at one time, its common parameters are as follows:

    1. -A: Copy the attributes of the file together
    2. -P: Copied along with the properties of the file, not by default, similar to-a, often used for backup
    3. -I: If the target file already exists, the action will be asked before overwriting
    4. -R: Recursive continuous Replication for directory replication behavior
    5. -U: The destination file is copied when it differs from the source file

For example:

    1. Cp-a file1 file2 #连同文件的所有特性把文件file1复制成文件file2
    2. CP file1 file2 file3 dir #把文件file1, file2, file3 copy to directory dir
6. MV Command

This command is used to move a file, directory, or rename, and its common parameters are as follows:

    1. -f:force mandatory Meaning, if the target file already exists, will not ask and directly overwrite
    2. -I: If the target file already exists, you will be asked to overwrite
    3. -U: Updates if the target file already exists and is newer than the target file

Note: This command can move a file or multiple files one folder at a time, but the last target file must be "directory".

For example:

    1. MV File1 file2 file3 dir # move the file file1, File2, file3 to the directory dir
    2. MV File1 file2 # Rename the file file1 to File2
7. RM Command

This command is used to delete files or directories, between remove, and its common parameters are as follows:

    1. -F: Force means ignoring files that do not exist and warning messages are not present
    2. -I: Interactive mode, ask the user whether to operate before deleting
    3. -R: Recursive delete, most commonly used for directory deletion, it is a very dangerous parameter

For example:

    1. Rm-i File # To delete files, ask for the action before deleting
    2. RM-FR dir # force removal of all files in directory dir
8. PS Command

This command is used to select and output the process run at a point in time, and its common parameters are as follows:

    1. -A: All processes are displayed
    2. -A: All processes not related to terminal
    3. -U: Related processes for effective users
    4. -X: Generally used in conjunction with a parameter to list more complete information
    5. -L: Longer, the PID information is listed in more detail

In fact, we just need to remember the general use of PS command parameter collocation can be, they are not many, as follows:

    1. PS aux # View all process data for the system
    2. PS Ax # View all processes not related to terminal
    3. Ps-la # View all process data for the system
    4. PS AXJF # View together with a subset of process tree states
9. Kill command

This command is used to send a signal to a job (%jobnumber) or to a PID (number), which is typically used with the PS and jobs commands, with the following basic syntax:

    1. Kill-signal PID

The common parameters of signal are as follows:

Note: The first number is the signal code, use can be used to replace the corresponding signal code.

    1. 1:sighup, start the terminated process
    2. 2:sigint, equivalent to input CTRL + C, interrupts the process of a program
    3. 9:sigkill, forcing the interruption of a process
    4. 15:sigterm to terminate the process with normal end-of-process mode
    5. 17:sigstop, which is equivalent to input CTRL + Z, pauses a process

For example:

    1. # at the end of the normal process to finally the first background work, you can use the Jobs command to view the background of the first worker process
    2. Kill-sigterm%1
    3. # re-change process ID to PID process, PID can be filtered using the PS command with the command of the command plus grep
    4. Kill-sighup PID
10. Killall Command

This command is used to send a signal to a process initiated by a command whose general syntax is as follows:

    1. Killall [-iie] [command name]

It has the following parameters:

    1. -I: interactive meaning, if need to delete, will ask the user
    2. -E: Indicates that command name is followed by the same, but command name cannot exceed 15 characters
    3. -I: Command name ignores case
    4. For example
    5. Killall-sighup syslogd # reboot SYSLOGD
11. File Command

This command is used to determine the basic data of files after the file command, because the type of the file under Linux is not the suffix of the future, so this command is very useful for us, its usage is very simple, the basic syntax is as follows:

    1. File filename
    2. #例如:
    3. File./test
12. Tar command

This command is used to package the file, which is not compressed by default and, if specified, it also invokes the appropriate compression program (such as Gzip and bzip) for compression and decompression. Its common parameters are as follows:

    1. -C: New package file
    2. -T: See what file names are included in the contents of the packaged files
    3. -X: Unpacking or decompression function, can be used with-C (uppercase) to specify the extracted directory, note-c,-t,-x cannot appear in the same command
    4. -J: Compression/decompression via BZIP2 support
    5. -Z: Compression/decompression with GZIP support
    6. -V: Displays the file name being processed during the compression/decompression process
    7. -F filename:filename for files to be processed
    8. -C dir: Specify a directory for compression/decompression dir

The above commentary may have knocked you out of the way, but usually we just need to remember the following three commands:

    1. Compression: Tar-jcv-f filename.tar.bz2 The name of the file or directory to be processed
    2. Enquiry: Tar-jtv-f filename.tar.bz2
    3. Unzip: Tar-jxv-f filename.tar.bz2-c to extract the directory

Note: The file name is not set to end in the suffix tar.bz2, this is mainly to illustrate the use of the compression program for BZIP2

13. Cat Commands

This command is used to view the contents of a text file, followed by the name of the file to be viewed, and is typically used in conjunction with more and less to allow you to view the data one page at a time. For example:

    1. Cat text | Less # View the contents of the text file
    2. # Note: This command can also use less text instead
14. Chgrp Command

This command is used to change the user group to which the file belongs, it is very simple to use, its basic usage is as follows:

    1. CHGRP [-R] Dirname/filename
    2. -R: Recursive persistent changes to all files and subdirectories
    3. For example
    4. Chgrp users-r./dir # Recursively modifies the user group for all files and subdirectories under the Dir directory to users
15. Chown Command

The command is used to change the owner of the file, using the same method as the CHGRP command, except that the modified file attributes are different and are no longer detailed.

16. chmod command

This command is used to change the permissions of a file, as follows:

    1. chmod [-r] XYZ file or directory
    2. -R: Continuous change of recursion, that is, all files under subdirectories are changed

At the same time, chmod can also use the U (user), G (group), O (Other), a (all) and + (join),-(delete), = (set) with the rwx to make changes to the permissions of the file.

    1. For example
    2. chmod 0755 File # Changes files permissions to-rxwr-xr-x
    3. chmod g+w File # Adds a user group writable permission to files ' permissions
18. Vim Command

This command is primarily used for text editing, which takes one or more file names as parameters, opens if the file exists, and creates a file with the file name if it does not exist. Vim is a very useful text editor, it has a lot of very useful commands, here no longer say. You can download a detailed description of VIM's common operations from here.

19. GCC Command

For a person who developed a C program with Linux, this command is very important, it is used to compile the C language source program files, compiled into an executable program, because many parameters of g++ and it very similar, so here only the parameters of the GCC, its common parameters are as follows:

    1. -o:output, used to specify the file name to generate an executable file
    2. -C: Used to generate a target file (. o) from the source file and prevent the compiler from creating a complete program
    3. -I: Increase the path of the search header file at compile time
    4. -L: Increase the path of the search static connection library at compile time
    5. -S: Generate assembly code files for source files
    6. -LM: A library of libraries named LIBM.A in the directory that represents the standard library
    7. -lpthread: Connecting NPTL-Implemented lines libraries
    8. -std=: Used to specify the version of the C language to use
    9. For example
    10. # Compile the source file test.c into executable program test according to C99 standard
    11. Gcc-o Test Test.c-lm-std=c99
    12. #把源文件test. c conversion to the appropriate assembler source file Test.s
    13. Gcc-s test.c
20. Time Command

This command is used to measure the execution time of a command (that is, a program). It's very simple to use, just like you would normally enter a command, but add a time to the front of the command, for example:

    1. Time./process
    2. Time PS aux

After the program or command has finished running, it outputs three times at the end, respectively:

User: CPU time, the user CPU time that the command executes, that is, the command executes the sum of time in the user state;

System: CPU time, the system CPU time that the command executes, that is, the command executes the sum of time in the kernel mentality;

Real: The actual time, from the command line to start execution to the end of the elapsed time;

Note: The sum of CPU time and system CPU time is CPU time, that is, the total amount of time the command consumes CPU execution. The actual time is greater than the CPU time, because Linux is a multitasking operating system, often when executing a command, the system also handles other tasks. Another problem to be aware of is that even though the same command is executed every time, the time spent is not the same, and the time spent is related to the operation of the system.

grep, sed and awk are quite useful!

Gerp Find, sed edit, awk analyzes and processes it based on content.

awk (Key Words:Analysis&processing)Parsing of one line of processing awk ' condition type 1{action 1} condition type 2{action 2} ' filename, awk can also read standardinput from the previous instruction
Unlike SED, which is often used for a whole line of processing, awk prefers to divide a row into several "fields" (areas), with the default delimiter being the SPACEBAR or TAB key
For example:
Last-n 5 | awk ' {print ' \ t ' $ ' $} ' there is no space between "\ t" in curly braces here, but it is best to add a space, and note that "\ T" is double-quoted, because the contents are in single quotes
$ A represents the entire row, representing the first region, and so on
The processing process for awk is:
1. Read the first line and fill in the first line with the variable ... In equal variables
2. Perform the action according to the condition limit
3. Next line of execution
As a result, awk is processed one line at a time, and the smallest unit processed at a time is a region
There are also 3 variables, NF: The number of fields processed per row, NR currently processed to the first few lines of FS current delimiter
Logic judgment > < >= <= ==!==, assignment direct use =
cat/etc/passwd | awk ' {fs= ': '} $3<10 {print ' \ t ' $ ' $} ' first defines the delimiter as:, then judge, watch, judge not write in {}, then perform the action, fs= ":" This is an action, assignment action, not a judgment, so do not write in {}
Begin END, which gives the programmer an initialization and finishing work, the actions listed after begin will be executed within {} Before awk begins scanning the input, and the actions within end{} will be executed after the input file has been scanned.
awk '/test/{print NR} ' ABC prints the line number of the line with test, note that regular expressions can be used between//
Within awk {}, you can use if else, for (i=0;i<10;i++), i=1 while (I&LT;NF)
It can be seen that many of Awk's uses are equivalent to the C language, such as the "\ T" delimiter, print format, if, while, for, and so on

Awk is a fairly complex tool, and when it's really used, add it. (Picture of the tool)

sed ( keyword : edit ) text Editing tool with behavioral units sed can directly modify the file, but it is generally not recommended to do so, you can analyze Standardinput
Basic working methods: sed [-NEF] ' [action] ' [input text]
-N: Quiet mode, in general SED usage, data from stdin is generally listed on the screen, and if the-n parameter is used, only the line that is processed by SED is listed.
-E: Multiple edits, such as you want to delete a row at the same time, and want to change the other rows, then you can use Sed-e ' 1,5d '-e ' s/abc/xxx/g ' filename
-F: First, the SED action is written in a file, and then through the Sed-fscriptfile can directly perform the SED action within the ScriptFile (no experimental success, not recommended)
-I: Direct editing, this time is really changing the contents of the file, and everything else just change the display. (Not recommended)
Action:
A is added, a string is followed by a, and the string appears on a new line. (Next line)
C supersedes, C-strings, which can replace rows between n1,n2
D Delete, not after anything.
I insert, followed by a string that appears on the previous line
P prints, lists selected data, usually works with Sed-n sed-n ' 3p ' only prints line 3
s substitution, similar to the substitution in VI, 1,20s/old/new/g

[Line-address]q exit, match to a line to exit, improve efficiency

[Line-address]r matches to a line to read a file for example: sed ' 1r qqq ' ABC, note that the written text is written on the 1th line behind, that is, line 2nd

[Line-address]w file, the line to which the match is written to a file such as: Sed-n '/m/w QQQ ' ABC, reads the line with M from ABC to the QQQ file, note that this write has coverage.


Example:
sed ' 1d ' ABC deletes the first line in the ABC file, noting that all rows except the first line are displayed, because the first row has been deleted (the actual file has not been deleted, but it was deleted when it was displayed)
Sed-n ' 1d ' ABC Content is not displayed, because the line processed by SED is a delete operation, so it is not realistic.
sed ' 2, $d ' ABC removes everything from the second line to the last line in ABC, note that the $ sign in the regular expression represents the end of the line, but it does not say that the end of the line, it refers to the end of the last line, ^ begins, if no line is specified, then the first line starts with
SED $d ' ABC has only deleted the last line, because it is not specified as the end of the line, it is considered to be the last line at the end of
Sed '/test/d ' abc file all the lines with test, delete all
sed '/test/a rrrrrrr ' ABC will RRRRRR R append to all of the next line with the test line is also possible through the line sed ' 1,5c rrrrrrr ' abc
sed '/test/c rrrrrrr ' ABC will rrrrrrr replace all rows with test, of course, this can also be done by line replacement, such as sed ' 1,5c rrrrrrr ' abc



grep ( keyword : intercept ) text-gathering tool, combined with regular expression is very powerful
Main parameters []
-C: Output only matching rows
-I: Case insensitive
-H: Do not display file names when querying multiple files
-L: Only file names with matching characters are output when querying multiple files
-N: Displays matching line numbers and rows
-V: Displays all lines that do not contain matching text (I often use the remove grep itself)
Basic how to work: grep to match the content file name, for example:
grep ' test ' d* shows all rows with test in the file beginning with D
grep ' Test ' AA bb cc shows lines that contain test in the AA bb cc file
grep ' [a-z]\{5}\ ' AA displays all strings containing a string of at least 5 consecutive lowercase letters

Three application forms of the Order

grep ' characters ' file

sed ' Command ' file

awk ' condition { command} ' file

The use of regular expressions within single quotes

Linux Commands (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.