Linux系统文件与目录内容检索(小白的“升级打怪”成长之路)

目录

一、文件和目录内容检索处理命令

1、grep筛选

2、find查找

3、sort排序

4、uniq去重

5、tr转换

6、cut 切割

7、which 命令

8、whereis命令

9、diff命令

二、文件与目录归档压缩命令

1、tar

2、zip/unzip

3、gzip/gunzip

4、bzip2 / bunzip2

三、统计命令

1、wc

2、du


一、文件和目录内容检索处理命令

1、grep筛选

在文本中查找指定的字符串所在的行

语法:

grep [选项] file

选项:

-i :忽略大小写

-v :反转匹配,只显示不匹配的g行

-c :记数,只输出匹配行的数量

-n :显示匹配行号及其行号

-l(小写L) :只输出包含匹配字符的文件名

-E :使用扩展正则表达式

-P :使用Perl正则表达式

-r或--recursive :递归搜索目录中的文件

案例:

显示行号

[root@huang ~]# grep oo  test.txt
good
food 
wood
wooooooood
gooood
#hoo
#boo
#joo
[root@huang ~]# grep oo -n test.txt
3:good
4:food
5:wood
6:wooooooood
7:gooood
21:#hoo
22:#boo
23:#joo

忽略大小写

[root@huang ~]# grep oo -n test.txt
3:good
4:food
5:wood
6:wooooooood
7:gooood
21:#hoo
22:#boo
23:#joo
[root@huang ~]# grep oo -n -i test.txt
3:good
4:food
5:wood
6:wooooooood
7:gooood
21:#hoo
22:#boo
23:#joo
24:GOOD
25:FOOD

反转匹配(除了指定的不出现)

[root@huang ~]# grep -n -i root passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@huang ~]# grep -n -i -v root passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin

根据关键字查找目录下文件内容并返回文件名称

查看/var/log/目录下包含"error"的日志文件并返回文件名
root@huang ~]# cd /var/log
[root@huang log]# grep -rl "error" ./
./gdm/:0.log.4
./gdm/:0.log.3
./gdm/:0.log.2
./gdm/:0.log.1
./gdm/:0.log
./messages
./secure
./anaconda/syslog
./anaconda/X.log
./anaconda/program.log
./anaconda/packaging.log
./anaconda/journal.log
./Xorg.9.log
./Xorg.0.log.old
./Xorg.0.log
./vmware-network.7.log

根据通配符查找

^:以什么什么开头

$:以什么什么结尾

.:表示单个字符

查找以f开头d结尾,中间有两个字符的字符串
[root@huang ~]# cat test.txt | grep "f..d"
122121321food1123213
ferd
[root@huang ~]# cat test.txt | grep "^f..d$"
ferd

2、find查找

find 递归地在层次目录中处理文件

选项: -name :根据关键字查找,支持通配符*

-type :根据文件类型查找

-size :根据文件大小查找

-maxdepth :指定查找时地路径深度

-exec :将find命令查找到的内容交给-exec后面的命令再次处理

-user :根据文件的属主进行查找

-perm :根据文件的权限进行查找

案例:

根据关键字查找

[root@huang ~]# find /var/log/ -name "*.log"
/var/log/audit/audit.log
/var/log/gdm/:0.log
/var/log/gdm/:0-greeter.log
/var/log/tuned/tuned.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
/var/log/anaconda/ks-script-GsoD6J.log
/var/log/anaconda/ks-script-l3o3Wu.log
/var/log/anaconda/journal.log
/var/log/boot.log
/var/log/vmware-vmtoolsd-root.log
/var/log/vmware-vmsvc-root.log
/var/log/Xorg.9.log
/var/log/wpa_supplicant.log
/var/log/vmware-vmusr-root.log
[root@huang ~]# find /var/log/ -maxdepth 1 -name "*.log"
/var/log/boot.log
/var/log/vmware-vmtoolsd-root.log
/var/log/vmware-vmsvc-root.log
/var/log/Xorg.9.log
/var/log/wpa_supplicant.log
/var/log/vmware-vmusr-root.log
/var/log/yum.log
/var/log/vmware-network.6.log
/var/log/Xorg.0.log
/var/log/vmware-network.7.log

根据文件类型查找

文件类型

1、普通文件:f-

2、目录:d

3、连接文件:l

4、字符设备文件:c

5、块设备文件:b

查找所有的普通文件
[root@c2407 log]# find ./ -type f
查找目录文件
[root@c2407 log]# find ./ -type d
查找连接文件
[root@c2407 /]# find ./ -type l
查找字符设备文件
[root@c2407 dev]# find ./ -type c
查找块设备文件
[root@c2407 dev]# find ./ -type b
[root@huang ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.cache/dconf/user
./.cache/imsettings/log.bak
./.cache/imsettings/log
./.cache/gnome-shell/update-check-3.28
./.cache/abrt/applet_dirlist
./.cache/abrt/lastnotification
./.cache/event-sound-cache.tdb.b58e2457a2bd406c99ed4bb5e5cf1a5e.x86_64-redhat-linux-gnu
./.cache/tracker/db-version.txt
./.cache/tracker/meta.db
./.cache/tracker/db-locale.txt
./.cache/tracker/meta.db-wal
./.cache/tracker/meta.db-shm
./.cache/tracker/ontologies.gvdb
./.cache/tracker/parser-sha1.txt
./.cache/tracker/locale-for-miner-user-guides.txt
./.cache/tracker/locale-for-miner-apps.txt
./.cache/tracker/last-crawl.txt
./.cache/tracker/first-index.txt
[root@huang ~]# find ./ -type l
./02.txt
./dir3/dir1
./03.txt

根据文件大小查找

查找大于指定容量
[root@huang ~]# find ./ -size +100M
查找小于指定容量
[root@huang ~]# find ./ -size -100M
查找等于指定容量
[root@huang ~]# find ./ -size 100M

多选项查找

查找文件类型为普通文件且文件后缀为.conf并且容量大于100M
[root@huang ~]# find ./ -name "*.conf" -type f -size +100M
./file2.conf

指定路径深度进行查找

root@huang ~]# find /var/log/ -maxdepth 1 -name "*.log"
/var/log/boot.log
/var/log/vmware-vmtoolsd-root.log
/var/log/vmware-vmsvc-root.log
/var/log/Xorg.9.log
/var/log/wpa_supplicant.log
/var/log/vmware-vmusr-root.log
/var/log/yum.log
/var/log/vmware-network.6.log
/var/log/Xorg.0.log

find结合xargs命令进行结果的再处理

[root@huang ~]# find /var/log -name "*.log" | xargs dirname | sort | uniq -c
     18 /var/log
      9 /var/log/anaconda
      1 /var/log/audit
      2 /var/log/gdm
      1 /var/log/tuned

3、sort排序

以行对文件进行排序

-n :按照数值进行排序

-r :反向排序

-k :指定排序字段 |

-o<输出文件> :将排序后的结果转存至指定文件

说明:

当使用sort -n对包含英文字母的文本进行排序时,它会尝试将英文字母按照其在字符编码中的顺序进行数值化解释并排序。

[root@huang ~]# ls -lh | sort -k 5 -n
总用量 5.7G
-rw-r--r--. 1 root  root   2.3K 5月  13 15:57 passwd
drwxr-xr-x. 2 root  root      6 5月   7 23:24 公共
drwxr-xr-x. 2 root  root      6 5月   7 23:24 模板
drwxr-xr-x. 2 root  root      6 5月   7 23:24 视频
drwxr-xr-x. 2 root  root      6 5月   7 23:24 图片
drwxr-xr-x. 2 root  root      6 5月   7 23:24 文档
drwxr-xr-x. 2 root  root      6 5月   7 23:24 下载
drwxr-xr-x. 2 root  root      6 5月   7 23:24 音乐1
drwxr-xr-x. 2 root  root      6 5月   7 23:24 桌面
-rw-r--r--. 1 root  root     54 5月  12 18:34 error.txt
drwx------. 3 huang huang    78 5月   7 23:10 wang
-rw-r--r--. 1 root  root   100M 5月  13 18:50 file1
-rw-r--r--. 1 root  root   100M 5月  13 19:10 file1.conf
-rw-r--r--. 1 root  root   150M 5月  13 19:13 file2.conf
-rw-r--r--. 1 root  root   200M 5月  13 18:50 file2、
​
​
​
-rw-r--r--. 1 root  root   300M 5月  13 18:50 file3
-rw-r--r--. 1 root  root   400M 5月  13 18:50 file4
-rw-r--r--. 1 root  root   500M 5月  13 18:50 file5
-rw-r--r--. 1 root  root   600M 5月  13 18:50 file6
-rw-r--r--. 1 root  root   700M 5月  13 18:50 file7
-rw-r--r--. 1 root  root   800M 5月  13 18:50 file8
-rw-r--r--. 1 root  root   900M 5月  13 18:50 file9
-rw-r--r--. 1 root  root  1000M 5月  13 18:50 file10
​

4、uniq去重

uniq是Unix和类Unix系统中命令,用于从排序的文本数据中去除重复行,仅保留唯一的行。它通常与sort命令结合使用,因为uniq只能删除相邻的重复行。

语法:

uniq [options] [input_film [output_file]]

选项:

-c :进行计数,并删除文件中重复出现的行

-d : 仅显示连续的重复行

-u :仅显示出现一次的行

-i :忽略大小写

[root@huang ~]# cat dir1 | uniq
asdfg
aweqwrqtresdfg
asdfg
aweqwrqtresdfg
[root@huang ~]# cat dir1 | sort -n | uniq
asdfg
aweqwrqtresdfg
[root@huang ~]# cat dir1 | sort -n | uniq -c
      8 asdfg
     16 aweqwrqtresdfg

案例:

删除输入文件中的重复行:

sort input.txt | uniq

仅显示重复的行:

sort input.txt | uniq -d

忽略大小写进行比较:

sort -i input.txt | uniq -i

5、tr转换

tr命令是Unix和类Unix系统中的一个用于字符转换或删除的实用程序。tr命令通常用于处理文本数据,例如删除特定字符、替换字符、转换大小写等操作

语法:

tr [options] set1 set2

选项: -d :删除所有属于字符集1(-d后面的参数)的字符

-s :将重复出现的字符串压缩为一个字符串;用字符集2替换字符集1

案例

1、将文本中的大写字母转换为小写字母:

[root@huang ~]# echo "HELLO" | tr "A-Z" "a-z"
hello

2、删除文本中的空格

[root@huang ~]# echo "hello world" | tr -d " "
helloworld

3、将文本中的特定字符替换为另一个字符

[root@huang ~]# echo "abcdefght" | tr "a-f" "C-Z"
CDEFGHght

4、压缩重复字符

[root@huang ~]# echo "ersoo ef    jsg" | tr -s " "
ersoo ef jsg

5、去掉空白符

[root@huang ~]# echo "esds   jaksjg  aksdh" | tr -d " "
esdsjaksjgaksdh

6、cut 切割

cut 是Unix和Unix系统中命令,用于根据指定的字符分隔符从输出中提取字段。cut命令用于处理文文件,通常与管道符(|)和其他命令一起使用,以进一步处理和分析文本数据

语法:

cut [options] [field -spec]

选项

-f :通过指定哪一个字段进行提取。cut命令使用"TAB"作为默认的字段分隔符

-d :"TAB"是默认的分割符,使用此选项可更改为其他的分隔符

案例:

1、提取指定范围内的字段:(连续)

[root@huang ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@huang ~]# cat /etc/passwd | cut -d: -f3-5
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
5:0:sync
6:0:shutdown
7:0:halt
8:12:mail

2、使用通配符提取多个字段:(可以不连续)

[root@huang ~]# cat /etc/passwd | cut -d : -f3,5
0:root
1:bin
2:daemon
3:adm
4:lp
5:sync
6:shutdown
7:halt
8:mail
11:operator

7、which 命令

which命令在Unix和类Unix系统中用于查找一个命令的完整路径。当您输入一个命令名称时。which命令会告诉您该命令位于哪个文件系统中,which命令通常用于确认命令是否存在于系统PATH中,或者用于在脚本中获取命令的确切路径。如果您想要查找不存在于PATH中,which命令将不会返回任何输出。

语法:

which [command]
[root@huang ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

8、whereis命令

whereis命令在Unix和类Unix系统中用于查找二进制文件、源代码文件和手册页的位置。它搜索默认的文件系统路径来确定指定命令的相关文件。

语法:

whereis []
[root@huang ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

9、diff命令

语法:

diff [选项] 文件1 文件2
[root@huang ~]# diff passwd passwd1
5a6,7
> aaaaaaaaaaaaaas
> saafffffffffffffff
12a15
> asssssssssss

二、文件与目录归档压缩命令

1、tar

归档命令

语法:

tar [选项] [归档文件名称] [-C] [解压路径]

选项

-z,--gzip :用gzip对存档压缩

-c,--create :建立新的存档

-v,--verbose :详细显示处理的文件

-f,--file :指定存档文件路径及文件

-j,--bzip2 :通过bzip2过滤归档

-x,--extract,--get :从归档中解出文件

-C :指定解压后的存储路径

tvf :仅查看归档包中的文件内容

案例:

压缩文件:

[root@huang ~]# tar zcvf test.tar.gz passwd/ passwd1
passwd
passwd1
[root@huang ~]# tar jcvf test.tar.bz2 passwd/ passwd1
passwd
passwd1
[root@huang ~]# ls
passwd  passwd1  test.tar.bz2  test.tar.gz  公共  模板  视频  图片  文档  下载  音乐  桌面

解压文件:

解压到当前目录

[root@huang ~]# ls
test.tar.bz2  test.tar.gz  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@huang ~]# tar xf test.var.gz
[root@huang ~]# ls
passwd  passwd1  test.tar.bz2  test.var.gz  公共  模板  视频  图片  文档  下载 

解压到指定目录

[root@huang ~]# tar xf test.var.bz2 -C /opt/

查看将压缩包的内容

[root@huang ~]# tar tvf test.tar.bz2
-rw-r--r-- root/root      2262 2025-05-14 16:18 passwd
-rw-r--r-- root/root      2262 2025-05-14 16:19 passwd1

2、zip/unzip

语法:

zip 压缩后的文件名 需要压缩的文件

解压缩命令:

unzip 压缩文件名

3、gzip/gunzip

默认压缩后源文件消失

语法:

gzip 需要压缩的文件
gzip -k filename        (保留文件)

解压缩命令

默认解压后源文件消失

gunzip 压缩文件
gunzip -k 压缩文件

4、bzip2 / bunzip2

压缩后源文件消失,压缩率最高

语法:

bzip2 需要压缩的文件

解压缩命令

解压后源文件消失

bunzip2 压缩文件

案例

压缩:

[root@huang ~]# ls
passwd  passwd2  passwd3  公共  模板  视频  图片  文档  下载  音乐  桌面
​
[root@huang ~]# zip passwd.zip passwd
  adding: passwd (deflated 61%)
​
[root@huang ~]# gzip passwd2
​
[root@huang ~]# bzip2 passwd3
​
[root@huang ~]# ls -lh
总用量 16K
-rw-r--r--. 1 root root 2.3K 5月  14 18:21 passwd
-rw-r--r--. 1 root root  917 5月  14 18:21 passwd2.gz
-rw-r--r--. 1 root root  934 5月  14 18:21 passwd3.bz2
-rw-r--r--. 1 root root 1.1K 5月  14 18:22 passwd.zip
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面

解压:

[root@huang ~]# rm -rf passwd
​
[root@huang ~]# unzip passwd.zip
Archive:  passwd.zip
  inflating: passwd                  
​
[root@huang ~]# gunzip passwd2.gz
​
[root@huang ~]# bzip2 -d passwd3.bz2
​
[root@huang ~]# ls -lh
总用量 16K
-rw-r--r--. 1 root root 2.3K 5月  14 18:21 passwd
-rw-r--r--. 1 root root 2.3K 5月  14 18:21 passwd2
-rw-r--r--. 1 root root 2.3K 5月  14 18:21 passwd3
-rw-r--r--. 1 root root 1.1K 5月  14 18:22 passwd.zip
drwxr-xr-x. 2 root root    6 5月   7 23:24 公共
drwxr-xr-x. 2 root root    6 5月   7 23:24 模板
drwxr-xr-x. 2 root root    6 5月   7 23:24 视频
drwxr-xr-x. 2 root root    6 5月   7 23:24 图片
drwxr-xr-x. 2 root root    6 5月   7 23:24 文档
drwxr-xr-x. 2 root root    6 5月   7 23:24 下载
drwxr-xr-x. 2 root root    6 5月   7 23:24 音乐
drwxr-xr-x. 2 root root    6 5月   7 23:24 桌面
​

工具对比

工具/格式压缩率速度典型扩展名特点
gzip.gz通用,适合文本文件
bzip2.bz2高压缩率,适合大文件
xz极高最慢.xz最高压缩率,资源消耗大
zip.zip跨平台(Windows兼容)
tar.tar仅归档,需配合压缩工具使用

三、统计命令

1、wc

统计文件内容的行数、字符数、单词数

1、wc -l   行数
2、wc -w   单词
3、wc -c   字符
[root@huang ~]# df -Th | wc -l
10
[root@huang ~]# df -Th | wc -c
667
[root@huang ~]# df -Th | wc -w
70

2、du

统计文件占用磁盘空间的容量

du -sh /path/to/dir        统计目录总大小(-s汇总,-h人性化显示)
du -ah /path              #显示所有文件及子目录大小(含隐藏文件)
[root@huang ~]# du -ah
4.0K    ./.bash_logout
4.0K    ./.bash_profile
4.0K    ./.bashrc
4.0K    ./.cshrc
4.0K    ./.tcshrc
4.0K    ./.cache/dconf/user
4.0K    ./.cache/dconf
4.0K    ./.cache/imsettings/log.bak
4.0K    ./.cache/imsettings/log
8.0K    ./.cache/imsettings
0       ./.cache/libgweather
0       ./.cache/evolution/addressbook/trash
0       ./.cache/evolution/addressbook
0       ./.cache/evolution/calendar/trash
0       ./.cache/evolution/calendar
0       ./.cache/evolution/mail/trash
0       ./.cache/evolution/mail
0       ./.cache/evolution/memos/trash
0       ./.cache/evolution/memos
0       ./.cache/evolution/sources/trash
0       ./.cache/evolution/sources
0       ./.cache/evolution/tasks/trash
0       ./.cache/evolution/tasks
0       ./.cache/evolution
0       ./.cache/gnome-shell/update-check-3.28
0       ./.cache/gnome-shell
0       ./.cache/abrt/applet_dirlist
4.0K    ./.cache/abrt/lastnotification
4.0K    ./.cache/abrt
12K     ./.cache/event-sound-cache.tdb.b58e2457a2bd406c99ed4bb5e5cf1a5e.x86_64-redhat-linux-gnu
4.0K    ./.cache/tracker/db-v
[root@huang ~]# du -sh .local/
400K    .local/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值