本文主要目的为总结Linux常用操作命令,由于时间、篇幅所限,仅列出部分个人认为常用的命令及参数,对于不常用的参数暂不做介绍。
参考资料- 鸟哥的Linux私房菜
- https://blog.csdn.net/m0_50546016/article/details/119984255
目录 文章目录
- 简介
- 参考资料
- 目录
- 一. 目录与文件管理(cd/pwd/mkdir/rmdir/ls/cp/mv)
- 二. 文件内容操作(vim/cat/more/tail/grep/sed/echo)
- 三. 系统管理(ps/top/free/kill)
- 四. 网络管理(ifconfig/netstat)
- 五. 压缩解压(tar/unzip)
- 六. 包管理(apt/yum)
- cd
命令简介:变更当前目录
命令格式:cd DIRECTORY
示例:
$ cd /usr/bin
- pwd
命令简介:显示当前目录
命令格式:pwd [OPTION]…
示例:
$ pwd /usr/bin
- ls
命令简介:列出目录下的文件
命令格式:ls [OPTION]… [FILE]…
常用参数:
-a:列出全部文件,包括隐藏文件(文件名以 . 开头)
-l:列出文件属性与权限等数据
-R:递归的列出所有子目录及文件
示例:
$ ls /etc/samba/ -alR /etc/samba/: total 28 drwxr-xr-x 3 root root 4096 Sep 8 16:49 . drwxr-xr-x 81 root root 4096 Sep 24 15:58 .. -rw-r--r-- 1 root root 8 Jun 20 2019 gdbcommands -rw-r--r-- 1 root root 8989 Sep 8 16:49 smb.conf drwxr-xr-x 2 root root 4096 Jun 20 2019 tls /etc/samba/tls: total 8 drwxr-xr-x 2 root root 4096 Jun 20 2019 . drwxr-xr-x 3 root root 4096 Sep 8 16:49 ..
- mkdir
命令简介:创建新目录
命令格式:mkdir [OPTION]… DIRECTORY…
常用参数:
-p:递归创建上层目录
示例:
$ ls dir1 $ mkdir -p dir2/dir3 $ ls dir2 -R dir2: dir3 dir2/dir3:
- rm
命令简介:删除指定的文件或目录
命令格式:rm [OPTION]… [FILE]…
常用参数:
-f:强制,忽略不存在的文件,不出现警告信息
-r:递归删除目录和它们的内容,删除目录的常用选项
示例:
$ rm -rf /tmp/testdir
注:使用rm -rf时请仔细确认删除的文件夹。可将-rf参数移到命令最后,避免误操作,如键入rm -rf /后误碰回车键。
- cp
命令简介:复制文件和目录
命令格式:cp [OPTION]… SOURCE… DIRECTORY
常用参数:
-d:保留链接,如不加此参数,链接将被复制为真实的文件
-R/-r:递归复制目录
-a:等同于 -dr
示例:
$ ls -R .: dir1 ./dir1: test $ cp -r dir1 dir2 $ ls -R .: dir1 dir2 ./dir1: test ./dir2: test
- mv
命令简介:移动或重命名文件
命令格式:mv [OPTION]… SOURCE… DIRECTORY
示例:
$ ls -R .: dir1 dir2 ./dir1: test ./dir2: test $ mv dir1/test dir2/test1 $ ls -R .: dir1 dir2 ./dir1: ./dir2: test test1二. 文件内容操作(vim/cat/more/tail/grep/sed/echo)
- vim
命令简介:功能强大的文本编辑器,vim需要一定的学习成本,在此受篇幅限制不对操作进行介绍。
如想了解基本操作,请参阅vimtutor;如需使用vim作为IDE编程环境,可以参阅 https://blog.csdn.net/wooin/article/details/1858917 等文章。
命令格式:vim [options] [file …]
示例:
$ vim test.c
- cat
命令简介:将文件内容打印至终端
命令格式:cat [OPTION]… [FILE]…
示例:
# cat /var/log/kern.log Oct 8 09:13:07 debDev kernel: [ 110.853233] e1000: ens33 NIC link is Down Oct 8 09:13:09 debDev kernel: [ 112.868894] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None Oct 8 09:13:31 debDev kernel: [ 135.044942] e1000: ens33 NIC link is Down Oct 8 09:13:35 debDev kernel: [ 139.076362] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None Oct 8 09:13:37 debDev kernel: [ 141.092293] e1000: ens33 NIC link is Down Oct 8 09:13:43 debDev kernel: [ 147.141304] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None Oct 8 09:14:34 debDev kernel: [ 197.541782] e1000: ens33 NIC link is Down Oct 8 09:14:38 debDev kernel: [ 201.575456] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None ......
- more
命令简介:一个过滤器,将文本分页显示,通常配合管道( | )处理其他程序的输出,如cat。
与more命令类似但功能更加强大的命令还有less。
命令格式:more [options] file…
示例:
# cat /var/log/kern.log | more Oct 8 09:13:07 debDev kernel: [ 110.853233] e1000: ens33 NIC link is Down Oct 8 09:13:09 debDev kernel: [ 112.868894] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None Oct 8 09:13:31 debDev kernel: [ 135.044942] e1000: ens33 NIC link is Down Oct 8 09:13:35 debDev kernel: [ 139.076362] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None ...... #内容总共占满屏幕,此处省略部分内容 Oct 9 09:33:50 debDev kernel: [ 0.000000] vmware: using sched offset of 7858469686 ns Oct 9 09:33:50 debDev kernel: [ 0.000001] tsc: Detected 2904.004 MHz processor Oct 9 09:33:50 debDev kernel: [ 0.002864] e820: update [mem 0x00000000-0x00000fff] usable ==> reserved Oct 9 09:33:50 debDev kernel: [ 0.002865] e820: remove [mem 0x000a0000-0x000fffff] usable Oct 9 09:33:50 debDev kernel: [ 0.002868] last_pfn = 0xc0000 max_arch_pfn = 0x400000000 --More--
此时按空格可以翻页,更多功能请参考命令手册。
- tail
命令简介:输出文件末尾部分,常用于查看日志文件最新的部分
常用参数:
-n lines:指定输出文件末尾的行数
命令格式:tail [OPTION]… [FILE]…
示例:
# tail -n 5 /var/log/kern.log Oct 12 08:48:32 debDev kernel: [ 6.225529] audit: type=1400 audit(1633999712.355:6): apparmor="STATUS" operation="profile_load" profile="unconfined" name="man_groff" pid=500 comm="apparmor_parser" Oct 12 08:48:32 debDev kernel: [ 6.226830] audit: type=1400 audit(1633999712.355:7): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/sbin/tcpdump" pid=499 comm="apparmor_parser" Oct 12 08:48:32 debDev kernel: [ 6.380566] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready Oct 12 08:48:32 debDev kernel: [ 6.389846] e1000: ens33 NIC link is Up 1000 Mbps Full Duplex, Flow Control: None Oct 12 08:48:32 debDev kernel: [ 6.390564] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
- grep
命令简介:检索文件中的关键词
常用参数:
-i:检索时忽略大小写
-n:显示行号
-r:递归读取目录中的所有文件
命令格式:grep [OPTION…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
示例:
# grep -rn "nameserver" /etc /etc/resolv.conf:3:nameserver 223.5.5.5 /etc/resolv.conf:4:nameserver 114.114.114.114 /etc/services:36:nameserver 42/tcp name # IEN 116 /etc/init.d/nmbd:10:# Short-Description: Samba NetBIOS nameserver (nmbd)
注:使用 -e 参数时,PATTERNS为一个正则表达式。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本,功能十分强大,语法详情可以参考 https://www.runoob.com/regexp/regexp-syntax.html 。
- sed
命令简介:过滤和转换文本的流编辑器,常用于在脚本中对文本文件进行自动化处理。它可以实现对文件进行新增、删除、替换及打印等操作,可以配合正则表达式进行复杂的替换操作。
命令格式:sed [OPTION]… {script-only-if-no-other-script} [input-file]…
示例:
### 在文件第4行后添加一行"newline" $ sed -e 4anewline testfile ### 将ifconfig结果中,IP前面的部分删除 $ /sbin/ifconfig ens33 | grep 'inet ' | sed 's/^.*inet//g' 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255
- echo
命令简介:显示一行文本,可以方便的在脚本中打印信息或配合IO重定向向日志中追加信息
命令格式:echo [SHORT-OPTION]… [STRING]…
示例:
$ echo Hello World! Hello World!三. 系统管理(ps/top/free/kill)
- ps
命令简介:显示当前的进程状态
命令格式:ps [options]
常用参数:
a:用一个终端(tty)列出所有进程,或者与x选项一起使用时列出所有进程
u:以面向用户的格式显示
x:列出与ps相同EUID的所有进程,或者与a选项一起使用时列出所有进程
示例:
$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 169380 10064 ? Ss 09:06 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S 09:06 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 09:06 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 09:06 0:00 [rcu_par_gp] root 6 0.0 0.0 0 0 ? I< 09:06 0:00 [kworker/0:0H-kblockd] root 8 0.0 0.0 0 0 ? I< 09:06 0:00 [mm_percpu_wq] root 9 0.0 0.0 0 0 ? S 09:06 0:00 [ksoftirqd/0] root 10 0.0 0.0 0 0 ? I 09:06 0:08 [rcu_sched] root 11 0.0 0.0 0 0 ? I 09:06 0:00 [rcu_bh] root 12 0.0 0.0 0 0 ? S 09:06 0:00 [migration/0] ......
- top
命令简介:显示linux进程
命令格式:top [options]
示例:
$ top
top - 14:49:11 up 5:42, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 159 total, 1 running, 158 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 2978.9 total, 2619.5 free, 160.6 used, 198.8 buff/cache
MiB Swap: 975.0 total, 975.0 free, 0.0 used. 2653.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
18 root 20 0 0 0 0 I 0.3 0.0 0:19.37 kworker/1:0-events_freezable
1383 qinxy 20 0 11104 3512 3060 R 0.3 0.1 0:00.02 top
1 root 20 0 169380 10064 7880 S 0.0 0.3 0:01.32 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-kblockd
8 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
10 root 20 0 0 0 0 I 0.0 0.0 0:08.64 rcu_sched
11 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_bh
12 root rt 0 0 0 0 S 0.0 0.0 0:00.07 migration/0
13 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/0:1-memcg_kmem_cache
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/1
16 root rt 0 0 0 0 S 0.0 0.0 0:00.43 migration/1
17 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/1
19 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/1:0H-kblockd
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/2
......
- free
命令简介:显示系统中可用和已用的内存数量
命令格式:free [options]
常用参数:
-h:显示易读的输出
-m:以MB为单位显示输出
示例:
$ free -h
total used free shared buff/cache available
Mem: 2.9Gi 160Mi 2.6Gi 9.0Mi 198Mi 2.6Gi
Swap: 974Mi 0B 974Mi
- kill
命令简介:向进程发送信号
命令格式:kill [options] […]
常用参数:
-:指定发送的信号,默认信号为 TERM
示例:
$ ps au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 528 0.0 0.0 5612 1680 tty1 Ss+ 09:06 0:00 /sbin/agetty -o -p -- u --noclear tty1 linux qinxy 659 0.0 0.1 8628 5312 pts/0 Ss 09:08 0:00 -bash qinxy 1116 0.0 0.1 8048 4956 pts/1 Ss 13:57 0:00 -bash qinxy 1475 3.2 0.2 17500 8332 pts/1 S+ 15:42 0:00 vim qinxy 1477 0.0 0.0 10632 3008 pts/0 R+ 15:42 0:00 ps au $ kill -9 1475 $ ps au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 528 0.0 0.0 5612 1680 tty1 Ss+ 09:06 0:00 /sbin/agetty -o -p -- u --noclear tty1 linux qinxy 659 0.0 0.1 8628 5312 pts/0 Ss 09:08 0:00 -bash qinxy 1487 0.3 0.1 7916 4740 pts/1 Ss+ 15:44 0:00 -bash qinxy 1493 0.0 0.1 10632 3056 pts/0 R+ 15:44 0:00 ps au
注: -9 为 KILL 信号,用于强制杀死进程。
四. 网络管理(ifconfig/netstat)- ifconfig
命令简介:查询、配置网络接口,常用于对网络接口的IP、掩码、MTU、MAC地址等信息进行配置
命令格式:ifconfig [-v] interface [aftype] options | address …
常用参数:
-a:显示所有当前可用的接口(包括down状态的接口)
示例:
# ifconfig -a ens33: flags=4163mtu 1500 inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fe15:54f5 prefixlen 64 scopeid 0x20 ether 00:0c:29:15:54:f5 txqueuelen 1000 (Ethernet) RX packets 12453 bytes 1003177 (979.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11367 bytes 4079246 (3.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4098 mtu 1500 ether 00:0c:29:15:54:ff txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 172 bytes 10320 (10.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 172 bytes 10320 (10.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # ifconfig ens37 192.168.2.100 netmask 255.255.255.0 # ifconfig ens33: flags=4163 mtu 1500 inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fe15:54f5 prefixlen 64 scopeid 0x20 ether 00:0c:29:15:54:f5 txqueuelen 1000 (Ethernet) RX packets 12596 bytes 1014007 (990.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11448 bytes 4087888 (3.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4099 mtu 1500 inet 192.168.2.100 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:15:54:ff txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 172 bytes 10320 (10.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 172 bytes 10320 (10.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- netstat
命令简介:打印网络连接、路由表、接口状态等信息
命令格式:netstat [options]
常用参数:
-r:显示内核路由表
-i:显示所有网络接口
-s:显示协议统计信息
-n:显示数字地址,不试图确定符号主机、端口或用户名
-p:显示socket所属的PID和进程名
-l:仅显示监听socket
-a:显示监听、非监听socket,配合-i时显示所有接口信息(包括未up接口)
-t: TCP协议
-u: UDP协议
示例:
# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 571/smbd tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 571/smbd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 532/sshd # netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.10.2 0.0.0.0 UG 0 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
- ping
命令简介:向网络主机发送ICMP ECHO_REQUEST报文,常用于诊断网络问题
命令格式:ping [options] destination
常用参数:
-c count:在发送count个ECHO_REQUEST报文后停止发送
-i interval:在发送每个数据包间等待interval秒
-I interface:interface可以是一个接口地址或接口名称,它将源地址或接口设置为指定的地址或接口
-s packetsize:指定发送的字节数,默认为56(加8字节ICMP头共64字节)
示例:
$ ping -c 3 -s 1000 192.168.10.2 PING 192.168.10.2 (192.168.10.2) 1000(1028) bytes of data. 1008 bytes from 192.168.10.2: icmp_seq=1 ttl=128 time=0.119 ms 1008 bytes from 192.168.10.2: icmp_seq=2 ttl=128 time=0.352 ms 1008 bytes from 192.168.10.2: icmp_seq=3 ttl=128 time=0.569 ms五. 压缩解压(tar/unzip)
- tar
命令简介:归档管理工具,用于压缩、解压文件
命令格式:tar {A|c|d|r|t|u|x} [options] [ARG…]
常用参数:
-c:创建归档
-x:解压归档
-t:查看归档文件
-f:归档文件名
-v:详细列出所处理的文件
-p:解压文件权限信息
–exclude=PATTERN:排除PATTERN文件
-j:使用bzip2过滤归档
-J:使用xz过滤归档
-z:使用gzip过滤归档
示例:
### 压缩/etc目录 $ tar czf etc.tar.gz /etc ### 解压aaa.tar.gz $ tar xf aaa.tar.gz
注:-c/x/t等标识操作的参数仅能存在一个。
- unzip
命令简介:解压zip文件工具
命令格式:unzip [options] file[.zip] [file(s) …] [-x xfile(s) …] [-d exdir]
示例:
$ unzip aaa.zip六. 包管理(apt/yum)
包管理器是在电脑中自动安装、配制、卸载和升级软件包的工具组合,几乎每一个Linux发行版都有自己的包管理器。
一般来说使用较多的Linux发行版分两大系列:
RedHat系:如Redhat、CentOS、Fedora等,其软件包格式为rpm
Debian 系:如Debian、Ubuntu及其衍生版等,其软件包格式为deb
- apt
命令简介:deb包管理的命令行接口
命令格式:apt [options] {command}
示例:
### 更新软件源 # apt update ### 更新软件 # apt upgrade ### 安装软件gcc # apt install gcc
- yum
命令简介:rpm包管理的命令行接口
命令格式:yum [options] [command] [package …]
示例:
### 更新软件 # yum update ### 安装软件gcc # yum install gcc



