Linux file operation commands (touch, cat, more, cp, mv, rm)

We learned how to operate directories (folders) before, so now let’s take a look at how to operate files.

1.touch command

Function: Create file

grammar:touch 参数

  • Parameters: path to the file being created

Note: The touch command has no options, and the parameter is required , indicating the path of the file to be created. Relative, absolute, and special path characters can be used.

Tip, you can judge whether it is a file or a folder by the first character, it is obvious:

  • If the first character is 'd', it is a folder, which is the first letter of directory;
  • If the first character is '-', it is a file.

2.cat command

Function: View file content

grammar:cat 参数

  • Parameters: file path to be viewed

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

3.more command

Function: View files, can support page turning for viewing

grammar:more 参数

  • Parameters: file path to be viewed

  • During viewing:

    • 空格key to page down

    • ctrl+b to page up

    • qExit viewing

Note: 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.

You will also find that both the more command and the cat command are used to view the content of the file, but the difference between the more command and the cat command is: cat directly displays all the content. more supports page turning. If the file content is too much, it can be page-by-page. display

4.cp command

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

Function: Copy files and folders

grammar:cp [-r] 参数1 参数2

  • Parameter 1, Linux path, indicates the copied file or folder

  • Parameter 2, Linux path, indicates the place to be copied.

  • Options: -r, optional, copy folder to use

Example:

  • cp a.txt b.txt, copy a.txt in the current directory to b.txt

  • cp a.txt test/, copy the current directory a.txt to the test folder

  • cp -r test test2, copy the folder test to the current folder as test2

Note: To copy a folder, you must use the -r option, otherwise it will not take effect

Through the above examples, we can easily find that

  • If parameter 1 and parameter 2 are both files, then parameter 1 file will be

5.mv command

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

Function: Move files and folders

grammar:mv 参数1 参数2

  • Parameter 1: Linux path, indicating the moved file or folder

  • Parameter 2: Linux path, indicating the place to be moved. If the target does not exist, rename it to ensure that the target exists.

Note: If the target does not exist, it will have the effect of renaming it.

6.rm command

Function: Delete files and folders. The rm command comes from the English word: remove

grammar:rm [-r -f] 参数...参数

  • Parameters: Support multiple parameters, each one represents deleted, separated by spaces.

  • Options: -r, delete folder using

  • Option: -f, means force, forced deletion, no confirmation prompt will be given , generally used by root users

    • Ordinary users will not be prompted when deleting content. Only root administrator users will be prompted when deleting content.

    • Therefore, ordinary users do not need the -f option.

  • Parameter 1, parameter 2,..., parameter N represent the paths of files or folders to be deleted, separated by spaces.

The rm command is very dangerous, so be careful, especially when switching to the root user.

- Example

Delete Files

delete folder

- Temporarily switch to root user

You can temporarily switch to the root user experience through su - root and enter the password ****** (the same as the default for ordinary users)

Return to the normal user by entering the exit command.

- Wildcard

The rm command supports the wildcard character *, which is used for fuzzy matching.

  • The symbol * represents a wildcard character, which matches any content (including empty), example:
  • test* means matching anything starting with test
  • *test, means match anything ending with test
  • *test* means matching any content containing test

7. Special reminder

rm is a dangerous command, especially when you are the root (super administrator) user. Please use with caution.

Please do not execute the following commands under the root administrator user:

  • rm -rf /
  • rm -rf /*

The effect is equivalent to formatting C drive on Windows.

If you do this in the company, you are likely to be considered as deleting the database and running away, so be careful!

Guess you like

Origin blog.csdn.net/lemonzjk/article/details/135441694