Common shell

Scene: general command output will display in the terminal, there are times when you need to execute some commands you want to save the results to a file for subsequent analysis / statistics, we need to use this time to redirect the output techniques.

>: Covering output, it will overwrite the original file contents

>>: the additional output will not overwrite the original file contents, we will continue to add to the end of the original content

2>: error output, it will overwrite the original file contents

>> 2: Error additional output will overwrite the original file contents, we will continue to add to the end of the original content

&>: The standard output and error output jointly written to the file. Covering the original content

& >>: The standard output and error output jointly written to a file (appended to the original content)

 

Redirect standard input (STDIN, file descriptor 0): Default input from the keyboard, may also be input from another file or command.

Redirect standard output (STDOUT, file descriptor 1): The default output to the screen.

Redirect the error output (STDERR, file descriptor 2): The default output to the screen.

 

Case 1: Use the output coverage (covering original language)

[root@ken ~]# vim test
[root@ken ~]# cat test
this is ken
[root@ken ~]# echo "this is oscar" > test
[root@ken ~]# cat test
this is oscar

 

Case 2: Using append output (the original text added)

[root@ken ~]# cat test
this is oscar
[root@ken ~]# echo "this is ken" >> test
[root@ken ~]# cat test
this is oscar
this is ken

 

Case 3: Using the wrong output redirection

The default output will fall on the wrong output terminal

[Root @ ken ~] # cat klkl> test # output redirection can not make use of the error output into the text
cat: klkl: No such file or directory
[Root @ ken ~] # cat klkl 2> test # 2 need> Error output redirection
[root@ken ~]# cat test
cat: klkl: No such file or directory

 

Case 4: The standard output and error output jointly written to the file

[root@ken ~]# cat klkl &> test
[root@ken ~]# cat test
cat: klkl: No such file or directory

 

Input redirection

 

Function is to redirect input directly into the command file.

Case: Lines of text statistics

[root@ken ~]# wc -l < /etc/passwd
28

 

Pipe character

 

Pipe command symbol (|) is the role of the previous command is supposed to be output to the standard input data after the normal standard screen as a command.

For example, we need to count the number of lines passwd file containing the root

[root@ken ~]# cat /etc/passwd | grep "root"  | wc -l
2

The output value of the search command is transmitted to the statistical commands, i.e., the original information to be output to the user list screen, and then to the wc command for further processing, and therefore need only to pipe character to put between the two commands

 

Tsuhaifu

 

For Linux operation and maintenance personnel, we sometimes encounter situations obviously a file name is on the lips but it just will not come. If you remember the beginning of a file of letters, would like to traverse to find out all files that start with the keyword, how does it work?

Wildcard is the universal symbol of matching information, such as

  1. An asterisk (*) on behalf of matches zero or more characters (can be 0)
  2. Question mark (?) Matches a single character on behalf of (single character must be present)
  3. Digitally [0-9] Representative matching single digit between 0 and 9 character in brackets,
  4. The letters in the brackets [abc] match is representative of a, b, c of any three characters in a character.

All the files beginning with sda View dev directory: Case 1

[Root @ ken ~] # ls / dev / sda *
/ Dev / sda / dev / sda1 / dev / sda2

 

Case 2: View dev directory beginning with sda, there is a character behind file

[Root @ ken ~] # ls / dev / sda?
/dev/sda1  /dev/sda2

After a question mark / dev / sda will not match the

 

File begins with sda, back to keep numbers under View dev: Case 3

Not match sda

[Root @ ken ~] # ls / dev / sda [0-9]
/dev/sda1  /dev/sda2

 

Case 4: View dev begin with sda, back to keep letter file

[root@ken ~]# ls /dev/sda[a-z]
ls: cannot access /dev/sda[a-z]: No such file or directory

Not match any documents also match less than / dev / sda, the back to keep up with the characters must be present in order to match.

 

Function of Three quotes

 

  1. Single quotation marks ( ''): wherein all variables escaped as a simple string.
  2. Double quotes ( ""): wherein the variable attributes retained without escaping.
  3. Backtick ( ``): the return result executes the commands.

 

Case 1: single quote and no explanatory variables WYSIWYG

[root@ken ~]# age=10
[root@ken ~]# echo 'my age is $age'
my age is $age

 

Case 2: double quotation marks, explanatory variables

[root@ken ~]# age=10
[root@ken ~]# echo "my age is $age"
my age is 10

 

Case 3: backticks for command execution

[root@ken ~]# name=`ls /root`
[root@ken ~]# echo $name
10.txt 11.txt 12.txt 13.txt 14.txt 1.txt 2.txt 3.txt 4.txt 5 =5 5.txt 6.txt 7.txt 8.txt 9.txt anaconda-ks.cfg elasticsearch-6.4.0.tar.gz elasticsearch-6.4.0.tar.gz.1 ken kenken nohup.out redis-manager-1.1 redis-manager-1.1-release.tar.gz test test1.sh test.sh

 

It all starts with "/"

 

In the Linux system, directory, character device, block device, the socket is so abstract became a file, Linux system, everything is a file. Now usually we are dealing with files, and how to find them? In the Windows operating system, you want to find a file, we have to turn into the disk partition where the file (assuming that this is the D drive), and then enter the specific directory under the partition, finally found the file. But there is no letter C / D / E / F, etc. In the Linux system, all files from the Linux system is the "root (/)" directory to start, and using tree according to the file system hierarchy standard (FHS) shaped structure to store files, and defines the purpose of the common directory. In addition, Linux system file and directory names are strictly case-sensitive. For example, root, rOOt, Root, rooT represents a different directory and file name must not contain slash (/). Linux system

File structure shown in Figure 6-1.

Chapter 6 disk storage structure and division.  Chapter 6 disk storage structure and division.

 

FHS is based on past experience numerous Linux system users and developers and summed up, is a regular user in a Linux system that must be observed when storing a file to guide us what should be saved to a file location, and tell

V The user should find the file where required. However, FHS For the user, can only be regarded as a moral constraint, some users are too lazy to comply, the file will still all over the place, and some had never even heard of it.

 

                          Linux system common directory name and the corresponding content

Directory name

The contents of the file should be placed

/boot

Power required documents - the kernel, the boot menu and the necessary configuration files

/dev

As files stored on any device with an interface

/etc

Profiles

/home

The user's home directory

/bin

Command can also be stored in single-user mode of operation

/lib

Library used at boot time, and function / bin and / sbin following command to invoke

/sbin

Boot process commands needed

/media                                         

It used to mount the device file directory                                                                                 

/opt

Placing third-party software

/root

The system administrator's home directory

/srv

Some network services data file directory

/tmp

Anyone can use the "Share" temporary directory

/proc

Virtual file system, such as system kernel, processes, external devices and network status

/usr/local

User-installed software

/usr/sbin

Linux system will not be used to boot the software / command / script

/usr/share

Help and documentation can also be placed to share files

/where

The main storage of frequently changing files, such as log
/lost+found

When the file system error, some of the missing pieces of the file stored here

 

Vs absolute path relative path

 

In the Linux system, there is another important concept - path.

Path refers to how to locate a file, into the absolute path and relative path.

Refers to the absolute path from the root directory (/) began to write the file or directory name

Refers to a relative path is relative to the current path of the wording.

Guess you like

Origin www.cnblogs.com/kakaisgood/p/11164671.html