Article Directory
Preface
If you are not familiar with the file directory structure of the Linux system, please click here .
pwd instruction
Introduction: pwd is the abbreviation of Print Working Directory. Display the absolute path of the current working directory.
Basic grammar
pwd [选项]
Option parameter description:
- -l: -logical, when the directory is linked, output the link path.
- -p: -physical, output the physical path.
Example 1 : Display the absolute path of the current working directory
pwd
ls instruction
Introduction : ls is the abbreviation of List. When the ls command prints the file list, you can also view file permissions, directory information, and so on. List the files in the directory.
Basic syntax:
ls [选项] [目录或者文件]
Common parameter description of options:
- -a: -all, list all files in the directory, including hidden files beginning with.
- -l: Display information in a list. In addition to the file name, the file permissions, owner, file size and other information are listed in detail.
Example 1: View all content information under /root
ls -a /root
cd command
Introduction: cd is the abbreviation of Change Dictory, used to switch to the specified directory.
Basic syntax:
cd [参数]
Note: Common parameters can be absolute path or relative path.
- cd and cd ~ are home directories
- cd / is to go back to the root directory
- cd... Back to the upper level of the current directory
Example 1: Use the absolute path to return to the /root directory
cd /root
Example 2: Use a relative path to return to the /root directory
cd ../root
Example 3: Go back to the previous directory
cd ..
Example 3: Back home directory
cd ~
mkdir instruction
Introduction: mkdir is the abbreviation of make directory, used to create directories.
Basic syntax:
mkdir [选项] 要创建的目录
Common parameter description of options:
- -p: If some directories in the path do not exist, after adding this option, the system will automatically create those non-existent directories, that is, multiple directories can be created at one time.
Example 1: Create a directory /home/casey
mkdir /home/casey
Example 2: Create a multi-level directory /home/hello/casey
mkdir -p /home/hello/casey
rmdir command
Introduction: rmdir is the abbreviation of remove dirctory, used to delete an empty directory, if there is content in the directory, it cannot be deleted.
Basic syntax:
rmdir [选项] 要删除的空目录
Common parameter description of options:
- -p; Recursively delete a directory. When the parent directory is empty after the subdirectory is deleted, it will also be deleted.
Example 1: Delete a directory /home/casey
rmdir /home/casey
Example 2: Recursively delete the directory /home/hello/casey
rmdir -p /home/hello/casey
Tip : If you need to delete a non-empty directory, you need to use rm -rf to delete the directory.
touch command
Introduction: The touch command is used to create an empty file.
Basic syntax:
touch [选项] 文件名称
Example 1: Create an empty file hello.txt.
touch hello.txt
cp instruction
Introduction: cp is the abbreviation of copy, used to copy files to the specified directory.
Basic syntax:
cp [选项] source dest
Common parameter description of options:
- -r: Copy the entire folder recursively.
Example 1: Copy /home/aaa.txt to the /home/bbb directory
cp /home/aaa.txt /home/bbb
Example 2: Copy the entire folder recursively, for example copy the entire directory /home/test to the /home/casey directory
cp -r /home/test /home/casey
rm instruction
Introduction: rm is the abbreviation of remove, used to remove [delete] files or directories.
Basic syntax:
rm [选项] 要删除的文件或目录
Common parameters for options:
- -r: delete the entire folder recursively
- -f: no prompt for forced deletion
Example 1: Delete /home/aaa.txt
rm /home/aaa.txt
Example 2: Recursively delete the entire folder
rm -rf /home/bbb
Example 3: Forcibly delete the method not mentioned, just bring the -f parameter
rm -f /home/aaa.txt
mv instruction
Introduction: mv is the abbreviation of move, used to move files and directories or rename.
Basic syntax:
mv [选项] 源文件或目录 目标文件或目录
mv oldNameFile newNameFile (功能描述:重命名)
mv /temp/movefile /targetFolder (功能描述:移动文件)
Common parameter description of options:
- -f When overwriting files, overwrite directly without asking.
- -b Back up the file before overwriting it.
Example 1: Rename the /home/aaa.txt file to bbb.txt
mv aaa.txt bbb.txt
Example 2: Move the /home/pig.txt file to the /root directory
mv /home/pig.txt /root/
cat instruction
Introduction: cat is the abbreviation of concatenate, used to view the content of the file and open it in read-only mode.
Basic syntax:
cat [选项] 要显示的行号
Common parameter description of options:
- -n: display line number
Example 1: View the contents of the /etc/profile file and display the line number.
cat -n /etc/profile
Tips : cat can only browse files, but not modify files. For the convenience of browsing, the pipeline command | more, cat file name | more [paged browsing] is usually taken.
more instruction
Introduction: The more command is a text filter based on the VI editor, similar to cat, but it will be displayed page by page, which is more convenient for users to read page by page. The most basic command is to press the space key (space). Go to the next page, press the b key to go back to the next page, and there is also the function of searching for strings (similar to vi).
Basic syntax:
more 要查看的文件
Example 1: View the /etc/profile file.
more /etc/profile
Commonly used shortcut keys
less command
Introduction: The less command is used to view the contents of a file on a split screen. Its function is similar to the more command, but it is more powerful than the more command and supports various display terminals. When the less command displays the content of a file, it is not displayed after loading the entire file at once, but loads the content according to the display needs, which is more efficient for displaying large files.
Basic syntax:
less 要查看的文件
Commonly used shortcut keys
> Instructions and >> Instructions
Introduction: > Instruction and >> Instruction,> Output redirection: will overwrite the original file content. >> Append: The content of the original file will not be overwritten, but appended to the end of the file.
Basic syntax:
ls -l > 文件(功能描述:列表的内容写入文件中覆盖)
ls -al >> 文件(功能描述:列表的内容追加到文件 aa.txt 的末尾)
cat 文件 1 > 文件 2 (功能描述:将文件1的内容覆盖到文件2)
echo "内容" >> 文件
Example 1: Write the contents of the list into the file a.txt (overwrite)
ls -l > a.txt
Note: ls -l> a.txt, overwrite the displayed content of ls -l into a.txt file, if the file does not exist, create the file.
Example 2: Append the contents of the list to the end of the file aa.txt.
ls -al >> aa.txt
Example 3: Overwrite the contents of file 1 to file 2
cat /etc/profile > c.txt
Example 4: Write the list of files in the /home directory to /home/info.txt
ls -l /home/ > /home/info.txt
echo command
Introduction: Echo output content to the console.
Basic syntax:
echo [选项] [输出内容]
Example: Use echo command to output environment variables and output current environment path.
echo $PATH
head command
Introduction: head is used to display the content at the beginning of the file. By default, the head command displays the first 10 lines of the file.
Basic syntax:
head 文件 (功能描述:查看文件头 10 行内容)
head -n 5 文件 (功能描述:查看文件头 5 行内容,5 可以是任意行数)
Example 1: View the first 5 lines of code in /etc/profile.
head -n 5 /etc/profile
tail instruction
Introduction: tail is used to output the tail content of the file. By default, the tail command displays the last 10 lines of the file.
Basic syntax:
tail 文件 (查看文件后10行内容)
tail -n 5 文件 (查看文件后5行内容,5可以是任意行数)
tail -f 文件 (实时监控该文档的所有更新)
Example 1: View the last 5 lines of code in /etc/profile
tail -n 5 /etc/profile
ln instruction
Introduction: ln (link files), its function is to establish a synchronized link for a certain file in another location, similar to the shortcut in windows.
Basic syntax:
ln [参数][源文件或目录][目标文件或目录]
Description of necessary parameters:
- -b delete, overwrite the previously established link
- -d allows super users to make hard links to directories
- -f enforce
- -i interactive mode, prompt the user whether to overwrite if the file exists
- -n treat symbolic links as general directories
- -s soft link (symbolic link)
- -v shows detailed processing
In the Linux file system, there are so-called links. We can regard them as aliases of archives. The links can be divided into two types: hard link and symbolic link, meaning hard link A file can have multiple names, and the soft link method is to generate a special file whose content points to the location of another file. Hard links exist in the same file system, but soft links can span different file systems.
Soft link:
1. A soft link exists in the form of a path. Similar to shortcuts in the Windows operating system
2. Soft links can cross file systems, hard links cannot
3. Soft links can link a non-existent file name
4. Soft links can link directories
Hard links:
1. Hard links exist in the form of copies of files. But it does not take up actual space.
2. It is not allowed to create hard links to directories
3. Hard links can only be created in the same file system
Example 1: Create a soft link linkToRoot in the /home directory and connect to the /root directory.
ln -s /root linkToRoot
Note: When we use the pwd command to view the directory, we still see the directory where the soft link is located.
history command
Introduction: View historical commands that have been executed, and you can also execute historical commands.
Basic syntax:
history [选项]
Common parameter description of options:
- -N: display the most recent N records in the history;
- -c: Clear the current history command;
- -a: Write the commands in the historical command buffer to the historical command file;
- -r: Read the commands in the historical command file into the current historical command buffer;
- -w: Write the current history command buffer command to the history command file;
- -d: delete the offset command in the history
Example 1: Display all historical commands
history
Example 2: Display the 10 commands used recently.
history 10