Detailed explanation of Linux cat command

The cat command is a text output command under linux, usually used to watch the content of a file; 

cat has three main functions: 

1. Display the entire file at once. 

$ cat   filename 

2. Create a file from the keyboard. 

$ cat  >  filename 

You can only create new files, not edit existing files. 

3. Combine several files into one file. 

$cat   file1   file2  > file 

The specific command format of cat is: cat [-AbeEnstTuv] [--help] [--version] fileName 

Description: Concatenate the file string and pass it to the base output (screen or add > fileName to another file) 

parameter: 

-n or –number number all output lines starting at 1 

-b or --number-nonblank Similar to -n, except that blank lines are not numbered 

-s or –squeeze-blank When encountering more than two consecutive blank lines, replace it with a blank line 

-v 或 –show-nonprinting 

example: 

cat -n linuxfile1 > linuxfile2 Add the file content of linuxfile1 to the line number and enter it into the file linuxfile2 

cat -b linuxfile1 linuxfile2 >> linuxfile3 Add the file contents of linuxfile1 and linuxfile2 with line numbers (no blank lines) and append the contents to linuxfile3. 

example: 

Add the file content of linuxfile1 to the line number and enter it into the file of linuxfile2 

cat -n linuxfile1 > linuxfile2 

Add the file contents of linuxfile1 and linuxfile2 with line numbers (no blank lines) and append the contents to linuxfile3. 

cat -b linuxfile1 linuxfile2 >> linuxfile3 

cat /dev/null > /etc/test.txt This is to clear the contents of the /etc/test.txt file 

We often see statements similar to cat << EOF in linux shell scripts. Unfamiliar children's shoes may find it strange: EOF seems to be the end of a file. What is the role of using it here? 

EOF is "end of file", which means end of text. 

<<EOF 

(content) 

EOF   

The first thing to note is that EOF has no special meaning here, you can use FOE or OOO, etc. (and certainly not limited to three characters or uppercase characters). 

You can replace EOF with something else, which means passing the content as standard input to the program 

Combining these two flags can avoid the use of multi-line echo commands and achieve multi-line output results.   

Next, a brief description of several common usage methods and their effects: 

1. cat<<EOF, ending with EOF input character as standard input: 

2. cat>filename, create a file, and output the standard input to the filename file, and end with ctrl+d as the input: Note: There is no '>' when inputting. 3. cat>filename<<EOF, ending with EOF as input, has the same effect as ctrl+d:   

2. Use 

Looking at the example is the quickest way to get familiar: 

# cat << EOF > test.sh 

> #!/bin/bash #"shell script" 

> #you Shell script writes here. > EOF  结果: 引用# cat test.sh #!/bin/bash #you Shell script writes here.  

As you can see, the content of test.sh is the content generated by cat. 

cat <<EOF >test.sh  内容 EOF   

--- is to write the content to test.sh, and the content that existed before will be overwritten. EOF can be replaced with other symbols such as EEE: cat <<EEE >test.sh content EEE 3. Other ways of writing 

1. Append file 

# cat << EOF >> test.sh  内容  EOF 

---Append the content to the back of test.sh without overwriting the original content 

2. Change the way of writing 

# cat > test.sh << EOF 内容  EOF 

3. EOF is just a logo, not fixed 

# cat << HHH > iii.txt 

> sdlkfjksl 

> sdkjflk 

> asdlfj 

> HHH The "HHH" here replaces the function of "EOF". The result is the same. 

Quote # cat iii.txt 

sdlkfjksl 

sdkjflk 

asdlfj  

4. In non-script 

If it's not in a script, we can use Ctrl-D to output the EOF flag 

# cat > iii.txt 

skldjfklj 

sdkfjkl 

кljкljкlj 

kljlk Ctrl-D  

result: 

Quote # cat iii.txt 

skldjfklj 

sdkfjkl 

кljкljкlj 

kljlk  

※For the meaning of ">", ">>", "<", "<<", etc., please check the introduction of bash by yourself.     

reference from : http://blog.csdn.net/jackalfly/article/details/7556848

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324664849&siteId=291194637