Linux basic commands, including file operations and basic server-related operations

basic operation

file operation

  • Create a folder: mkdir folder name
    Create a multi-layer folder at one time: mkdir -p folder name/folder name/folder name
    Create a file: touch file name

  • Enter the folder: cd file path (just enter cd and press Enter to return to the root directory)
    view the files under the folder: ls or ll
    view files, including hidden files: ls -a

  • Delete folder: rm -rf folder name
    Delete file: rm file name
    supplement:
    r: recursive deletion, f: forced deletion
    If the file name is long, after entering the file name, press the tab key, it will be automatically completed and
    multiple deletions can be deleted at the same time folders, example:rm -rf file1 file2

  • Modify file name: mv file name before modification and file name after modification
    Example:mv file file_modify

  • Move file: mv filename to move path to move
    Example: move to parent:mv text.txt ../text.txt

  • Copy file: cp The file name of the file to be copied after copying
    Example:cp file file_copy

File content manipulation

View Files:

打印出文件所有内容:cat 文件名
打印出文件前面几行:head 文件名
打印出文件末尾几行:tail 文件名

Write content:

往文件写入内容:示例:echo “hello world” > text.txt
写入变量:示例:echo “${
   
   {secrets.ID_RSA}}” > ~/.shh/id_rsa

edit file content

vi 文件名(会进入预览模式)
按i进入编辑模式 
按esc退出编辑模式 
:w 保存文件
:q 退出文件
:q! 强制退出
:wq 保存并退出

Note: vi or vim: If the following file name does not exist, create and open it; if it already exists, open the existing one.

other

  • Clear screen: clear

server related

Login: Enter ssh 用户名@服务器ip地址, enter the password after pressing Enter, the default user name is root
Login example: ssh [email protected]
Exit: exit

establish trust

Once trust is established, you can log in to the server without a password.

step

  1. Generate ssh key locally, find the id_rsa.pub file after generation
  2. Copy the contents of the id_rsa.pub file to ~/.ssh/authorized_keys of the server and
    enter the commands one after another after logging in to the server:
    (1). cd ~/.ssh
    (2). lsCheck whether there is an authorized_keys file in this directory. If not, create touch authorized_keysone .
    (3). vi authorized_keysEnter the authorized_keys file and edit: press i to enter the edit mode, paste the content copied from id_rsa.pub, press escexit to edit, enter to :wsave, enter to :qexit.
  3. Enter exitto log out of the server and log in again.

remote operation

remote copy scp

  1. First enter the (local) directory of the file you want to copy
  2. scp the file name you want to copy username@server ip: the directory you want to copy to
    For example, copy the test.txt file in the current directory to the /root/test-dir directory of the server:
    scp test.txt [email protected]:/root/test-dir

Execute commands remotely

Login in the form + space + quotation marks. The commands to be executed are written in the quotation marks, and multiple commands are separated by semicolons.
For example, to create a remote.txt file in the test-dir directory of the server:
ssh [email protected] "cd ./test-dir; touch remote.txt"

Guess you like

Origin blog.csdn.net/dongkeai/article/details/127443818