Getting Started with Linux Basic Instructions (2)

This article mainly provides some basic Linux instructions for file operations . I hope it will be helpful to everyone, and I will support you!

Table of contents

cp command (copy)

mv command (cut)

nano command

cat command (print file content)

> output redirection

>> append redirection

< input redirection 

more command

less command (recommended)

head command

tail command


cp command (copy)

Syntax: cp [source file/directory] [destination directory]

Function: copy the file to the specified directory path.

Common usage:

1. Copy files: cp file target directory path/ rename file name   Note: If there is no file with the same name as the renamed file under a certain path, the copied file will be renamed. The rename file name can be omitted, and the file to be copied will be copied directly to this path by default.

2. Copy directory: cp -r directory target directory path/ rename directory name  Note: -r means recursive, and the directory needs to be copied recursively. The renaming rules are the same as above.

mv command (cut)

Syntax: mv [source file/directory] [destination directory path]

Function: Cut the file or directory to the specified path.

Common usage:

1. Cut file: mv file target directory path/ rename file name  Note: If there is no file with the same name as the cut file in a certain path, the cut file will be renamed. The file name for renaming can be omitted, and the cut file will be directly cut to this path by default.

Small tips: Rename the file: mv file name   function: a rename the file in the current path.

2. Cut directory: mv directory target directory path/ rename directory name

The renaming rules are the same as above.

nano command

Syntax: nano [file]

Function: open file

Common usage:

Open the file, write the content, close the file.

cat command (print file content)

Syntax: cat [options] [file]

Function: Print the content of the specified file .

options:

-b number non-empty output lines
-n number all lines of output
-s do not output multiple blank lines

Common usage: cat a.txt prints the contents of a.txt.

> output redirection

Syntax: [content] > file

Function: Redirect the output of the specified content and write it to the specified file. If the file name does not exist, it will be created by the way. (The content of the file will be cleared every time)

Common usage: echo "hello" > a.txt writes "hello" into a.txt. It is cleared every time it is written.

Supplement: echo "string": print the string to the display.

 

Small tips: > File/directory   function: create a file/directory, or empty a file/directory.

>> append redirection

Syntax: [content] >> file

Function: append the specified content to the specified file. The meaning of appending is to write new content without changing the content of the original file.

Common usage: echo "hello" >> a.txt Append "hello" to a.txt.

< input redirection 

Execute the following command in linux

 It can be seen that the cursor is flashing on the next line, waiting for input

 After typing freely, press Enter

 It can be seen that the same string of strings is printed on the display again. The reason here is that when the cat command is not followed by a file, it defaults to reading from the standard input stream - the keyboard.

At this time, < input redirection can be used to change the input source, which means reading from a.txt.

more command

Syntax: more [option] [file name]     enter key to scroll down. 'q' to quit.

Function: View the content of large files, preventing the screen from being too large .

Common usage: more bigdir.txt

less command (recommended)

Because the moer command can only scroll down, not up, it is more recommended to use the less command.

Syntax: less [option] [file name]    can use the up and down keys on the keyboard to scroll up and down. 'q' to quit.

Function: view the content of large files, search for the content of specified keywords.

options:

-i   ignore case when searching
-N   display the line number of each line
/ string: function to search down for " string "
? string: function to search upwards for " string "
n : repeat previous search (relative to  /  or  ?  )
N : Repeat previous search in reverse (relative to  /  or  ?  )

Usage: less bigdir.txt 

head command

Syntax: head [-n] [file]

Function: View the content of the first n lines of the file. The first ten lines are displayed by default.

Common usage: head -5 bigdir.txt

tail command

Syntax: tail [-n] [file]

Function: View the content of the last n lines of the file. The last ten lines are displayed by default.

Common usage: tail -5 bigdir.txt

Guess you like

Origin blog.csdn.net/2301_76144863/article/details/131951701