文章目录

  • 1.find
  • (1)-name:基于文件名查找
  • (2)-size:基于文件大小
  • (3)-type:基于文件类型
  • (4)-[cma][time|min]基于修改时间
  • (5)-maxdepth:基于文件的层数
  • 2.grep
  • (1)-r:递归子路径;-n:显示行号
  • (2)-A:接着输出后面几行,-B接着输出前面几行
  • (3)与find结合使用
  • 3.more
  • 4.cat
  • 5.tail
  • 6.head
  • 7.df -h:查看磁盘大小
  • 8.du -h(du -h .)当前文件及每个文件的大小
  • 9.思考:查找当前目录下是否有文件类型是txt的,大于1M的文件,按照从大到小的顺序排序,输出前10个文件?
  • 10.linux中怎么查看压缩包中的内容

1.find

(1)-name:基于文件名查找

基于全局/目录(及子目录),当前目录.(及子目录)
(a)find / -name 1.txt

(b)find / -name 1.txt -print0|xargs -0 ls -l
-print0:将三行文件打印成一行,并用NULL分开(0的ASICII码对应的字符/0);
-print0用/0隔开,xargs -0再用/0分开,若不用,默认管道的输出有空格,会出错;
ls -l:列出每个文件的大小;

(c)find / -name "1.txt" -print0|xargs -0 ls -l

(2)-size:基于文件大小

(a)find . -size +10M
当前文件夹,超过10M的文件

(b)find . -size +10M -print0|xargs -0 ls -l
具体大小

(3)-type:基于文件类型

(a)find . -name config -type d
查找文件夹

(b)find . -name config -type f
查找文件

(4)-[cma][time|min]基于修改时间

(a)find . -ctime -1
基于一天内,c:change,m:modify两者在这里的意思差不多

(b)find . -ctime 1
刚好一天前的这一天

(c)find . -ctime +1
一天前的修改

(d)find . -cmin -30
30min内修改的文件

(e)find . -amin -1
1min内读过哪些文件,a是access的意思

(5)-maxdepth:基于文件的层数

(a)find . -maxdepth 3 -name "*.txt"
文件最深的层数是3

2.grep

(1)-r:递归子路径;-n:显示行号

(a)grep "a" 1.txt

(b)grep -n "a" 1.txt
输出目录:-n

(c)grep -n "a" *
当前目录下所有文件

(d)grep -rn "a" *
当前目录下及子目录

(e)grep -n "a" *.txt
当前目录下所有*.txt文件中

(2)-A:接着输出后面几行,-B接着输出前面几行

(a)grep -rn ”aa“ -A 2 1.txt
接着输出后面2行
A:after
(b)grep -rn ”aa“ -B 2 1.txt
接着输出前面两行
B:befor

(3)与find结合使用

(a)find . -maxdepth 3 -name "*.txt" -print0 |xargs -0 grep -n "123"
找到所有txt文件中,含有有123内容的文件

3.more

(a)more -3 1.txt
q离开,b前翻页,空格往后翻页;
每页行数是3行;

(b)more -3 +4 1.txt
从第4行开始读;

(c)找某一时间点的日志,但是文件很大
grep "15:54:01" 1.txt
grep "15:54:01" -A 10 -B 10 1.txt

或者
grep  -n "15:54:01" 1.txt
查看行号
more +8 1.txt
从第8行开始查看

4.cat

(a)cat 1.txt
(b)cat -n 1.txt

5.tail

(a)tail 1.txt

(b)tail -n 2 1.txt

(c)tail -f 1.txt
监控文件的变动:-f

6.head

(a)head 1.txt

(b)head -n 2 1.txt

7.df -h:查看磁盘大小

8.du -h(du -h .)当前文件及每个文件的大小

(a)du -h -d 1
-d指定最大的深度
显示文件夹的大小

9.思考:查找当前目录下是否有文件类型是txt的,大于1M的文件,按照从大到小的顺序排序,输出前10个文件?

先通过 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

10.linux中怎么查看压缩包中的内容

bzcat 档名.bz2
zcat 档名.gz