Shell Common Commands

Source: Internet
Author: User
Tags alphabetic character control characters echo command egrep

"Set Environment variables"
1. Use the ENV command to display all environment variables
$ env
2. Use the echo command to view a single environment variable
$ echo $PATH
3. Set a new environment variable
$ export hello= "hello!"
Syntax: Export [-fnp][variable name]=[variable setting value]
Parameters:
-F represents the function name in [variable name].
-n Deletes the specified variable. The variables are not actually deleted, but are not exported to the execution environment of the subsequent directives.
-p lists all the environment variables that the shell assigns to the program.
Note: Export only temporarily modifies environment variables, exits the current shell, and sets the environment variable to expire.

If you want to permanently change the environment variables, edit the relevant files directly, modify and reload.
/etc/profile: The environment setting information for each user of the system, which is executed when the user logs on for the first time. and collect the shell settings from the configuration file of the/ETC/PROFILE.D directory.
/ETC/BASHRC: Execute this file for each user running the bash shell.
~/.bash_profile: Each user can use this file to enter shell information that is specific to their own use, which is performed only once when the user logs on.
~/.BASHRC: This file contains bash information dedicated to your bash shell, which is read when you log in and every time you open a new shell.

Note: In addition, the/ETC/BASHRC file is used for all users, while ~/.BASHRC acts on the current user, inheriting variables in/ETC/BASHRC, they are \ "parent-child \" relationships, and if the variables conflict, the ~/.BASHRC prevail.

Reference: http://blog.chinaunix.net/uid-25533439-id-3291246.html

"File Security and Permissions"
1. Create a file with touch: $ touch temp;
2. View catalogue: LS-[a|l| ...]
Permission bit for file:
RWX: Top three, file owner readable, write, execute
rw-: Middle three bits, group user readable, write
r--: Last three bits, other users are only readable
File type bit:
D directory.
L Symbolic Link (pointing to another file).
s socket file.
B-block device files.
C-character device file.
P Name the pipe file.
-Ordinary files, or more accurately, do not belong to several types of files.

3, chmod change the file permissions
chmod [who] operator [permission] filename
Who
U file is a master permission.
Group G user Rights.
o Other user rights.
A All users (file owner, group user, and other user).
operator
+ Add permissions.
-Remove Permissions.
= Set permissions.
Permission
R Read permission.
W Write permission.
X Execute permissions.
S file owner and group Set-id.
t Sticky bit *.
L lock files so that other users cannot access them.
U,g,o for files belonging to the owner, the group of users and other users of the operation.

Note: Set permissions using absolute mode
chmod [mode] File
File owner: R w x:4 + 2 + 1
Group of users: R W x:4 + 2 + 1
Other users: R W x:4 + 2 + 1
Example: chmod 744 test:rwx r--R-gives the file permission to read, write, and execute, and all other user-readable permissions.

Note: Directory permissions represent different meanings than files
R: You can list the files in this directory
W: You can create or delete files in this directory
X: Can search or enter this directory

4. Using Chown to modify the owner
Chmod-r-H owner File
-The R option means that the same action is performed on all the files under all subdirectories.
The-h option means that changing the owner of a symbolic link file does not affect the target file that the link points to.

5, Umask
When you initially log on to the system, the Umask command determines the default mode in which you create the file. This command is actually the exact opposite of the chmod command. Your system administrator must set a reasonable umask value for you to ensure that you create a
File has the desired default permissions to prevent other non-group users from having write access to your files.

Find
Find
Find Pathname-options [-print-exec-ok]
Command parameters:
The directory path that the Pathname:find command looks for, to represent the current directory, and/to represent the system root directory.
The-print:find command outputs the matched file to standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of '
Command ' {} \;, note the space between {} and \;
-ok: The same as-exec, except that the shell given by this parameter is executed in a more secure mode
command, a prompt is given before each command is executed, allowing the user to determine whether to execute.

Options:
-name: Finds files by file name.
-perm: Finds files according to file permissions.
-user: Finds files according to the owner of the file.
-mtime-n +n: Finds files according to the time the file was changed,
-n means that the file change time is now less than n days, and +n indicates that the file change time is now N days ago.
-type find a file of a certain type.
-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.
Example:
In the/logs directory, look for files that change time before 5th and delete them:
$ find Logs-type f-mtime +5-exec rm {} \;

Xargs
When you use the-EXEC option of the Find command to process a matching file, the Find command will match all files to the
to exec execution. However, some systems have a limit on the length of the command that can be passed to exec, so that the find
After the command runs for several minutes, an overflow error occurs. The error message is usually "parameter column too Long" or "parameter column overflow".
This is where the Xargs command is used, especially with the Find command.
The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time and
Not all, not like the-exec option. So it can first I process a portion of the file that gets first, then the next batch,
And so on.

Example: Find the Memory information dump file (core dump) across the system and save the results to/tmp/core.log
In the file
$ find/-name "core"-print | Xargs echo "" >/tmp/core.log

"Input and Output"
1, echo command output escape character and variable.
Example:
# echo-e "\007your home is $HOME and you're connected on ' TTY '"
Your home is/root, you is connected ON/DEV/PTS/1
Note:
\007 or \a can let the terminal ring a sound
Displays the $home directory,
And you can have the system execute the TTY command (note that the command is caused by a sign in the upper-left corner of the keyboard, a grave accent in French,
Not single quotes).
\ n indicates a newline, \ t represents a tab

2. Redirect: redirect symbol > and >>
Example:
Redirect and overwrite the original file
$ echo "The log files has all been done" > myfile
Append to the end of a file, without overwriting the original content
$ echo "$LOGNAME carried them out at ' date '" >>myfile

3. Cat: Displays the contents of the file, creates a file, and can also use it to display control characters.
Note: The file will not stop at the page break; it will display the entire file at a moment. Therefore, you can use the more command or
The output of the cat command is passed through the pipeline to another command with paging, using the command less file to achieve phase
The same functionality.
Example: $ cat MyFile | More

1) display files named file:
$ cat File
2) show File1,file2,file3 of these three files
$ cat File1 file2 File3
3), create a file containing the above three files, named Bigfile, you can redirect the output to a new file
In
$ cat file1 file2 file3 > Bigfile
4) If there are no parameters in the cat's command line, each line entered is immediately output to the screen by the cat command
5) New File
$cat >myfile

4, Tee: Read the standard input data, and output its contents into a file.
Syntax: Tee [-ai][--help][--version][file ...]
Note: The tee instruction reads data from a standard input device and outputs its contents to a standard output device while saving
into a file. We can use tee to store the imported data into a file, or even save several files at a time.
Parameter:-a attaches to the face of the existing file rather than overwriting it. If the name of the file given the tee instruction already exists,
Presets overwrite the contents of the file. With this parameter, the data will be added to the top of the file content without deleting the original
Capacity.
-I ignores interrupt signal
--help online Help
--version displaying version information

Example:
1) List The contents of the text file Slayers.story, while copying 3 copies, the file names are ss-copy1,
Ss-copy2, Ss-copy3:
$ cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

2) List the current directory and put the results in myfile
$ls-L |tee myfile

5. Pipeline
You can pass the output of one command to another command as input through a pipeline, using the vertical bar "|" Said.
Note: File descriptor (there are actually 12 file descriptors in the system)
Input file-Standard input 0: It is the input to the command, the default is the keyboard, or it can be the output of a file or other command.
Output file-standard Output 1: It is the output of the command, the default is the screen, or it can be a file.
Error output file-standard error 2: This is the output of the command error, the default is the screen, the same can be the file.
If the file specifier is not specifically specified, the command uses the default file descriptor (terminal).
Example:
Frequently used file redirection commands
Command > file redirect the standard output to file
Command >> file appends the standard output to file
Command 1 > file redirect the standard output to file
Command > file 2>&1 redirect standard output and standard error to file
Command 2 > file redirects the standard error to file
Command 2 >> file appends the standard error to file
The command < file1 >file2 command commands the file1 file as the standard input, with the File2 file as the standard output
command < filename commands command with file as standard input
Command << delimiter is read from the standard input until the delimiter delimiter is encountered
Command <&m file descriptor m as standard input
Command >&m standard output redirected to file descriptor M
Command <&-to turn off standard input

"Text Filtering tool grep"
Linux has grep, Egrep, Fgrep

Grep:
The traditional grep program, in the absence of parameters, only output sentences conforming to the re string, the common parameters are as follows:
-V: Reverse mode, only output the "no" RE string sentence;
-R: Recursive mode, which can simultaneously process files in all levels of subdirectories;
-Q: Silent mode, does not output any results (except stderr);
-I: Ignore case;
-W: Whole word comparison, similar to \<word\>;
-N: Output line number at the same time;
-C: Output only matches the number of rows;
-L: Only the name of the file that matches the match is output;
-O: Outputs only strings that conform to re;
-e: Switch to Egrep;

Egrep
The expanded version of grep improves the operation of many traditional grep that cannot or is inconvenient.
Fgrep
Without re-processing, the expression is treated as a generic string only.

Single quote double quotation mark
When you enter a string parameter in the grep command, it is best to enclose it in double quotation marks, and use single quotes when calling pattern matching.
For example: "string", there are two reasons to do this
One is to prevent it from being misunderstood as a shell command, and two is a string that can be used to look up multiple words.
You should also use double quotation marks when calling variables, such as grep "$MYVAR" filenames, and if not, no results will be returned.

1. Query the word "ABC" in All Files
$ grep "ABC" *
2. Precise matching
$grep "Abc\>" Abc.txt
Return rows that contain only ABC
3, with the regular match, to find the number of empty rows
$ Grep-c ' ^$ ' abc.txt

"Regular expression (RE)"
1. Match single character with period
Matches any single character except for "\ r \ n".
Note, ". "Allows any character in the ASCII set to be matched, or as a letter, or as a number.

2. Match the string or character sequence at the beginning of the line with ^
^ only allow to match characters or words at the beginning of a line
Example: The fourth character at the beginning of a line is a:^ ... a

3. Match strings or characters at the end of a row
$ with ^ In contrast, it matches a string or character at the end of a line, and the $ symbol is placed after the matching word. Assume that you want to match the
All lines of the word com end: com$

4. Use * to match single character or its repeating sequence in a string
Matches the preceding sub-expression 0 or more times (greater than or equal to 0 times). For example, zo* can match "Z", "Zo" and "Zoo". * Equivalent to {0,}.
Similar to the "*" number
"+" number: matches the preceding subexpression one or more times (greater than or equal to 1 times). For example, "zo+" can Match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
“?” Number: Matches the preceding subexpression 0 or one time. For example, "Do (es)?" You can match "do" in "do" or "does".?

5, using \ Shielding the meaning of a special character
Special characters: $. ' " *[]^|\+?

6. Use [] to match a range or set
Use "-" to denote a range of strings, indicating that the string range starts with the character "-" to the left of "-" and to the "-" right word
End of the symbol.
Example:
1) any number [0-9];
2) Any lowercase letter [a-z];
3) match any non-alphabetic character: [^a-za-z];
4) match any non-numeric character: [^0-9];

7. Number of occurrences of matching pattern results using \{\}
Use * to match all matching results any time, but if you specify the number of times, you should use \{\}, this mode has three kinds of
form, namely:
pattern\{n\} match pattern appears n times.
Pattern\{n,\} The matching pattern appears at least n times.
PATTERN\{N,M} The matching pattern occurs between N and M times, and N,m is any integer in 0-255.
Example: Match letter A appears two times and ends with B: a\{2\}b

Important: Not all of the regular characters are supported in all languages, so be careful when you actually use them.
Because. []^$ All languages are supported, so equivalent substitution can be made:
Equivalent:
?, *,+,\d,\w are equivalent characters
? Equivalent to match length {0,1}
* Equivalent to match length {0,}
+ equivalent to match length {1,}
\d equivalent to [0-9]
\w equivalent to [a-za-z_0-9]


Reference: HTTP://BAIKE.BAIDU.COM/LINK?URL=2_39TB0YXCEYDNFSOVDMQM6_JYE09JX8CDD4GKLO6X3JI0RJHBCJ9EHQNP34JUD-XL53VCTIWLNYGDA48RAJHQ

"Awk and sed"
Awk, SED, and grep, commonly known as the Three Musketeers under Linux, have a lot of similarities, but they also have their own features, similar in that they can all match text, where SED and awk can also be used for text editing, and grep doesn't have that function. SED is a non-interactive, character stream-oriented editor, and Awk is a pattern-matching programming language because its main function is to match text and process it, while it has some programming language syntax, such as functions, branching loops, variables, and so on, of course, compared to our common programming languages, Awk is relatively straightforward.

With awk, we can do the following things:
Treats a text file as a text database consisting of fields and records;
The ability to use variables in the process of manipulating text databases;
Ability to use mathematical operations and string manipulation
The ability to use common programming structures, such as conditional branching and looping;
capable of formatting output;
ability to customize functions;
Ability to execute UNIX commands in an awk script;
Ability to handle the output of UNIX commands;

Sed is essentially an editor, but it is non-interactive, which is different from vim, and it is a character stream oriented, and the input character stream is output after sed processing. These two features make SED a very useful processing tool under the command line.
The process of SED is simplified after this:
Reads a new line of content into the cache space;
Remove the first instruction from the specified operation instruction to determine if the pattern is matched;
If it does not match, the subsequent edit commands are ignored, and the next command is returned to the 2nd step;
If a match is performed, subsequent editing commands are executed against the cached row, and after completion, go back to the 2nd step and continue to remove the next instruction;
When all instructions are applied, output the contents of the cache line, and go back to the 1th step to continue reading the next line of content;
When all the lines have been processed, finish.

Reference:
http://blog.csdn.net/a81895898/article/details/8482387
http://blog.csdn.net/a81895898/article/details/8482333

This article is from the "Night" blog, be sure to keep this source http://icyore.blog.51cto.com/8486958/1596280

Shell Common Commands

Related Article

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.