Linux::File content operation [5]: Simple usage of echo command and input redirection, output redirection, and append redirection in writing file content!

Preface: This article is the content of the basic Linux operation chapter!
The environment I use is based on Tencent Cloud Server: CentOS 7.6 64bit.


Study set:


Note: Here, combined with the "echo command" , the simple usage of "redirect" and "pipeline" is introduced in advance . The in-depth underlying principles of "redirect" and "pipeline" will be explained in the subsequent chapter "Basic I/O"!


Note: Please remember: everything in Linux is a file! [Including but not limited to: monitor, keyboard, etc.]


Directory index:
1. Basic syntax, functions and basic usage of echo
2. What is redirection and the types and functions of redirect items
- - 2.1 What is redirection
- - 2.2 Types and functions
of redirection 3. Basic usage examples of output redirection and Test
4. Basic usage examples and tests of additional redirection
5. Basic usage examples and tests of input redirection
- - 5.1 Basic usage and instructions of input redirection
- - 5.2 Simple gameplay of input redirection: Easily implement backup file data
6 . Recommended related articles or series


1. Basic syntax and functions

Basic syntax:

  • echo "[string]": [ie: command + string]

Function:

  • Output the string to the standard output device (ie: the monitor)!

Basic usage is as follows:

[Mortal@VM-12-16-centos ~]$ echo "hello echo"
hello echo
[Mortal@VM-12-16-centos ~]$ echo "hello world"
hello world
[Mortal@VM-12-16-centos ~]$ echo 'a'
a
[Mortal@VM-12-16-centos ~]$ echo 'abc'
abc

2. What is redirection and its types and functions

2.1 What is redirection

(Note: Here is a summary of redirection based on our recent learning content!)
"Redirection" : Modify the default location of the original data output and output it to the specified output location (such as: in a file!)


2.2 Redirect types and functions

Redirect type Function (contacted with file content operations)
Output redirection ">" Starting from the file, the overwrite is written
Add redirect ">>" Write append from end of file
Enter redirect "<" Read the contents of the file and output it to the standard output device

illustrate:

  1. When outputting/appending redirection, if there is a file, it will be accessed directly. If there is no file, the file will be created and then accessed!
  2. Output/append redirection can be combined with the echo command to write the contents of the file!
  3. Output/append redirection: both are performing write operations!
  4. Input redirection can be combined with the cat command to read and print file contents! [That is: changing the data that should be read from the standard input device to reading the data from the file]

3. Basic usage examples and tests of output redirection

Output redirection ">": Starting from the file, overwriting is writing.

Create test directories and files

[Mortal@VM-12-16-centos ~]$ mkdir test
[Mortal@VM-12-16-centos ~]$ cd test
[Mortal@VM-12-16-centos test]$ ls
[Mortal@VM-12-16-centos test]$ touch a.txt
[Mortal@VM-12-16-centos test]$ ls
a.txt

Basic write test (1): The file exists and writes information combined with echo!

[Mortal@VM-12-16-centos test]$ echo "输出重定向测试!" > a.txt
[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向测试\n

Basic write test (2): The file does not exist combined with echo write information!

[Mortal@VM-12-16-centos test]$ ls
a.txt
[Mortal@VM-12-16-centos test]$ echo "测试无文件输出重定向测试!" > b.txt
[Mortal@VM-12-16-centos test]$ ls
a.txt  b.txt
[Mortal@VM-12-16-centos test]$ cat b.txt 
测试无文件输出重定向测试!

Basic write test (3): Output redirection feature: start from file, overwrite is write

[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向测试!\n
[Mortal@VM-12-16-centos test]$ echo "输出重定向:覆盖式写入测试" > a.txt
[Mortal@VM-12-16-centos test]$ ls
a.txt  b.txt
[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向:覆盖式写入测试

4. Add basic usage examples and tests of redirection

Append redirection ">>": append writing from the end of the file.

[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向:覆盖式写入测试
[Mortal@VM-12-16-centos test]$ echo "a" >> a.txt 
[Mortal@VM-12-16-centos test]$ echo "b" >> a.txt 
[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向:覆盖式写入测试
a
b

5. Basic usage examples and tests of input redirection

5.1 Basic usage and instructions of input redirection

Supplementary usage review of cat: read content from standard input and output [already mentioned above!

  • Input redirection "<": Read the file content and output it to the standard output device!
[Mortal@VM-12-16-centos test]$ cat a.txt
输出重定向:覆盖式写入测试
a
b
[Mortal@VM-12-16-centos test]$ cat < a.txt 
输出重定向:覆盖式写入测试
a
b

As in the above situation: cat can already read the contents of the print file! Why do we still need to use input redirection to read output file information?


Note: The actual focus introduced here is: the simple usage of input redirection and understanding the change of the input source "device". The usage of redirection will be explained in depth in the "Basic I/O" section later!


5.2 Simple gameplay of input redirection: easily implement backup of file data

Combined with the previous knowledge: output redirection can write data to the file! Then here we use input and output redirection to implement simple file backup!

[Mortal@VM-12-16-centos test]$ cat a.txt 
输出重定向:覆盖式写入测试
a
b
[Mortal@VM-12-16-centos test]$ cat < a.txt > aa.txt
[Mortal@VM-12-16-centos test]$ ls
aa.txt  a.txt  b.txt
[Mortal@VM-12-16-centos test]$ rm a.txt 
[Mortal@VM-12-16-centos test]$ cat aa.txt 
输出重定向:覆盖式写入测试
a
b

Another: Now that we have studied in order, we have begun to involve the issue of writing file contents. There are two writing methods in the instructions that have been introduced:

  1. Use nano to edit text content!
  2. Use the echo/cat combination mentioned in this article to redirect the data content.

Relatively speaking, the current operation is not very traversal: so the article about the use of vim editor is being updated:
1. Linux:: [Simple Development:: vim editor: (1)]:: Basic understanding of vim editor Three commonly used vim modes | Usage: open editing, exit, save, and close vim ;
2. Linux:: [Simple Development:: vim editor: (2)]:: The most commonly used operations in normal/command mode (1) : Detailed explanation: text copy/paste/cut/delete and undo normal operations and undo operations ;
3. Linux :: [Simple Development:: vim editor: (3)]:: Most commonly used in normal/command mode Operation (2): Detailed explanation: move the cursor to locate the content, between lines: quickly locate the text: beginning/end; within lines: jump between words ;

6. Recommendations for related articles or series

1. Linux learning directory collection ;


2. Linux:: [Basic commands:: File content operations: (1)]:: nano command:: Use the built-in file editor and a simple demonstration to compile and execute an executable program with gcc under Linux (for understanding only: it will be used Just create a file) [Basically not used] ;
3. Linux:: [Basic commands:: File content operations: (2)]:: cat / tac command:: View the entire contents of the target file in forward/reverse order And additional usage of cat: read content from standard input and output ;
4.Linux:: [Basic instructions:: File content operations: (3)]:: more / less instructions:: View part or all of the contents of the target file | Specify to view the first n lines of content [compared to the two, it is recommended to use less] ;


Guess you like

Origin blog.csdn.net/weixin_53202576/article/details/131054504