这里总结了常用的近 30 个 linux 命令及小技巧,把这些命令学了就可以对 linux 进行基本的操作了。
man
查看命令的使用手册,当对命令参数或格式不确定时可以查看。
- 格式:
man <命令>
快捷键:
/<关键字>
从上往下搜索?<关键字>
从下往上搜索n和N
在搜索时定位到下一个关键字和上一个关键字
cd(change direction)
切换目录路径
1cd .. # 返回上一级
2cd - # 切换到上次访问路径
3cd ~ # 切换到家目录
cp(copy)
拷贝文件、文件夹。
参数:
-r
复制时递归进行, 常用与复制目录-a
在复制时保留文件属性, 相当于参数组合-pdr-t
复制到执行的目录, 通常 cp 的第二个参数为复制到的目的路径, 但当省略第一个参数是, 需要用-t 来指定目的路径-d
若对象为"链接", 则保留"链接"文件属性,即复制后仍为"链接"文件-p
复制时保留文件原属性
小技巧
1cp a.conf{,.bak} # 拷贝配置文件,命令等价于cp a.conf a.conf.bak
2# 效果
3[root@lqc ~]# cp a.conf{,.bak}
4[root@lqc ~]# ll
5-rw-r--r-- 1 root root 14 Aug 1 21:35 a.conf
6-rw-r--r-- 1 root root 14 Aug 1 21:41 a.conf.bak
mv
移动目录、文件夹。
格式:mv <src_dir> <dest_dir>
小技巧
1mv ./a.conf{,.bak} # 把文件设为备份
2mv ./a.conf{.bak,} # 还原备份
rm
删除文件、文件夹。 危险命令!重要数据、配置文件删前记得备份。
格式:rm <option> <file/dir>
或rm <file/dir> <option>
参数:
-r
递归删除,用于删除目录-f
强制删除
小技巧 在重要的服务器修改配置文件或者删除数据是很危险的,笔者曾经就把项目的数据库文件给误删了(rm -rf ./*), 幸好之前有意识备份了一下,否则要被骂惨了。通常在执行 rm 命令时不能直接执行,以提示用户此操作危险。因此常设置 rm 别名(alias)。
1# 设置别名
2alias rm="echo 操作危险,禁止直接执行"。
3# 真正执行删除
4\rm a.txt
5/bin/rm a.txt
ls (list)
查看文件夹下的目录和文件。
参数:
-l
列表显示目录信息-h
human->h 存储单位格式化,以 M、G 等人类易读单位显示-i
在列表信息中显示inode
-t
列表信息以时间顺序显示-tr
列表信息以时间逆序(time reverse)显示
常用:
1ls -ilh
2inode 权限 硬链接数 属主 属组 大小 时间 文件/文件夹名称
334111974 drwxr-xr-x. 3 root root 101 Jul 21 10:36 abrt
417446237 -rw-r--r--. 1 root root 16 Jul 21 10:39 adjtime
516778297 -rw-r--r--. 1 root root 1.5K Apr 1 12:29 aliases
616777285 -rw-r--r--. 1 root root 12K Jul 21 10:40 aliases.db
7 90884 drwxr-xr-x. 2 root root 261 Jul 21 15:42 alternatives
817470583 -rw-------. 1 root root 541 Aug 9 2019 anacrontab
cat
查看文件信息。
格式:cat <file>
使用
1# 在文件尾部追加信息, xxxx为内容
2cat >a.txt<EOF
3xxxx
4xxxx
5xxxx
6EOF
7
8# 查看信息
9cat a.txt
10[root@lqc ~]# cat a.txt
11slkdjfl
12123
13456
小技巧
通过管道符 | 把第一个命令的输出做为grep
的输入来过滤内容。里的 cat 命令用来模拟标准输出。其实,grep 命令可以直接对文本文件进行过滤,见图 2。
grep 是个强大的命令,linux 三剑客之一,linux 玩的这个命令必须熟练掌握,学习可点另一篇文章 linux 三剑客
alias
为命令设置别名,在 shell 中通过命令设置是暂时的,只有在/etc/profile或/etc/bashrc
中设置才是永久生效的。
示例:alias rm="echo 禁止直接删除"
取消别名: unalias <别名>
使用
1#查看别名
2[root@lqc ~]# alias
3alias cp='cp -i'
4alias egrep='egrep --color=auto'
5alias fgrep='fgrep --color=auto'
6alias grep='grep --color=auto'
7alias l.='ls -d .* --color=auto'
8alias ll='ls -l --color=auto'
9alias ls='ls --color=auto'
10alias mv='mv -i'
11alias rm='rm -i'
12alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
13
14#设置别名
15[root@lqc ~]# alias rm='禁止使用rm执行删除'
16#进行查看
17[root@lqc ~]# alias
18alias cp='cp -i'
19alias egrep='egrep --color=auto'
20alias fgrep='fgrep --color=auto'
21alias grep='grep --color=auto'
22alias l.='ls -d .* --color=auto'
23alias ll='ls -l --color=auto'
24alias ls='ls --color=auto'
25alias mv='mv -i'
26alias rm='禁止使用rm执行删除'
27alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
28
29#删除别名
30[root@lqc ~]# unalias rm
31#查看
32[root@lqc ~]# alias
33alias cp='cp -i'
34alias egrep='egrep --color=auto'
35alias fgrep='fgrep --color=auto'
36alias grep='grep --color=auto'
37alias l.='ls -d .* --color=auto'
38alias ll='ls -l --color=auto'
39alias ls='ls --color=auto'
40alias mv='mv -i'
41alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which
查看命令文件路径,仅用户可执行文件。
格式:which <cmd>
1[root@lqc ~]# which rm
2/usr/bin/rm
3[root@lqc ~]# which alias
4/usr/bin/alias
whereis
查看命令路径详细信息, 可以通过该命令判断命令是否存在(配合 shell 编程)。
格式:whereis <cmd>
1[root@lqc ~]# whereis rm
2rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
3[root@lqc ~]# whereis mkdir
4mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
5[root@lqc ~]# whereis yum
6yum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8
source
重新加载 shell 配置文件,这个名令挺常用,下载一些软件总需要配置环境变量,配置完后需要使用此命令刷新。
常用可加载配置文件:
- /etc/profile 系统变量、用户定义变量
- /etc/bashrc 系统函数
- ~/.bash_profile
- ~/.bashrc
systemctl 重要
管理程序服务运行状态。
常用:network、httpd、firewalld、mysqld 等按装的软件
格式:systemctl <cmd> <service_name>
常用命令:
- 启动服务 start,示例:
systemctl start <service_name>
- 停止服务 stop
- 重启服务 restart
- 查看服务运行状态 status
- 让服务开机自启 enable
- 关闭开机自启服务 disable
- 确认服务是否运行 is-active
- 确认服务是否开机自启 is-enable
使用
以邮件服务 postfix 为例
1# postfix开始状态
2[root@lqc ~]# systemctl status postfix
3● postfix.service - Postfix Mail Transport Agent
4 Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
5 Active: active (running) since Wed 2020-08-26 15:49:52 CST; 33min ago
6 Process: 1253 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
7 Process: 1249 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
8 Process: 1243 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
9 Main PID: 1325 (master)
10 CGroup: /system.slice/postfix.service
11 ├─1325 /usr/libexec/postfix/master -w
12 ├─1326 pickup -l -t unix -u
13 └─1327 qmgr -l -t unix -u
14
15Aug 26 15:49:50 lqc.com systemd[1]: Starting Postfix Mail Transport Agent...
16Aug 26 15:49:52 lqc.com postfix/postfix-script[1323]: starting the Postfix mail system
17Aug 26 15:49:52 lqc.com postfix/master[1325]: daemon started -- version 2.10.1, configura...ix
18Aug 26 15:49:52 lqc.com systemd[1]: Started Postfix Mail Transport Agent.
19Hint: Some lines were ellipsized, use -l to show in full.
20
21# 停止postfix服务
22[root@lqc ~]# systemctl stop postfix
23# 查看状态
24[root@lqc ~]# systemctl status postfix
25● postfix.service - Postfix Mail Transport Agent
26 Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
27 Active: inactive (dead) since Wed 2020-08-26 16:24:03 CST; 3s ago
28 Process: 1642 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
29 Process: 1253 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
30 Process: 1249 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
31 Process: 1243 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
32 Main PID: 1325 (code=killed, signal=TERM)
33
34Aug 26 15:49:50 lqc.com systemd[1]: Starting Postfix Mail Transport Agent...
35Aug 26 15:49:52 lqc.com postfix/postfix-script[1323]: starting the Postfix mail system
36Aug 26 15:49:52 lqc.com postfix/master[1325]: daemon started -- version 2.10.1, configura...ix
37Aug 26 15:49:52 lqc.com systemd[1]: Started Postfix Mail Transport Agent.
38Aug 26 16:24:03 lqc.com systemd[1]: Stopping Postfix Mail Transport Agent...
39Aug 26 16:24:03 lqc.com systemd[1]: Stopped Postfix Mail Transport Agent.
40Hint: Some lines were ellipsized, use -l to show in full.
41
42# 禁止postfix开机自启
43[root@lqc ~]# systemctl disable postfix
44Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
45
46# 开启postfix服务
47[root@lqc ~]# systemctl start postfix
ln
创建链接文件。分为软链接和硬链接。
格式:
ln <src> <dest>
把 src 链接到 dest,不加参数默认为硬链接,==注意:硬链接只能针对文件==ln -s <src> <dest>
把 src 链接到 dest,软链接文件、目录均可链接。软链接就像一个快捷方式。
软硬链接区别: 从图中可以看导硬链接更像文件的一个副本,当删除文件时文件不会消失,我们还可以通过硬链接文件访问。而软链接指向文件删除后文件删除后,文件索引信息消失,文件等同删除,当再次往软链接写入文件,会新建与指向文件同名的文件,但二者已经不是一个文件。 使用
1# 创建测试文件
2[root@lqc ~/test]# touch a.txt
3[root@lqc ~/test]# echo "hello world" > a.txt
4[root@lqc ~/test]# cat a.txt
5hello world
6
7# 创建软链接, 也称符号链接
8[root@lqc ~/test]# ln -s /root/test/a.txt /root/test/a_softLink.txt
9[root@lqc ~/test]# ll
10total 4
11lrwxrwxrwx 1 root root 16 Aug 26 16:30 a_softLink.txt -> /root/test/a.txt
12-rw-r--r-- 1 root root 12 Aug 26 16:29 a.txt
13
14#创建硬链接
15[root@lqc ~/test]# ln /root/test/a.txt /root/test/a_hardLink.txt
16[root@lqc ~/test]# ll
17total 8
18-rw-r--r-- 2 root root 12 Aug 26 16:29 a_hardLink.txt
19lrwxrwxrwx 1 root root 16 Aug 26 16:30 a_softLink.txt -> /root/test/a.txt
20-rw-r--r-- 2 root root 12 Aug 26 16:29 a.txt
ss 与 netstat
二者功能相同,都是查看 TOC、UDP 接口情况。在 centos7 中不能直接使用 netstat,需要下载 net-tools 软件包。
参数:
- -l list — 列表显示网络服务信息
- -n number — 以数字方式进行显示服务好,例如 ssh 显示为 22
- -t tcp — 网络协议
- -u udp — 网络协议
- -p process — 显示服务进程信息
常用&小技巧
1netstat -lntup
2
3# 显示的同时过滤信息
4netstat -lntup |grep <关键字>
tail、head、more
tail 显示尾部的分信息,head 显示头部的。这个命令经常用来查看日志文件(/var/log/..)
参数:-n 显示多少行,默认 10 行。
1# 示例,head相同
2tail -n 100 /var/log/secure
3
4# 持续追踪日志,使用后shell将阻塞,有日志就打印到标准输出
5tail -f /var/log/secure
yum
包管理工具,下载,安装软件很方便。
安装软件: yum install <pkg_name> -y
-y 交互时自动选择 yes
查看主机安装软件包: yum grouplist
安装未安装的软件包: yum groupinstall -y <未装软件包名>
在 repo 中搜索相关信息: yum provides <key_val>
常用:
1yum repolist all #列出所有仓库
2yum list all #列出仓库中所有软件包
3yum info <pkg-name> #查看软件包名称
4yum install <pkg-name> #安装软件包
5yum reinstall <pkg-name> #重新安装软件包
6yum update <pkg-name> #更新软件包
7yum remove <pkg-name> #移除软件包
8yum clean all #清除所有仓库缓存
9yum check-update #检查可更新的软件包
10yum grouplist #查看系统中已经安装的软件包租
11yum groupinstall <pkg-group> #安装指定软件包租
12yum groupremove <pkg-group> #移除指定软件包租
13yum groupinfo <pkg-group> #查询指定的软件包租信息
rpm
centos、readhat 包管理工具,提供包的安装、查询、更新等管理功能。
参数:
-q
查询-a
所有-l
列表显示-f
文件-i
install 安装
常用:
1rpm -qa <filename> #在系统中所有安装包查询是否安装
2rpm -ql <filename> #查询安装包路径
3rpm -qf <filename> #查询命令属于哪个包
4rpm -ivh <file.rpm> #安装软件
5rpm -Uvh <file.rpm> #更新软件
6rpm -e <filename> #卸载软件
7rpm -qpi <filename> #列出软件描述信息
8rpm -qpl <filename> #列出软件文件信息的命令格式
sh、bash
运行 shell 脚本。
格式:sh <shell_script>
free
查看内存使用信息。
使用:
1free -h
ps
查看进程信息.
常用:
1ps <pid> # 查看指定线程信息
2ps -ef #查看进程详细信息, 常配合grep使用
top、升级版 htop
查看进程信息,相当于 win 任务管理器,直接输入命令进行使用
tar
格式:
tar -zcvf <压缩文件路径(文件名后缀.tar.gz)> <压缩源src>
压缩tar -xvf <extract_dir> <dir_file_tar.gz>
解压
注意:压缩时尽量用相对路径,否则有异常提示
压缩命令:
1tar -zcvf aaa.tar.gz ./* #压缩当前目录下所有文件
2tar -acvf aaa.tar.gz /opt/data/* #压缩指定路径下文件
3
4# 压缩时排除
5tar -zcvf <dir_file_tar.gz> <src_dir> --exclude-from=<exclude_dir/file>
6
7# 压缩时批量排除
8tar -zcvf <dir_file_tar.gz> <src_dir> --exclude-from=<exclude_file>
9
10# 多个h参数,把链接指向文件/目录找到并压缩
11tar -zcvhf <dir_file_tar.gz> <src> <src>....
参数:
-z
压缩方式 zip-c
小写字母,create 创建压缩文件-C
大写字母,指定到解压目录, 默认当前目录-v
显示压缩过程-x
extract 提取–解压-f
指定压缩文件路径信息-t
list the contents of an archive-p
保持原来的权限和与属性-h
备份文件是链接文件时找到源文件并压缩
检查是否压缩成功:
- diff
- vimdiff
- tar tf <archieve_file_tar.gz>
列表列出压缩包内容: diff/vimdiff <file_1> <file_1>
注意:
- 当以绝对路径指定压缩源文件时,默认将跟路径“/”去掉并输出提示信息,常用相对路径
- –exclude-from 可以是排除目录或包含排除目录的文件,可以用 find 查找生成
tree
层级显示目录。
格式:tree <dir>
参数:
- 不参数,将递归显示文件和目录
-L
level 显示层级数-d
direct,仅显示目录
find
在目录下查找文件/目录。
格式:find <dir> -type <d/f> -name <d/f name>
参数:
- -type 指明查找类型,d 目录,f 文件
- -name 指定匹配名称的关键字,可以使用*模糊匹配
示例:
1find / -type f -name "*nginx*" # 在/目录下查找名称包含nginx的文件
grep
在文本文件或管道输入流等中查找关键信息。
格式:grep [option] <key_str> <src>
示例:
1history | grep "rm" # 在执行命令历史中查找rm命令
2grep -E "200$" ./a.txt # 在a.txt文件中找出200结尾的行
useradd
创建用户
格式: useradd [option] 用户名
常用:
1# 添加普通用户,并设置密码
2echo "passwd" | useradd normal_user --password --stdin
3useradd normal_user
4passwd normal_user
5
6# 添加虚拟用户, nginx为用户名
7useradd nginx -M -s /sbin/shell
passwd
以交互的方式设置密码,直接使用 passwd 是修改当前用户的登录密码,passwd 加用户名修改指定用户的密码(root 操作)。
chmod
修改文件、文件夹的权限。 常用参数:-R 递归修改权限
1chmod nnn <file/dir> #直接赋予权限 , chmod 777赋予所有者,用户组,其他用户权力
2chmod +<r/w/x> <file/dir> #赋予所有者、用户组、其用户权力
3chmod -<r/w/x> <file/dir> #移除所有者、用户组、其他用户权力
4chomod <(u/g/o)=(+/-)(r/w/x)> <file/dir> # u user,g group, o other 示例:chmod u=+x 给user组设置运行权限
5
6# 给予所有权限
7chmod -R 777 <dir/file>
chown
修改文件、文件夹的属主数组。 常用参数:
- -R 递归修改权限
示例:
1# 两命令功能相同
2chown -R nginx.nginx ./nginx/html # 为html文件夹及其子文件夹、文件设置属主数组为nignx
3chown -R nginx:nginx ./nginx/html # 为html文件夹及其子文件夹、文件设置属主数组为nignx
(完)