The article directories

  • 1. Files and directories
    • 1. The PWD command
    • 2. The ls command
    • 3. The CD command
    • 4. The mkdir command
    • 5. The rmdir command deletes an empty directory
    • 6. Touch commands
    • 7. The cp command
    • 8. The rm command
    • 9. The mv command
    • 10. The cat command
    • 11. More instructions
    • 12. Less orders
    • 13. The echo command
    • 14. The head instructions
    • 15. The tail command
    • 16. > instructions and >> instructions
    • 17. Ln the instructions
    • 18. The history instruction

1. Files and directories

1. The PWD command

  • Basic syntax:pwd(Function description: Display the absolute path of the current working directory)
  • Example: Display the absolute path of the current working directory

2. The ls command

  • Basic syntax:Ls [option] [directory or file]
  • Commonly used options

    -a: Displays all files and directories in the current directory, including hidden ones.

    -l: Displays information in a list
  • Examples of application

    Example: View all information about the current directory

3. The CD command

  • Basic syntax:CD [parameter](Function description: Switch to the specified directory)
  • Understanding: Absolute path and relative path
  • CD -orcd: Switch to your home directory, for example if you are root,CD -to/root
  • cd ..Go back to the directory one level above the current directory
  • Examples of application

    Case 1: Switch to root using absolute path,cd /root

    Case 2: Use a relative path to the /root directory, such as in/home/xdr ,cd .. /.. /root

    Case 3: go to the directory one level above the current directory,cd ..

    Case 4: Back home Catalog,cd ~

4. The mkdir command

  • The mkdir command is used to create a directory
  • Basic syntax:Mkdir [option] Directory to create
  • Commonly used options

    -p: Creates a multi-level directory application instance
  • Examples of application

Example 1: Create a directory /home/xdr630

mkdir /home/xdr630
Copy the code

Example 2: Create a multilevel directory /home/animal/tiger

mkdir -p /home/animal/tiger
Copy the code

5. The rmdir command deletes an empty directory

  • The basic grammar

    Rmdir [option] Empty directory to delete
  • Examples of application

    Example: Delete a directory/home/xdr630
  • Using the details

    rmdirDelete the empty directory,Cannot be deleted if there is content in the directory.
  • Tip: Delete if neededDirectory is not empty, need to useRm -rf Indicates the directory to be deleted
  • Such as:rm -rf /home/animal

6. Touch commands

  • The touch command creates an empty file
  • Basic syntax:Touch file name
  • Examples of application

    Case:/homeDirectory, create an empty filehello.txt
touch hell0.txt
Copy the code

7. The cp command

  • The cp command copies the file to the specified directory
  • Basic syntax:Cp [option] source dest
  • Commonly used options

    -r: Recursively copies the entire folder
  • Examples of application
  1. Case 1: will/home/hello.txtCopy to/home/aaadirectory
cp hello.txt /home/aaa
Copy the code
  1. Case 2: Recursively copy an entire folder, for example, to/home/bbbThe entire directory, copy to/opt
cp -r /home/bbb /opt
Copy the code
  • Using the details

    Methods to force overwriting without prompting:\cp
\cp -r /home/bbb/opt
Copy the code

8. The rm command

  • Description:The rm command removes a file or directory
  • Basic syntax:Rm [option] File or directory to be deleted
  • Common options:

    -r: Deletes the entire folder recursively

    -f: Forcible deletion is not prompted
  • Examples of application
  1. Case 1: will/home/hello.txtdelete
rm /home/hello.txt
Copy the code
  1. Case 2: Delete the entire folder recursively/home/bbb
rm -rf/home/ BBB [delete entire folder without prompting]Copy the code
  • Using the details

    Forcible deletion method without prompting: Bring-fParameters can be

9. The mv command

  • Mv Moves files and directories or renames them
  • The basic grammar
mvOldNameFile newNameFileCopy the code
mv/temp/ moveFile /targetFolderCopy the code
  • Examples of application
  1. Case 1: will/home/cat.txtRename the file topig.txt
mv /home/cat.txt pig.txt
Copy the code
  1. Case 2: will/home/pig.txtFile move to/rootdirectory
mv /home/pig.txt /root
Copy the code
  1. Case 3: Move the entire directory, for example/opt/bbbMove to the/home
mv /opt/bbb /home
Copy the code

10. The cat command

  • Cat View file contents
  • Basic syntax:Cat [option] The file to view
  • Commonly used options

    -n: Displays the line number
  • Examples of application

    Case 1: View/etc/profileFile contents and display line numbers
cat -n /etc/profile
Copy the code

  • Using the details

    Cat can only browse files, but not modify them. For easy browsing, it usually carries pipe commands| more
cat -nThe/etc/profile | more interact []Copy the code
  • The pipe command is used to give more the result obtained previously, as in:
cat -n /etc/profile | more
Copy the code

  • Enter the enter key to change to the next line, enter space to turn the page

11. More instructions

  • The MORE directive is a text filter based on the VI editor that displays the contents of a text file page by page in a full-screen manner. The more command has several built-in shortcut keys (interactive commands). For details, see operation instructions
  • Basic syntax:More Indicates the file to view
  • Operation instructions:

  • Example Use more to view files
more /etc/profile
Copy the code

12. Less orders

  • The less command is used to view file contents in split screen. Similar to the MORE command, but more powerful than the MORE command, the less command supports various display terminals. The less command does not load the entire file at one time, but loads the content according to the display requirements, which is efficient for displaying large files.
  • Basic syntax:Less Indicates the file to view
  • Operation instructions:

  • Examples of application

    Case: AdoptionlessView a large file file/ opt/essays. TXT
Less /opt/ articles.txtCopy the code

13. The echo command

  • Echo outputs content to the console
  • Basic syntax:Echo [options] [output content]
  • Examples of application
  1. Case: UseechoInstruction output environment variables, such as output$PATH or $HOSTNAMEEnvironment variable names are generally capitalized
echo $PATH
Copy the code

echo $HOSTNAME
Copy the code



2. Case: UseechoCommand outputHelloWorld

14. The head instructions

  • Head is used to display the beginning of a file. By default, the head command displays the first 10 lines of a file
  • The basic grammar
Head file (Function description: View file headers10Line)Copy the code
head -n 5File (Function description: View the file header5Line,5Can be any number of rows)Copy the code
  • Examples of application

    Case: View/etc/profileThe first five lines of code
head -n 5 /etc/profile
Copy the code

15. The tail command

  • tailUsed to print the contents of the tail of a file. By default, the tail directive displays the first part of the file10Line.
  • The basic grammar
  1. Tail file(Function description: View the last 10 lines of the file)
  2. Tail -n 5 File(Function description: View 5 lines at the end of the file. 5 can be any number of lines.)
  3. Tail -f fileFunction Description: Track all updates to this document in real time.
  • Examples of application
  1. Case 1: View/etc/profileThe last five lines of code
tail -n 5 /etc/profile
Copy the code

  1. Case 2: Real-time monitoringmydate.txt(Create a new empty file), see if you see the file changes, real-time append

    hello,world.
tail -f /home/mydate.txt
Copy the code
  • Open another terminal, appendecho "helloworld", you can see the real-time change of the terminal above

16. > instructions and >> instructions

> Output redirection and >> append

  • The basic grammar
  1. Ls-1 > fileFunction description: Write the contents of the list to a filea.txtMiddle (overwrite)
  2. Ls -al >> fileFunction description: Appends the contents of the list to the fileaa.txtAt the end of the)
  3. Cat file 1 > file 2Function Description: WillFile 1Content covered toFile 2)
  4. Echo "Content" >> File(additional)
  • Examples of application
  1. Case 1: will/homeIn the directoryFile listWritten to the/home/info.txt, overwrite write
ls -l/home > /home/info.txt [ifinfo.txtIf no, it will be created.Copy the code
  • View the directory to write to:

  • Open theinfo.txtTo view

  1. Case 2: Append the current calendar information to/home/mycalThe instructions in the file (which will be created automatically without mycal) are:
  • calCommand: View the current date

cal >> /home/mycal
Copy the code



17. Ln the instructions

  • Soft links, also known as symbolic links, are similar to shortcuts in Windows, where links to other files are stored
  • The basic grammar

    Ln -s [original file or directory] [soft link name](Function description: to create a soft link to the original file)
  • Examples of application
  1. Case 1: in/homeDirectory to create a soft connectionmyrootAnd connected to the/rootdirectory
ln -s /root /home/myroot
Copy the code

  • At this point, switch tomyrootDirectory, is access torootThe directory

  1. Case 2: Delete the soft connectionmyroot.Pay attention to: Don’t take itmyrootAt the back of the”/
rm -f /home/myroot
Copy the code

  • Detail description

    When we usepwdCommand to view the directory, still see the soft link directory.

18. The history instruction

  • You can view history commands that have been executed or run history commands
  • The basic grammar

    history(Function description: View historical commands that have been executed.)
  • Examples of application
  1. Example 1: Display all the history command history
  2. Case 2: Show the 10 most recently used commands.
history 10
Copy the code



3. Case 3: Execute the history number as1005Command, use firsthistoryCommand to view the number, at this time1005The instruction corresponding to the number isll

!1005
Copy the code