Advanced Linux Advanced Commands

Output redirection

 

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 # can not make use of output redirection error output into the text 
CAT: klkl: No File or Directory SUCH 
[Ken the root @ ~] klkl CAT # 2> Test # 2 need to use> error output redirection 
[root @ ken ~] # the Test CAT 
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

 

Work less used, you can understand, the role is to redirect input files directly into the command.

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

[ken to the root @ ~] # the 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

[ken to the root @ ~] # the 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 mark ( "): an escape where all variables as a simple string.
  2. Double quotes ( ""): wherein the variable attributes retained without escaping.
  3. Anti-quotes ( "): 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

 

Soft connections, hard-wired

 

Soft connection

Soft connection: the following is the equivalent of windows shortcuts

[Soft link is a shortcut, equivalent to a hard copy and paste link]

Once created, the source file, soft links and hard links both can view the contents of the file.

Edit source files, soft, hard links move along.

Delete files, soft link failure, without affecting hard links. Re-build a file with the same name as the source file, soft links directly linked to the new file, and hard links unchanged. Because the soft link by thy name link.

ln -s

-s: Specifies the source file who is followed by the connection object files

[ken to set a root @ ~] # ln -s kenken ken 
[to set a root @ ken ~] # the ls 
elasticsearch-640targz nohupout redis-ken-manager-1.1 The releasetargz 
elasticsearch-640. tar.gz.1 kenken redis-manager-1.1

 

Add some content to the connection file

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

Find the source file content synchronization

 

Delete connection file

[root@ken ~]# rm -rf kenken
[root@ken ~]# cat ken
this is ken

The same source file

 

Delete files

[root@ken ~]# rm -rf ken
[root@ken ~]# cat kenken
cat: kenken: No such file or directory

Failure connection file

 

Create a source file again

[root @ ken ~] # ken touch 
[root @ ken ~] # cat kenken 
[root @ ken ~] # cat ken

Re-build a file with the same name as the source file, soft links directly linked to the new file. Because the soft link is a link pressing name

 

to sum up:

Soft connection

ln - s original path to the destination path

Features:

1, which is equivalent to the shortcut win

2, no effect delete the link, source files

3, delete the source file, the file link failure

4, modify the source file \ file links, content changes

 

Hard links

ln source destination file

[ken to set a root @ ~] # ln ken ken1 
[to set a root @ ken ~] # the ls 
elasticsearch-ken kenken 640targz redis-manager-1.1 The 
elasticsearch-640targz1 ken1 nohupout redis -manager-1.1-release.tar.gz

 

to sum up:

1, no influence delete the link, source files

2, delete the source files, without affecting the linked file

3, modify the source file \ file links, content changes

 

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/it-peng/p/11404354.html