Linux system knowledge 2—Directory switching related commands (cd/pwd), relative paths, absolute paths, special path characters, directory creation commands mkdir, file operation commands touch, cat, more, cp, mv, rm, grep, wc pipe characters

Table of contents

1. cd to switch working directory

2. pwd to view the current working directory

3. Relative paths, absolute paths, and special path characters

3.1 Absolute path writing method

3.2 Relative path writing method

3.3 Special path characters

4. Create directory command (mkdir)

4.1 Create a directory (folder) through the mkdir command

1.mkdir command

2.mkdir -p option

5. File operation command part1 (touch, cat, more)

5.1touch command

1 touch to create file

5.2 Use cat command to view file contents

1.cat command to view file contents

5.3 more command

1.more command to view file contents

6. File operation commands part2 (cp, mv, rm)

6.1 cp command

1.cp command to copy files

6.2 mv command

1.mv move files or folders

6.3 rm command

1.rm delete files and folders

2.rm - wildcard

3. rm forcefully removes the -f option

7. Search command (which, find)

7.1 which command

7.2 find command - find files by file name

7.3find command-find files by file size

8. grep, wc and pipe character

8.1 grep command

Edit

8.2 WC command to do quantity statistics

8.3 Pipe character

9.echo, tail and redirection characters

9.1echo command

9.2 Backticks

9.3 Redirect character

9.4 tail command

1. cd to switch working directory

When the Linux terminal (command line) is opened, the user's HOME directory will be used as the current working directory by default
We can change the current working directory through the cd command.
The cd command comes from English: Change Directory 

Syntax:

 cd [Linux path]

The cd command does not require options.Only parameters, indicating which directory to switch to
The cd command is direct Execute, do not write parameters, which means returning to the user's HOME directory

2. pwd to view the current working directory

It is actually inappropriate to verify the current working directory through ls.

We can use the pwd command to view the current working directory.

 pwd command comes from: Print Work Directory 
Syntax: pwd 
● pwd command, no options, no parameters, just enter pwd directly

3. Relative paths, absolute paths, and special path characters

3.1 Absolute path writing method

 cd /home/wzy/Desktop

 Absolute path: Starting from and the directory, it is a way of describing the path. The path description starts with /

3.2 Relative path writing method

cd Desktop

 Relative path:Start from the current directory, a way to describe the path, the path description does not need to start with /

3.3 Special path characters

    .    indicates the current directory, for example, cd ./ Desktop indicates switching to the Desktop directory under the current directory, which has the same effect as cd Desktop

   ..    represents the upper-level directory, for example: cd .. can switch to the upper-level directory, cd ../.. Switch to the upper-level directory
   ~   means HOME directory, for example: cd ~ to switch to the HOME directory or cd ~/ Desktop to switch to HOME The Desktop Directory

 Examples are as follows:

 

4. Create directory command (mkdir)

4.1 Create a directory (folder) through the mkdir command

1.mkdir command

New directories (folders) can be created through the mkdir command
mkdir comes from English: Make Directory 

Syntax: mkdir [- p ] Linux path

The parameter is required, indicating the Linux path, that is, the path of the folder to be created, either a relative path or an absolute path

- p optionoptional means automatically creating a non-existentparent directory , suitable for creating continuous multi-level directories

Examples are as follows:

2.mkdir -p option

If you want to create multiple levels of directories at once,

An error will be reported because the upper-level directories home and good do not exist, so the 666 directory cannot be created. You can use the -p option to create the entire chain.

Note: Creating a folder requires modifying permissions. Please ensure that all operations are within the home directory and cannot be performed outside home.

5. File operation command part1 (touch, cat, more)

5.1touch command

1 touch to create file

Files can be created through the touch command

Syntax: touch Linux path

 The touch command has no options, and the parameters are required, indicating the file path to be created. Relative, absolute, and special path characters can be used.

5.2 Use cat command to view file contents

1.cat command to view file contents

After we have the file, we can view the contents of the file through the cat command.
However, we have not yet learned the vi editor and cannot edit content into the file. Therefore, for the time being, we first manually add content to the file through graphics to test. cat command

Prepare the file content and view the content through cat

Syntax: cat Linux path

 The cat command has no options, only the parameters are required. The parameters indicate: the file path to be viewed, relative, absolute, and special path characters can be used.

5.3 more command

1.more command to view file contents

The more command can also view the file content. The difference from cat is:
* cat directly displays all the content
* more supports translation. page, if the file content is too much, it can be displayed page by page

Syntax: more Linux path

There are also no options, only required parameters. The parameters represent: the file path to be viewed. Relative, absolute, and special path characters can be used.

Press space to turn pages, q key to exit viewing

6. File operation commands part2 (cp, mv, rm)

6.1 cp command

1.cp command to copy files

The cp command can be used to copy files\folders. The cp command comes from the English word: copy 

Syntax: cp [- r ] Parameter 1 Parameter 2

- r option, optional, used to copy folders, indicating recursion
Parameter 1: Linux path, indicating the copied file or folder
Parameter 2: Linux path ◇ indicates the place to be copied

Among them: How to use -r 

6.2 mv command

1.mv move files or folders

The mv command can be used to move files\folders. The mv command comes from the English word: move 

Syntax: mv Parameter 1 Parameter 2

Parameter 1, Linux path, the file or folder to be moved
Parameter 2, Linux path, where the table is to be moved. If the target does not exist, rename it. Make sure the target exists

6.3 rm command

1.rm delete files and folders

The rm command comes from the English word: remove 

Syntax: rn [- r - f ] Parameter 1 Parameter 2.........Parameter N

* Same as the cp command, the -r option is used to delete the folder
* -f means force, forced deletion ( will not pop up a prompt Confirmation information )
                * Ordinary users will not be prompted to delete content, only root administrator users will be prompted to delete content
                *So the -f option is not used by ordinary users

* Parameter 1, parameter 2, ...., parameter N represent the path of the file or folder to be deleted, separated by spaces

2.rm - wildcard

 The rm command supports the wildcard character *, which is used for fuzzy matching
. The symbol * represents the wildcard character, which matches any content (including empty). Example:
        test *     means matching any content starting with test
        * test     means matching any content ending with test* test a> means matching any content containing test* test *
         

Demo:
Delete all files or folders starting with test:

3. rm forcefully removes the -f option

You can temporarily switch to the root user experience by su - root and enter the password *******

Use the exit command to return to the normal user

7. Search command (which, find)

7.1 which command

The Linux commands we learned earlier are actually binary executable programs. and in Windows systems. exe file means the same thing.
We can use the which command to see where the program files for the series of commands used are stored

Syntax:
 which command to find

7.2 find command - find files by file name

In graphics, we can easily search for specified files through the search function provided by the system.

Similarly, in Linux systems, we can search for specified files through the find command.

Syntax: find starting path -name " file name to be found "

In order to ensure that subsequent demonstrations have the maximum limit, the search can be completed throughout the system
, we can switch to root.

find wildcard match which is the same as

Examples are as follows:

7.3find command-find files by file size

Syntax: find starting path- size +1- n [ kMG ]

+ - represents greater than and less than
n represents large and small numbers
kMG represents large and small units, k (lowercase letters) represents kb, M represents MB, G means GB 

Example:
Find files less than 10KB: 

find / -size -10k

Find files larger than 100MB:

find / size + 100M

Find files larger than 1GB: 

find /- size +1G

 

8. grep, wc and pipe character

8.1 grep command

You can use the grep command to filter file lines by keywords from the file.

Syntax: grep [- n ] keyword file path

option - n, optional, indicates that the line number of the matching line is displayed in the results.
Parameters, keywords, required, indicating filtering keywords, with spaces or other special symbols, it is recommended to use "" to surround the keywords
Parameter, file path, required, indicating the file path to filter the content, which can be used as the content input port
Now, create itheima .txt in the HOME directory through the touch command, and edit it through the graphical page and save the following content:

8.2 WC command to do quantity statistics

You can count the number of lines, words, etc. in a file through the wc command.

Syntax: wc [ -c -m -l -w ] file path

option, -c counts the number of bytes
option, -m counts the number of characters
option, -l counts the number of lines
option, -w Count the number of words
Parameters, file paths, files to be counted, can be input as content

8.3 Pipe character

Pipe character: |  

The meaning of the pipe character is: use the result of the command on the left side of the pipe character as the input of the command on the right side.

As shown above:
test .txt output result (File content)< a i=4> as the input of the grep command on the right (filtered files)

9.echo, tail and redirection characters

9.1echo command

You can use the echo command to output specified content in the command line

Syntax: echo output content

No options, only one parameter, indicating the content to be output. Complex content can be surrounded by " "
Demonstration:
Display on the terminal : Hello Linux 


 With special symbols such as spaces or \, it is recommended to use double quotes:


Because if it is not surrounded, it will be easily recognized as parameter 2 after the space. Although echo will not be affected, you have to develop a habit.

9.2 Backticks

Look at the following command: echo pwd 
The original intention is to output the current working path, but pwd is output as an ordinary character.
We can surround the command with backticks   ` (often also called a floating number) and < The content enclosed by /span> will be executed as a command instead of ordinary characters`

9.3 Redirect character

Let’s learn two more special symbols

Redirection symbols: > and >>>
>, overwrite and write the result of the command on the left to the file specified on the right side of the symbol

>>, append the result ◇ of the left command to the file specified on the right side of the symbol

Demo:

9.4 tail command

Use the tail command to view the tail content of the file and track the latest changes to the file.

The syntax is as follows: tail [- f - num] Linux path

Parameter Linux path, indicating the tracked file path
Option -f, indicating continuous tracking
Option -num indicating how many lines at the end to view, no Fill in the default 10 lines

Tail content, default ten lines

Guess you like

Origin blog.csdn.net/WZY22502701/article/details/131927008