Linux文件查找与读取的常用指令

主要指令:find grep more cat tail head

1. find

用来查找文件

基于文件名进行查找( -name )

比如,查找1.txt

[root@localhost yangfan]# mkdir demo
[root@localhost yangfan]# cd demo/
[root@localhost demo]# touch 1.txt
[root@localhost demo]# cd ..
[root@localhost yangfan]# pwd
/home/yangfan
[root@localhost yangfan]# find / -name 1.txt
# 这是一个官方的bug,是个空文件夹,不用管
find: ‘/run/user/1000/gvfs’: Permission denied
/home/yangfan/demo/1.txt
[root@localhost yangfan]# find / -name 1.txt -print0|xargs -0 ls -l
find: ‘/run/user/1000/gvfs’: Permission denied
-rw-r--r--. 1 root root 0 Oct 20 06:23 /home/yangfan/demo/1.txt
[root@localhost yangfan]# find . -name *.txt -print0|xargs -0 ls -l

根据文件大小进行查找( -size )

# 比如,查找当前目录下大于5M的文件
[root@localhost yangfan]# find . -size +5M
./.mozilla/firefox/sd4g0v9f.default/places.sqlite
[root@localhost yangfan]# find . -size +5M -print0|xargs -0 ls -l
-rw-r--r--. 1 yangfan yangfan 10485760 Oct 20 06:10 ./.mozilla/firefox/sd4g0v9f.default/places.sqlite

根据类型进行区分查找( -type )

-type f # 表示文件 file
-type d # 表示文件夹 dir

[root@localhost yangfan]# find / -name config
find: ‘/run/user/1000/gvfs’: Permission denied
/sys/devices/pci0000:00/0000:00:00.0/config
/sys/devices/pci0000:00/0000:00:01.0/config
# 中间还有很多
/usr/share/yelp/mathjax/config
/usr/src/kernels/3.10.0-693.el7.x86_64/include/config
/usr/src/kernels/3.10.0-693.el7.x86_64/scripts/config

# 文件和文件夹全都显示出来了
[root@localhost yangfan]# find / -name config -type d
find: ‘/run/user/1000/gvfs’: Permission denied
/sys/kernel/config
/usr/lib/python2.7/site-packages/firewall/config
/usr/lib64/python2.7/config
/usr/share/yelp/mathjax/config
/usr/src/kernels/3.10.0-693.el7.x86_64/include/config

根据修改时间进行区分查找( -ctime -cmin -mtime -mmin -atime -amin)

-ctime :修改时间 /天
-cmin :修改时间 /分钟
-mtime :修改时间 /天,不包括权限
-mmin :修改时间 /分钟不包括权限
-atime :读取时间 /天
-amin :读取时间 /分钟

# 1分钟之内修改过的文件
[root@localhost yangfan]# find . -cmin -1

根据文件夹的深度进行查找( -maxdepth -mindepth )

查找最大深度为3的文件夹下的txt文档

[root@localhost yangfan]# find . -maxdepth 3 -name "*.txt"
./.cache/tracker/db-version.txt
./.cache/tracker/db-locale.txt
./.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
./demo/1.txt

2. grep

在文件中进行查找

[root@localhost demo]# grep "a" 1.txt
[root@localhost demo]# echo "aa" >>1.txt 
[root@localhost demo]# echo "bb" >>1.txt 
[root@localhost demo]# echo "bb" >>1.txt 
[root@localhost demo]# echo "bb" >>1.txt 
[root@localhost demo]# echo "aa" >>1.txt 
[root@localhost demo]# cat 1.txt
aa
bb
bb
bb
aa
[root@localhost demo]# grep "a" 1.txt
aa
aa
# 输出所在行号
[root@localhost demo]# grep  -n "a" 1.txt
1:aa
5:aa

# 输出前后行 -A (after) -B (before)
[root@localhost demo]# grep  -n "a" -A 2 1.txt
1:aa
2-bb
3-bb
--
5:aa

[root@localhost demo]# find . -maxdepth 3 -name "*.txt" -print0|xargs -0 grep -n "aa"
1:aa
5:aa

3. cat

# 查看文件内容
[root@localhost demo]# cat 1.txt 
aa
bb
bb
bb
aa
[root@localhost demo]# cat -n 1.txt 
     1    aa
     2    bb
     3    bb
     4    bb
     5    aa

4. more

当文件行数较多时(比如日志文件),加载全部比较费时间,此时可以使用more进行分页查看,然后使用【回车键】查看下一行,【空格键】查看下一页,【b】返回上一页,【q】退出

[root@localhost demo]# more 2.txt 
# 指定每一页的行数  -3 表示3行
[root@localhost demo]# more -3 2.txt 
aa
bb
cc

# 指定从第四行开始读
[root@localhost demo]# more -3 +4 2.txt 
dd
aa
dd
  1. head tail
    head 头
    tail 尾
# 默认打印前十行
[root@localhost demo]# head 2.txt
aa
bb
cc
dd
aa
dd
ff
vvv
vv
ss
# 指定打印前三行
[root@localhost demo]# head -n 3 2.txt 
aa
bb
cc
# 指定打印最后三行
[root@localhost demo]# tail -n 3 2.txt 
sd
ss

# 持续监控文件的变动,在末尾新增/删除 
[root@localhost demo]# tail -f 2.txt 
aa
dfdsd
dd
bb
cc
kk
hh
sd
ss

假设要取 /demo/2.txt 的第11到15行

[root@localhost demo]# head -n 15 2.txt | tail -n 5
dfd
ee
sdfs
htyj
rtr

[root@localhost demo]# cat -n 2.txt | head -n 15 | tail -n 5
    11    dfd
    12    ee
    13    sdfs
    14    htyj
    15    rtr

df du

df 查看磁盘使用情况,列出文件系统整体磁盘使用情况
du 查看目录下文件大小(常用在查看目录所占磁盘空间)
查看磁盘使用情况

[root@localhost demo]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        58G  3.4G   55G   6% /
devtmpfs        898M     0  898M   0% /dev
tmpfs           912M     0  912M   0% /dev/shm
tmpfs           912M  9.0M  903M   1% /run
tmpfs           912M     0  912M   0% /sys/fs/cgroup
/dev/sda1       297M  157M  141M  53% /boot
tmpfs           183M   28K  183M   1% /run/user/1000
tmpfs           183M     0  183M   0% /run/user/0

# 当后面接了目录时,会自动分析出该目录所在的磁盘分区,并将该分区容量显示出来
[root@localhost demo]# df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        58G  3.4G   55G   6% /

# 查看路径深度不超过1的
[root@localhost yangfan]# du -h -d 1
13M    ./.mozilla
13M    ./.cache
104K    ./.config
0    ./Desktop
0    ./Downloads
0    ./Templates
0    ./Public
0    ./Documents
0    ./Music
0    ./Pictures
0    ./Videos
304K    ./.local
8.0K    ./demo
26M    .

free
可以显示Linux系统中空闲的、已用的物理内存及swap内存,及被内核使用的buffer。

[root@localhost yangfan]# free
              total        used        free      shared  buff/cache   available
Mem:        1867024      820500      271800        9948      774724      784264
Swap:       2098172           0     2098172

练习题目:输出当前路径 和 当前路径的子路径下所有的txt文件,要求大小超过1M,并且按照从大到小的顺序进行排序,输出前十个

先通过 find . -name '*.txt' -size +1M -type f 查看是否有大于1M的txt文件,没有的话就不用继续了
再通过 find . -name '*.txt' -size +1M -type f -print0|xargs -0 du -m|sort -nr|head -10
全部评论

相关推荐

12-24 20:49
武汉大学 Java
点赞 评论 收藏
分享
12-01 18:27
已编辑
湖南科技大学 后端工程师
点赞 评论 收藏
分享
Tom哥981:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务