Linux拓展
约 1685 字大约 6 分钟
2025-03-21
1. 磁盘相关命令
Linux磁盘管理常用命令为 df、du
- df:检查磁盘空间占用情况(并不能查看某个目录占用的磁盘大小)
- du:显示文件或目录所占的磁盘空间
1.1 df 命令
检查文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。
命令格式:
df [选项] [文件系统]
选项:
- -a:显示所有文件系统的磁盘空间占用情况。
- -h:以人类可读的方式显示磁盘空间。
- -i:显示inode信息。
- -k:以KB为单位显示磁盘空间。
- -m:以MB为单位显示磁盘空间。
- -x:显示除系统分区以外的其他文件系统的磁盘空间占用情况。
测试:
# 将系统内所有的文件系统列出来!
# 在 Linux 底下如果 df 没有加任何选项
# 那么默认会将系统内所有的 (不含特殊内存内的文件系统与 swap) 都以 1 Kbytes 的容量来列出来!
[root@localhost /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 889100 0 889100 0% /dev
tmpfs 899460 704 898756 1% /dev/shm
tmpfs 899460 496 898964 1% /run
tmpfs 899460 0 899460 0% /sys/fs/cgroup
/dev/vda1 41152812 6586736 32662368 17% /
tmpfs 179896 0 179896 0% /run/user/0
# 将容量结果以易读的容量格式显示出来
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 869M 0 869M 0% /dev
tmpfs 879M 708K 878M 1% /dev/shm
tmpfs 879M 496K 878M 1% /run
tmpfs 879M 0 879M 0% /sys/fs/cgroup
/dev/vda1 40G 6.3G 32G 17% /
tmpfs 176M 0 176M 0% /run/user/0
# 将 /etc 底下的可用的磁盘容量以易读的容量格式显示
[root@localhost /]# df -h /etc
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.3G 32G 17% /
1.2 du 命令
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的,这里介绍Linux du命令。
命令格式:
du [选项] [文件或目录]
选项:
- -a:显示目录下所有文件和目录的大小。
- -h:以人类可读的方式显示文件和目录的大小。
- -k:以KB为单位显示文件和目录的大小。
- -m:以MB为单位显示文件和目录的大小。
- -s:显示总计的大小。
2. 进程管理
查看系统进程的命令,可以查看系统中正在运行的进程,以及进程的详细信息。
命令格式:
ps [选项]
选项:
- -a:显示所有进程。
- -e:显示所有进程,包括其他用户的进程。
- -f:显示进程的详细信息,包括命令行参数。
- -u:指定用户的所有进程。
案例:查看mysql进程
[root@localhost ~]# ps -ef | grep mysql
root 1112 1 0 10:56? 00:00:00 /usr/sbin/mysqld
mysql 1113 1112 0 10:56? 00:00:00 /usr/sbin/mysqld
root 1120 1113 0 10:56? 00:00:00 /usr/sbin/mysqld
案例:删除mysql进程
[root@localhost ~]# kill -9 1112
[root@localhost ~]# kill -9 1113
[root@localhost ~]# kill -9 1120
3. 管道符 |
管道符(|)是Linux/Unix操作系统中使用的一个命令,它是将一个命令的输出作为另一个命令的输入。此命令可以连续地运行多个命令,前一个命令的输出作为后一个命令的输入,从而实现复杂的功能。
命令格式:
命令1 | 命令2
管道符使用案例:
# 查看redis配置文件的端口号
[root@localhost ~]# cat /etc/redis.conf | grep port
port 6379
4. find 命令
find命令是一种搜索文件命令,它可以查找符合条件的文件,并对文件执行指定的操作。
命令格式:
find [路径] [条件]
条件:
- -name:按照文件名查找文件。
- -iname:按照文件名忽略大小写查找文件。
- -type:按照文件类型查找文件,如:d表示目录,f表示普通文件,l表示符号链接文件。
案例:查找/etc目录下所有以.conf结尾的文件
[root@localhost ~]# find /etc -name "*.conf"
/etc/profile.d/colorls.sh
/etc/profile.d/vte.sh
/etc/profile.d/vte-2.91.sh
...
5. grep 命令
grep命令是一种搜索文本命令,它可以从指定的文件中搜索特定的字符串,并输出匹配的行。
命令格式:
grep [选项] [模式] [文件]
选项:
- -i:忽略大小写。
- -n:显示匹配行及行号。
- -v:显示不匹配的行。
案例:在文件file.txt中查找包含hello的行
[root@localhost ~]# cat file.txt
hello world
hello linux
hello world
hello macos
[root@localhost ~]# grep hello file.txt
hello world
hello linux
hello world
hello macos
6. whereis 命令
whereis命令是搜索命令的位置命令,它可以搜索某个命令的可执行文件、帮助文件、源代码文件等。
命令格式:
whereis [命令名称]
案例:搜索ls命令的位置
[root@localhost ~]# whereis ls
ls: /bin/ls /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1/ls.1.bz2 /usr/share/man/man1/ls.1.gz.gz /usr/share/man/man1/ls.1.bz2.bz2
7. wc 命令
wc命令是统计指定文件中的行数、字数、字节数的命令。
命令格式:
wc [选项] [文件]
选项:
- -c:统计字节数。
- -l:统计行数。
- -m:统计字符数。
- -w:统计单词数。
案例:统计file.txt文件中的行数、字数、字节数
[root@localhost ~]# cat file.txt
hello world
hello linux
hello world
hello macos
[root@localhost ~]# wc file.txt
4 20 100 file.txt
结果:文件file.txt共有4行,20个字符,100字节。
案例:统计总共有多个用户
[root@localhost ~]# cat /etc/passwd | wc -l
79
8. 压缩和解压缩
8.1 tar 命令
tar命令是Linux/Unix操作系统中用于对文件进行打包、压缩和解压缩的命令。
命令格式:
tar [选项] [文件]
选项:
- -c:创建打包文件。
- -x:解压文件。
- -t:查看打包文件内容。
- -z:使用gzip压缩。
- -j:使用bzip2压缩。
- -v:显示压缩过程。
案例:压缩/hello.txt文件位hello.tar.gz文件
[root@localhost ~]# tar -zcvf hello.tar.gz /hello.txt
[root@localhost ~]# ls
hello.tar.gz hello.txt
案例:解压hello.tar.gz文件到/tmp目录
[root@localhost ~]# tar -zxvf hello.tar.gz -C /tmp
[root@localhost ~]# ls /tmp
hello.txt
8.2 gzip 命令
gzip命令是Linux/Unix操作系统中用于压缩文件的命令。
命令格式:
gzip [选项] [文件]
选项:
- -c:将压缩后的文件输出到屏幕上。
- -d:解压缩文件。
- -f:强制压缩或解压缩文件。
- -v:显示压缩过程。
案例:压缩/hello.txt文件为hello.txt.gz文件
[root@localhost ~]# gzip -cv /hello.txt
[root@localhost ~]# ls
hello.txt hello.txt.gz
案例:解压hello.txt.gz文件
[root@localhost ~]# gzip -dv hello.txt.gz
[root@localhost ~]# ls
hello.txt
8.3 zip 命令
zip命令是Linux/Unix操作系统中用于压缩文件的命令。
命令格式:
zip [选项] [文件]
选项:
- -r:递归压缩目录。
- -j:压缩文件,并保持原有文件。
- -l:显示压缩文件内容。
- -q:安静模式。
- -v:显示压缩过程。
案例:压缩/hello.txt文件为hello.zip文件
[root@localhost ~]# zip hello.zip /hello.txt
[root@localhost ~]# ls
hello.txt hello.zip
案例:解压hello.zip文件
[root@localhost ~]# unzip hello.zip
Archive: hello.zip
extracting: hello.txt
[root@localhost ~]# ls
hello.txt hello.zip