Tab
键可以自动补全命令。Ctrl
+ C
可以中断当前程序,或者是已输入的字符。Ctrl
+ D
可以退出登录。command param1 param2 ...
-
后面带一个或多个字母,或者是 --
后面带一个词,表示选项。这里面的每一个字母或词,表示某个选项设置。如:很多命令用 -h
--help
表示输出该命令的帮助信息。docker run \
--name mysql \
-v /root/mysql:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password \
--restart always \
--network ljl \
--privileged=true \
-p 3306:3306 \
-d \
mysql
等价于docker run --name mysql -v /root/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password --restart always --network ljl --privileged=true -p 3306:3306 -d mysql
pwd
- 当前工作目录print working directory
pwd
/home/ding
whoami
- 当前用户名whoami
ding
ls
- 列出文件 / 目录list
ls
apt.tar.gz docker docker-tar jenkins-docker-certs status test
config docker.service jenkins-data mongo_test 'sudo systemctl stop'
ls /usr
bin games include lib lib32 lib64 libexec libx32 local sbin share src
-a
- 列出文件和目录,包括隐藏文件 / 目录ls -a
. .bashrc docker.service mongo_test status .Xauthority
.. .cache docker-tar .mysql_history .sudo_as_admin_successful
apt.tar.gz .config jenkins-data .profile 'sudo systemctl stop'
.bash_history config jenkins-docker-certs .rpmdb test
.bash_logout docker .local .ssh .viminfo
-l
- 列出文件和目录的具体信息,每个文件 / 目录一行ls -l
total 30880
-rw-r--r-- 1 root root 31578805 Apr 14 14:41 apt.tar.gz
drwxrwxr-x 5 foxconn foxconn 4096 Apr 18 15:19 config
drwxrwxr-x 2 foxconn foxconn 4096 Apr 14 10:56 docker
-rw-r--r-- 1 root root 1759 Apr 14 11:36 docker.service
drwxrwxr-x 2 foxconn foxconn 4096 Apr 15 18:55 docker-tar
drwxrwxrwx 22 root root 4096 Apr 20 17:49 jenkins-data
drwxr-xr-x 2 root root 4096 Apr 15 18:36 jenkins-docker-certs
drwxr-xr-x 4 root root 4096 Apr 8 17:19 mongo_test
-rw-r--r-- 1 root root 0 Apr 20 16:59 status
-rw-r--r-- 1 root root 7103 Apr 11 15:55 'sudo systemctl stop'
drwxrwxr-x 3 foxconn foxconn 4096 Apr 12 09:52 test
d
、文件 -
、链接文件 l
、块设备 b
、串行端口设备 c
)rwx
,分别为读取、写入、执行。如果没有对应权限,则空位为 -
ls -al
total 30944
drwxr-xr-x 14 foxconn foxconn 4096 Apr 21 10:34 .
drwxr-xr-x 4 root root 4096 Apr 13 15:05 ..
-rw-r--r-- 1 root root 31578805 Apr 14 14:41 apt.tar.gz
-rw------- 1 foxconn foxconn 5193 Apr 21 09:56 .bash_history
-rw-r--r-- 1 foxconn foxconn 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 foxconn foxconn 3771 Feb 25 2020 .bashrc
drwx------ 2 foxconn foxconn 4096 Apr 1 18:08 .cache
drwx------ 3 foxconn foxconn 4096 Apr 12 09:02 .config
drwxrwxr-x 5 foxconn foxconn 4096 Apr 18 15:19 config
drwxrwxr-x 2 foxconn foxconn 4096 Apr 14 10:56 docker
-rw-r--r-- 1 root root 1759 Apr 14 11:36 docker.service
drwxrwxr-x 2 foxconn foxconn 4096 Apr 15 18:55 docker-tar
drwxrwxrwx 22 root root 4096 Apr 20 17:49 jenkins-data
drwxr-xr-x 2 root root 4096 Apr 15 18:36 jenkins-docker-certs
drwxrwxr-x 3 foxconn foxconn 4096 Apr 21 09:45 .local
drwxr-xr-x 4 root root 4096 Apr 8 17:19 mongo_test
-rw------- 1 foxconn foxconn 18 Apr 12 08:59 .mysql_history
-rw-r--r-- 1 foxconn foxconn 807 Feb 25 2020 .profile
drwxr-xr-x 2 foxconn foxconn 4096 Apr 20 17:53 .rpmdb
drwx------ 2 foxconn foxconn 4096 Apr 18 15:15 .ssh
-rw-r--r-- 1 root root 0 Apr 20 16:59 status
-rw-r--r-- 1 foxconn foxconn 0 Apr 1 18:10 .sudo_as_admin_successful
-rw-r--r-- 1 root root 7103 Apr 11 15:55 'sudo systemctl stop'
drwxrwxr-x 3 foxconn foxconn 4096 Apr 12 09:52 test
-rw------- 1 foxconn foxconn 8080 Apr 15 08:56 .viminfo
-rw------- 1 foxconn foxconn 212 Apr 21 08:58 .Xauthority
cd
- 转到目录change directory
转到对应目录:
cd 目录路径
转到当前用户家目录,可以用其中任意一条:
cd ~
cd
mkdir
- 创建目录make directory
mkdir dir1
mkdir dir1 dir2 dir3
-p
- 递归创建目录mkdir -p dir1/dir2/dir3/dir4
如果路径上的某个目录不存在,则会一并创建。
cp
- 复制copy
file
复制到 dir
cp file dir
可以顺便重命名:
cp file dir/file2
file_1
、file_2
… 复制到 dir
cp file_1 file_2 ... dir
这时候最后一个必须是目录。
-i
- 若文件已存在,提示是否覆盖cp -i file dir
若文件已存在,提示:
cp: overwrite 'dir/file'?
填 y
才会覆盖。
-r
- 递归复制可用于目录的复制操作。
cp -r dir1 dir2
-d
:若源文件为链接文件的属性,则复制链接文件属性而非文件本身-p
:连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用)--preserve=all
:除了 -p
的权限相关参数外,还加入 SELinux 的属性。links、attr 等也复制-a
:相当于 -dr --preserve=all
mv
- 移动move
mv file1 file2
可以移动文件。如果源文件和目标文件在同一目录下,则相当于重命名。
mv file1 file2 file3 dest_path
移动多个文件。此时 dest_path
必须为目录。
-i
:询问是否覆盖已有文件-u
:源文件较新时才会覆盖已有文件-f
:强制覆盖已有文件rm
- 删除remove
rm file
rm file1 file2 file3
-i
- 删除前询问是否删除rm -i file
rm: remove regular file 'file'?
填 y
才会删除。
-r
- 递归删除rm -r dir
会删除 dir
目录及其下的所有文件。
-f
- 强制删除忽略不存在的文件,不会出现警告信息。
rm -f file
rm -rf /
rmdir
- 删除空目录remove directory
rmdir dir
如果不是空目录,会报错:
rmdir: failed to remove 'dir': Directory not empty
-p
- 递归删除连同上层空目录一并删除。
rmdir -p dir1/dir2/dir3/dir
cat
- 查看文件concatenate
cat file
-n
- 打印行号cat -n file
ding@ding-server:~$ cat -n file
1 # ~/.profile: executed by the command interpreter for login shells.
2 # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
3 # exists.
4 # see /usr/share/doc/bash/examples/startup-files for examples.
5 # the files are located in the bash-doc package.
6
7 # the default umask is set in /etc/profile; for setting the umask
8 # for ssh logins, install and configure the libpam-umask package.
9 #umask 022
10
11 # if running bash
12 if [ -n "$BASH_VERSION" ]; then
13 # include .bashrc if it exists
14 if [ -f "$HOME/.bashrc" ]; then
15 . "$HOME/.bashrc"
16 fi
17 fi
18
19 # set PATH so it includes user's private bin if it exists
20 if [ -d "$HOME/bin" ] ; then
21 PATH="$HOME/bin:$PATH"
22 fi
head
- 显示开始几行# 显示前 10 行
head file
# 显示前 20 行
head -n 20 file
# 后面 100 行不显示,显示剩余部分
head -n -100 file
tail
- 显示后面几行# 显示后 10 行
tail file
# 显示后 20 行
tail -n 20 file
# 前面 100 行不显示,显示剩余部分
tail -n -100 file
-f
- 持续刷新显示后面所接文件内容tail -f file
按 Ctrl
+ C
结束。
sudo
- 临时使用 root 权限执行命令sudo 命令
[sudo] password for ding:
需要输入自己的密码。
可以使用该命令的用户需由管理员配置好。
su
- 切换用户# 切换到指定用户 username
su username
# 切换到 root 的话可以省略用户名
su
# 以上切换用户后,目录为之前的目录
# 切换用户的同时切换到对应用户 username 的家目录
su - username
切换时需要输入要切换用户的密码。如未设置密码,则无法登录
在不知道 root 用户的密码的情况下,切换到 root(前提是能够运行 sudo
):
sudo su
cat .profile | grep "#"
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
# include .bashrc if it exists
# set PATH so it includes user's private bin if it exists
# set PATH so it includes user's private bin if it exists
sudo
时自动输入密码(可以用于脚本)echo "password" | sudo -S 命令
more
- 查看长文件more 文件
# 也可使用管道
其他命令 | more
less
- 查看长文件,但功能更多less 文件
# 也可使用管道
其他命令 | less
man
- 查看 Linux 文档manual
查看文档(实际上调用了 less 的功能):
man 关键词
libc
)/dev
下的文件某关键字在多个章节存在,则默认取序号最小的那个。
# 精确查询关键词在哪些章节存在
man -f 关键词
# 查看文档时定义章节序号
man 序号 关键词
# 模糊查询
man -k 关键词的一部分
info
:info 关键词