Command format and help information
Terminal command format
command [-options] [parameter]
- command: the command name, the English word or the abbreviation of the corresponding function
- -options: options, which can be used to control the command, or can be omitted
- parameter: parameter, can be zero, one or more
View command help information --help man
command --help
man command
help will print help information directly on the terminal.
man will open the documentation of the help information, which is short for manual.
Man operation shortcut keys
- Spacebar/f: scroll forward one screen
- b: scroll back one screen
- Enter key: advance one line
- q: quit
Common commands
clear screen clear
clear
View file information ls
ls: List the contents of the directory, short for list
ls
3.txt a aa.tar.gz b test.sh
ls -a: Display all subdirectories and files in the specified directory, including hidden files. "." stands for hidden files.
ls -a
. .. 3.txt a aa.tar.gz b .bash_history .bash_logout .bash_profile test.sh
ls -l: Display the detailed information of the file in a list, which can be directly abbreviated as "ll".
ls -l
-rw-rw-r-- 1 xxx xxx 174 Nov 17 11:03 3.txt
drwxrwxr-x 3 xxx xxx 4096 Nov 17 09:53 a
-rw-rw-r-- 1 xxx xxx 317 Nov 18 10:41 aa.tar.gz
drwxrwxr-x 2 xxx xxx 4096 Nov 18 10:44 b
-rwxrwxr-x 1 xxx xxx 28 Nov 15 11:21 test.sh
Change working directory cd
cd: Change the current working directory, which is short for change directory.
cd/cd ~: Switch to the current user's home directory, /home/user directory.
cd ~
[[email protected] ~]$
cd /: Change to the root directory.
cd /
[[email protected] /]$
cd ..: switch to the parent directory.
[[email protected] ~]$ cd ../..
[[email protected] /]$
cd -: switch to the last used directory, you can switch back and forth between the last two working directories.
[[email protected] a]$ cd -
[[email protected] b]$ cd -
[[email protected] a]$
# stands for root privilege users, $ stands for ordinary users.
relative path and absolute path
Relative path: Without / or ~ at the beginning of the path, it means relative to the directory where the current directory is located.
Absolute path: add / or ~ at the beginning of the path, indicating the directory location starting from the root directory or home directory.
Display the current directory path pwd
pwd: Check the current directory, which is short for print work directory
pwd
/home/xxx
Create a new directory mkdir
mkdir: Create a new directory, which is short for make directory.
mkdir 文件名
mkdir -p: Create directories recursively, and can directly create multi-level nested directories.
mkdir -p 文件名/文件名/文件名
If you do not add the -p parameter and create a multi-level directory directly, an error will be reported
mkdir 文件名/文件名/文件名
mkdir:cannot create directory ‘文件名/文件名/文件名’: No such file or directory
Create an empty file touch
touch: If the file does not exist, create an empty file with no content. If the file exists, modify the last modified date of the file
touch 文件名
delete file or directory rm
rm: Delete files, short for remove.
rm 文件名
rm -r: delete a directory or file
rm -r 目录名/文件名
rm -r can delete files and directories, even if the directory is not empty. If you delete a directory, you must add the -r parameter, otherwise an error will be reported.
rm 目录名
rm:cannot remove ‘目录名’: Is a directory
rm -f: Forcefully delete without prompting, even if there is no such file, no error will be reported.
rm -f 任意名称
rm -f cannot delete a directory. If the parameter of rm -f is the name of an existing directory, an error will be reported.
rm -f 实际存在的目录名
rm:cannot remove ‘实际存在的目录名’: Is a directory
Note: The combination of rm -rf forces all files in the current directory to be deleted and cannot be restored. Use with caution.
rm -i: Ask when deleting, confirm to delete the file.
rm -i 文件名
rm: remove regular file ‘文件名’?
输入y则删除,输入n则没有删除。即yes和no。
delete directory rmdir
rmdir: Delete directory, short for remove directory.
rmdir 目录名
rmdir will report an error if the deleted directory is not empty. rm -r will not report an error and can be successfully deleted.
rmdir 非空目录名
rmdir:failed to remove ‘非空目录名’: Directory not empty
copy file or directory cp
cp: Copy a file, which is short for copy.
cp 路径/文件名 目标位置路径
cp -a: Copy the directory.
cp -a 路径/目录名 目标位置路径
cp cannot directly copy the directory, and will report an error if -a is not added
cp 路径/目录名 目标位置路径
cp: omitting directory ‘目录名’
move/rename file or directory mv
mv: Move/rename files, short for move.
mv 路径/旧文件名 相同路径/新文件名
重命名
mv 路径/文件名 其他路径/文件名
移动
mv 路径/旧文件名 其他路径/新文件名
移动后重命名
In Linux, files and directories in the same directory cannot have the same name. Everything under Linux is a file, and a directory is also a file.
Therefore, when mv a directory, if there is a directory with the same name in the target location, it will be moved to this directory as a subdirectory, and mv will be used for moving. If there is no directory with the same name in the target location, it will be renamed to this directory name, and mv will be used for renaming.
mv 路径/文件名 任意路径/已存在目录名
移动到已有目录名下,作为子目录
mv 路径/文件名 任意路径/不存在目录名
重命名为本来不存在的目录名称
create link file ln
ln: Establish a hard link. After deleting the source file, the linked file can continue to be used, and the two files occupy the same hard disk space. Is the shorthand for link.
ln 源文件路径/源文件名称 放置链接路径
-rw-rw-r-- 1.txt xxx xxx 0 Nov 22 21:54 1.txt
ln hard links can only link ordinary files, not directories. Otherwise, an error will be reported.
ln 源目录路径/源目录名称 放置链接路径
ln: ‘源目录名称’: hard link not allowed for directory
ln -s: Create a software link. After deleting the source file, the linked file cannot be used any longer. The two files only occupy one hard disk space. Similar to shortcuts under windows.
ln -s 源文件路径/源文件名称 放置链接路径
lrwxrwxrwx 2.txt xxx xxx 3 Nov 22 21:56 2.txt -> a/2.txt
find file find
find: Find the target file, usually used together with -name.
find 路径(当前目录可省略) -name 目标文件名称
find -name: use the regular pattern to match the target file, and return all the file names that match the rule.
find a -name '*.txt'
搜索a目录下,文件名以.txt为扩展名的文件
find -name '1*'
搜索当前目录下,文件名以1开头的文件
find -name '*1*'
搜索当前目录下,文件名包含1的文件
View file content cat, less, more, head, tail
cat: display all the content of the file at once, suitable for viewing text files with less content
cat 文件名
less, more: can be used to display the content of the file in split screen, and only one page of content is displayed at a time. Suitable for viewing text files with a lot of content.
less 文件名
more 文件名
The difference between more and less
- less does not have to read the entire file, and the loading speed will be faster than more.
- For less, you can press the up and down arrow keys on the keyboard to switch the displayed content, but for more, you cannot use the up and down arrow keys to control the display.
- Less will not automatically exit when you turn to the end of the file, and more will automatically exit when you turn the page to the end.
- After less exits, the terminal will not leave the displayed file content, and after more exits, the displayed file content will be left on the terminal.
Shortcut keys for less and more operations
- Spacebar/f: scroll forward one screen
- b: scroll back one screen
- Enter key: advance one line
- q: quit
head, tail: only look at the front or back content. By default, the first/last ten lines of the file are displayed.
head 文件名
tail 文件名
head -n, tail -n: set the number of displayed lines.
head -n 行数 文件名
tail -n 行数 文件名
Packing/compression tar, gzip
pack/unpack
tar: Used for backing up files, you can pack each file into a large file, and you can also restore a packed large file into individual files.
tar -cvf 打包文件名.tar 路径/被打包的文件
打包
tar -xvf 打包文件名.tar
解包
- -c: represents packaging
- -x: represents unpacking
- -v: represents the process
- -f: represents the specified file name
-f must be placed at the end of the option, other options are optional.
compress/decompress
gzip: used to compress files. tar is only responsible for packaging files, but not compressing them. The combination of tar and gzip commands can be used to package and compress files, and the extension is generally xxx.tar.gz.
There is an option -z in the tar command to call gzip, -z: stands for compression.
tar -zcvf 打包文件名.tar.gz 路径/被打包压缩的文件
tar -zxvf 打包文件名.tar.gz
-C: You can specify the target path for file unpacking. The target directory must exist.
tar -zxvf 打包文件名.tar.gz -C 解压目标路径
Output text echo in the terminal
echo: The specified content will be displayed in the terminal, usually used together with redirection.
echo 在终端输出的内容
redirect>/>>
>>/>>: output or append the content displayed on the terminal to the specified file
- > Indicates output, overwriting the original file content.
- >> means to append, append the content to the end of the existing file.
echo 在终端输出的内容 > x.txt
echo 在终端输出的内容 >> x.txt
text editor vim
The vim editor has three modes: command mode, edit mode, and last line mode
Default is command mode, "i" enters edit mode, "Esc" exits command mode, ":" enters last line mode.
in last line mode
- wq: save and exit
- q: exit directly when there is no change
- q!: Force quit without saving after changing the content