如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情
du(disk usage)命令用于估算文件或目录的磁盘使用空间。它会递归地计算目录下所有文件和子目录所占用的磁盘块大小。
find命令用于在指定的目录层次结构中查找符合特定条件的文件和目录。
一、du命令
du(disk usage)命令用于估算文件或目录的磁盘使用空间。它会递归地计算目录下所有文件和子目录所占用的磁盘块大小。通过du命令,用户可以了解磁盘空间的使用情况,从而更好地管理磁盘资源,例如找出占用大量磁盘空间的文件或目录,以便进行清理或迁移操作。
du命令的参数
-h或--human - readable:以人类可读的方式显示磁盘使用量,如将字节数转换为 KB、MB、GB 等更直观的单位。
-s或--summarize:只显示指定目录或文件的总大小,而不显示其下每个子目录和文件的大小。
-a或--all:显示所有文件(包括普通文件和目录)的大小,默认情况下du只显示目录的大小。
-c或--total:在显示每个目录或文件大小的同时,计算并显示总计大小。
du 统计目录的大小
du -sh /etc
inode号码
df-i 查看总inode
df-h 查看block磁盘大小
inode号码或者磁盘空间任意一个满了,都会导致磁盘无法写入
二、find查找文件
find命令是一个功能强大的文件搜索工具,用于在指定的目录层次结构中查找符合特定条件的文件和目录。它可以根据文件名、文件大小、文件类型、文件权限、修改时间等多种属性来进行搜索,帮助用户快速定位所需的文件。
语法结构:
find 在哪找(绝对路径/相对路径) 按什么类型查找
注意:find是递归查找文件
按文件类型查找
f 普通文件 -
d 目录
l 软链接
c 字节设备
b 块设备
语法结构:find ./(路径) -type文件类型
[root@yunzhongziedu ~]# mkdir yunzhongzi
[root@yunzhongziedu ~]# touch yunzhongzi/{1..3}.txt
[root@yunzhongziedu ~]# touch yunzhongzi/{1..3}.log
[root@yunzhongziedu ~]# touch yunzhongzi/{1..3}.TXT
[root@yunzhongziedu ~]# mkdir yunzhongzi/yunzhongzi{1..4}
[root@yunzhongziedu ~]# cd yunzhongzi
[root@yunzhongziedu yunzhongzi]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 13 15:35 1.log
-rw-r--r-- 1 root root 0 Mar 13 15:35 1.txt
-rw-r--r-- 1 root root 0 Mar 13 15:35 1.TXT
-rw-r--r-- 1 root root 0 Mar 13 15:35 2.log
-rw-r--r-- 1 root root 0 Mar 13 15:35 2.txt
-rw-r--r-- 1 root root 0 Mar 13 15:35 2.TXT
-rw-r--r-- 1 root root 0 Mar 13 15:35 3.log
-rw-r--r-- 1 root root 0 Mar 13 15:35 3.txt
-rw-r--r-- 1 root root 0 Mar 13 15:35 3.TXT
drwxr-xr-x 2 root root 6 Mar 13 15:36 yunzhongzi1
drwxr-xr-x 2 root root 6 Mar 13 15:36 yunzhongzi2
drwxr-xr-x 2 root root 6 Mar 13 15:36 yunzhongzi3
drwxr-xr-x 2 root root 6 Mar 13 15:36 yunzhongzi4
1.按照深度等级查找文件
#案例 :只查找一级目录以下的文件
[root@yunzhongziedu yunzhongzi]# find ./ -maxdepth 1 -type f
#案例1.查找当前目录下所有的普通文件
[root@yunzhongziedu yunzhongzi]# find ./ -type f
#案例2.查找当前目录下所有的目录
[root@yunzhongziedu yunzhongzi]# find ./ -type d
#案例3.按照文件名称查找
语法 :find ./ -name "文件名称/目录名称"
[root@yunzhongziedu yunzhongzi]# find ./ -name "1.txt"
./1.txt
#案例4.按照文件的名称模糊擦查找
[root@yunzhongziedu yunzhongzi]# find ./ -name "*.txt"
./1.txt
./2.txt
./3.txt
#案例5.按照文件名称查找(忽略大小写)
[root@yunzhongziedu yunzhongzi]# find ./ -iname "*.txt"
#案例6.按照目录的名查找
[root@yunzhongziedu yunzhongzi]# find ./ -name "yunzhongzi1"
./yunzhongzi1
#案例7.按照文件的inode号码查找
[root@yunzhongziedu yunzhongzi]# ll -i
[root@yunzhongziedu yunzhongzi]# find ./ -inum 33577780
./2.TXT
#案例8.按照类型和名称联合查找
[root@yunzhongziedu yunzhongzi]# find ./ -type f -name "1.txt"
./1.txt
#案例9.按照大小查找
语法: find ./ -size 10M #查找10M的文件
+10M #查找大于10M的文件
-10M #查找小于10M的文件
搭建环境:
[root@yunzhongziedu yunzhongzi]# cat /etc/services /etc/services /etc/services > s.txt
[root@yunzhongziedu yunzhongzi]# cat s.txt s.txt s.txt s.txt s.txt s.txt s.txt s.txt > all.txt
[root@yunzhongziedu yunzhongzi]# dd if=/dev/zero of=test.txt bs=1M count=10
[root@yunzhongziedu yunzhongzi]# ll -h all.txt
-rw-r--r-- 1 root root 16M Mar 13 15:52 all.txt
#案例1).查找等于10M的文件
[root@yunzhongziedu yunzhongzi]# find ./ -size 10M
./test.txt
#案例2).查找大于10M的文件
[root@yunzhongziedu yunzhongzi]# find ./ -size +10M
./all.txt
#案例3查找小于10M的文件
[root@yunzhongziedu yunzhongzi]# find ./ -size -10M
#案例4.查找文件大于5M且小于15的文件
注意:默认关系之间是并且的关系
并且 and 同时成立
或者:or 成立一个即可
[root@yunzhongziedu yunzhongzi]# find ./ -size +5 -size -15M
./s.txt
./test.txt
#案例5.查找文件小于10M或者大于15M
[root@yunzhongziedu yunzhongzi]# find ./ -size -10M -or -size +15M
#案例6.查找文件名称.txt 并且大于5M的文件
[root@yunzhongziedu yunzhongzi]# find ./ -size +5M -name "*.txt"
./all.txt
./test.txt
#案例7.按时间查找
-mtime #文件的修改时间
语法 :find ./ -mtime +7 #7天前被修改的文件
find ./ -mtime -7 #7天内被修改的文件
案例环境搭建:
[root@yunzhongziedu yunzhongzi]# date -s 20081010
Fri Oct 10 00:00:00 CST 2008
[root@yunzhongziedu yunzhongzi]# touch 2008-{1..5}.txt
[root@yunzhongziedu yunzhongzi]# ntpdate ntp1.aliyun.com
13 Mar 16:41:19 ntpdate[26699]: step time server 120.25.115.20 offset 486751257.530505 sec
#1)
[root@yunzhongziedu yunzhongzi]# find ./ -type f -mtime +7
./2008-1.txt
./2008-2.txt
./2008-3.txt
./2008-4.txt
./2008-5.txt
#2)
[root@yunzhongziedu yunzhongzi]# find ./ -type f -mtime -7
三、find查找到的结果交给其他命令
1、xargs
1.find结果交给rm命令
xargs将find查找到的文件甩到命令的最后
注:xargs后面的别名失效
语法:find ./ -type f |xargs rm
#案例:查找出.TXT的文件后删除
[root@yunzhongziedu yunzhongzi]# find ./ -name "*.TXT"|xargs rm
#案例:删除目录需要-r
[root@yunzhongziedu yunzhongzi]# find ./ -name "yunzhongzi4"|xargs rm -r
#案例2.将find的执行结果交给ls查看详细信息
[root@yunzhongziedu yunzhongzi]# find ./ -name "all.txt"|xargs ls -r
./all.txt
#案例3.将查到的文件交给cat查看
[root@yunzhongziedu yunzhongzi]# echo yunzhongzi > 2008-3.txt
[root@yunzhongziedu yunzhongzi]# echo yunzhongzi > 2008-2.txt
[root@yunzhongziedu yunzhongzi]# find ./ -name "2008-[23].txt"|xargs cat
yunzhongzi
yunzhongzi
#案例4.find结果交给cp
[root@yunzhongziedu yunzhongzi]# find ./ -name all.txt|xargs -i cp {} /opt
[root@yunzhongziedu yunzhongzi]# ll /opt
total 15712
-rw-r--r-- 1 root root 16087032 Mar 13 16:59 all.txt
#案例5.find结果交给mv
[root@yunzhongziedu yunzhongzi]# find ./ -name s.txt|xargs -i mv {} /opt
[root@yunzhongziedu yunzhongzi]# ll /opt
total 17676
-rw-r--r-- 1 root root 16087032 Mar 13 16:59 all.txt
-rw-r--r-- 1 root root 2010879 Mar 13 15:52 s.txt
2、-exec
;是exec的结束标志,默认;表示命令分隔符,有特殊含义,不是一个简单的分号
#案例1.将find结果交给 rm
[root@yunzhongziedu yunzhongzi]# find -name 2008-1.txt -exec rm {} \;
#案例2.将find结果交给ls
[root@yunzhongziedu yunzhongzi]# find -name 2008-1.txt -exec ls -l {} \;
#案例3.将find结果交给从cp mv
[root@yunzhongziedu yunzhongzi]# find -type f -exec cp {} /opt/ \;
3、``和$()
``或者$()先执行再交给其他命令
1.find结果交给rm
[root@yunzhongziedu yunzhongzi]# rm -f `find -name "all.txt"`
2.find结果交给ls
[root@yunzhongziedu yunzhongzi]# ll $(find -name 2.txt)
-rw-r--r-- 1 root root 0 Mar 13 15:35 ./2.txt
3.find结果交给cp
[root@yunzhongziedu yunzhongzi]# cp $(find -name 2.txt) /opt
cp: overwrite ‘/opt/2.txt’? y
四、Linux中特殊符号的作用
&& 作用:前面的命令必须执行成功才能执行后面的命令
|| 作用:前面的命令必须执行失败才能执行后面的命令
; 作用:前面的命令不管是成功还是失败,都能执行后面的命令
&&案例
成功:前边的命令成功,后面才可以执行
[root@yunzhongziedu yunzhongzi]# echo hehe && touch h.txt
hehe
[root@yunzhongziedu yunzhongzi]# ls
h.txt
失败:前边的命令失败,后面不会执行
[root@yunzhongziedu yunzhongzi]# ech hehe && touch 1shfsdfsfd.txt
-bash: ech: command not found
[root@yunzhongziedu yunzhongzi]# ls
1.log 2008-2.txt 2008-4.txt 2.log 3.log h.txt yunzhongzi2
1.txt 2008-3.txt 2008-5.txt 2.txt 3.txt yunzhongzi1 yunzhongzi3
ll案例
前边执行失败,后边的才执行
[root@yunzhongziedu yunzhongzi]# ech hehe || touch 1shfsdfsfd.txt
-bash: ech: command not found
[root@yunzhongziedu yunzhongzi]# ls
1shfsdfsfd.txt
;不管前边是否成功都执行
[root@yunzhongziedu yunzhongzi]# ech hehe ; touc aaaaaaa.txt ; cat 1.txt(1.txt没有内容)
-bash: ech: command not found
-bash: touc: command not found
三条命令全错,但不影响执行
今日小结
find查找文件
1.find ./ -name "" #按名称查找
2.find ./ -iname "" #不区分大小写按姓名查找
3.find ./ -inum "" #按inode号码查找
4.find ./ -tyep f/d/c/b "" #按文件类型查找
5.find ./ -tyep f -name "" #联合查询默认并且同时成立
6.find ./ -maxdepth 1 -type f #按照深度等级查找
7.find ./ -size 10M -10M +10M #按照文件大小查找
8.find ./ -size 10M -size +10M #并且
9.find ./ -size 10M -or -name "1.txt" #查找文件名为1.txt或者10M的文件
10.find ./ -mtime +7 -7 #按照文件修改时间查找
find将结果交给其他命令
方法1
find ./ -name "1.txt" |xargs rm -r #删除目录需要加-r
find ./ -name "1.txt" |xargs ls -l #查看详细信息
find ./ -name "1.txt" |xargs -i cp {} /opt/ #复制
方法2
find ./ -name "1.txt" -exec rm {} \;
find ./ -name "1.txt" -exec ls -l {} \;
find ./ -name "1.txt" -exec cp {} /opt/ \;
方法3
rm -f `find ./ -name 1.txt`
ll `find ./ -name 1.txt`
cp `find ./ -name 1.txt` /opt/
$()同等效果
3个特殊符号
&& 作用:前面的命令必须执行成功才能执行后面的命令
|| 作用:前面的命令必须执行失败才能执行后面的命令
; 作用:前面的命令不管是成功明日记号还是失败,都能执行后面的命令
du -sh 查看目录总大小
明日预告:打包压缩,解压,文件权限,满满的干货,敬请期待!!!
想成为大佬,就要从小白开始,从0开始,一点一点的积累,慢慢成长,明天你就是大佬!!