03-目录管理和软连接-find-xargs
[root@localhost data]# rm -rf /data /* #有空格删根
1,find | xargs
文件相关信息:metadata, data
-name filename 查找名为filename的文件 -perm 按执行权限来查找 -user username 按文件属主来查找 -group groupname 按组来查找 -mtime -n +n 按文件更改时间来查找文件,-n指n天以内,+n指n天以前 -atime -n +n 按文件访问时间来查GIN: 0px"> -ctime -n +n 按文件创建时间来查找文件,-n指n天以内,+n指n天以前 -nogroup 查无有效属组的文件,即文件的属组在/etc/groups中不存在 -nouser 查无有效属主的文件,即文件的属主在/etc/passwd中不存 -newer f1 !f2 找文件,-n指n天以内,+n指n天以前 -ctime -n +n 按文件创建时间来查找文件,-n指n天以内,+n指n天以前 -nogroup 查无有效属组的文件,即文件的属组在/etc/groups中不存在 -nouser 查无有效属主的文件,即文件的属主在/etc/passwd中不存 -newer f1 !f2 查更改时间比f1新但比f2旧的文件 -type b/d/c/p/l/f 查是块设备、目录、字符设备、管道、符号链接、普通文件 -size n[c] 查长度为n块[或n字节]的文件 -depth 使查找在进入子目录前先行查找完本目录 -fstype 查更改时间比f1新但比f2旧的文件 -type b/d/c/p/l/f 查是块设备、目录、字符设备、管道、符号链接、普通文件 -size n[c] 查长度为n块[或n字节]的文件 -depth 使查找在进入子目录前先行查找完本目录 -fstype 查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到 -mount 查文件时不跨越文件系统mount点 -follow 如果遇到符号链接文件,就跟踪链接所指的文件 -cpio %; 查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到 -mount 查文件时不跨越文件系统mount点 -follow 如果遇到符号链接文件,就跟踪链接所指的文件 -cpio 对匹配的文件使用cpio命令,将他们备份到磁带设备中 -prune 忽略某个目录
find -name april* 在当前目录下查找以april开始的文件 find -name april* fprint file 在当前目录下查找以april开始的文件,并把结果输出到file中 find -name ap* -o -name may* 查找以ap或may开头的文件 find /mnt -name tom.txt -ftype vfat 在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件 find /mnt -name t.txt ! -ftype vfat 在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件 find /tmp -name wa* -type l 在/tmp下查找名为wa开头且类型为符号链接的文件 find / -mtime -2 在/下查最近两天内改动过的文件 find / -atime -1 查1天之内被存取过的文件 find / -mmin +60 在下查60分钟前改动过的文件 find / -amin +30 查最近30分钟前被存取过的文件 find / -newer tmp.txt 在/下查更新时间比tmp.txt近的文件或目录 find / -anewer tmp.txt 在/下查存取时间比tmp.txt近的文件或目录 find / -used -2 列出文件或目录被改动过之后,在2日内被存取过的文件或目录 find / -user cnscn 列出/目录内属于用户cnscn的文件或目录 find / -uid +501 列出/目录内用户的识别码大于501的文件或目录 find / -group cnscn 列出/内组为cnscn的文件或目录 find / -gid 501 列出内组id为501的文件或目录 find / -nouser 列出/内不属于本地用户的文件或目录 find / -nogroup 列出/内不属于本地组的文件或目录 find / -name tmp.txt -maxdepth 4 列出/内的tmp.txt 查时深度最多为3层 find / -name tmp.txt -mindepth 3 从第2层开始查 find / -empty 查找大小为0的文件或空目录 find / -size +512k 查大于512k的文件 find / -size -512k 查小于512k的文件 find / -links +2 查硬连接数大于2的文件或目录 find / -perm 0700 查权限为700的文件或目录 find /tmp -name tmp.txt -exec cat {} \; find /tmp -name tmp.txt -ok rm {} \; find / -amin -10 # 查找在系统中最后10分钟访问的文件 find / -atime -2 # 查找在系统中最后48小时访问的文件 find / -empty # 查找在系统中为空的文件或者文件夹 find / -group cat # 查找在系统中属于 groupcat的文件 find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件 find / -mtime -1 #查找在系统中最后24小时里修改过的文件 find / -nouser #查找在系统中属于作废用户的文件 find / -user fred #查找在系统中属于FRED这个用户的文件
[root@localhost ~]# stat /etc/passwd File: /etc/passwd Size: 2548 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 134840055 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2022-03-25 20:10:21.501147392 +0800 Modify: 2022-03-25 20:10:21.470146822 +0800 Change: 2022-03-25 20:10:21.471146841 +0800 Birth: 2022-03-25 20:10:21.470146822 +0800
find /data/ –name "*.jpg" -mtime +365 | xargs gzip find /data/ –name "*.jpg" -mtime +365 | xargs ls find /data/ –name "*.jpg" -mtime +365 | xargs rm -f find /data/* –name "*.txt" -mtime +30 | xargs gzip #文件经过gzip压缩后,文件会多出一个 .gz后缀。gzip命令对文本文件有60%~70%的压缩率 find /data/* –name "*.txt" -mtime +30 | xargs ls find /data/* –name "*.txt" -mtime +30 | xargs rm -f
find /var/log/*.log |xargs gzip
find /var/log/*.log.gz |xargs gunzip
管道符 前面的输出 是后面的输入
xargs 更像一个筛选器,将符合管道传递过来文件名的内容一并处理掉,这是一个极度高效的方法
touch file1{1..100900} touch file2{1..100900} touch file3{1..100900} touch file4{1..100900} touch file5{1..100900}
[root@localhost test]# df -i /boot/ Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 524384 524384 0 100% /boot
[root@localhost test]# touch new1.txt touch: cannot touch 'new1.txt': No space left on device
清理大文件
[root@localhost test]# cp /dev/zero /boot/test/zero.bak
cp: error writing '/boot/test/zero.bak': No space left on device
vi zero.bak
错误方法
rf -f zer.bak 要等到文件不再被进程使用才释放,生产慎用,要重启服务才释放空间,或等到业务不繁忙执行
lsof |grep delete
vi 3539 root 5r REG 8,1 793378816 34668 /boot/test/zero.bak (deleted)
[root@localhost ~]# ps -ef|grep 3539 root 3539 2739 98 00:09 pts/1 00:02:47 vi zero.bak root 3593 3501 0 00:12 pts/3 00:00:00 grep --color=auto 3539 [root@localhost ~]# kill -9 3539
正确方法
[root@localhost test]# cat /dev/null > zero.bak
rm -f
4,硬链接和软连接
4.1 硬链接
[root@localhost data]# ln f1.txt f1.txt.link
[root@localhost data]# ll -i f1.txt
134840056 -rw-r--r-- 2 root root 697 Mar 26 18:15 f1.txt
[root@localhost data]# ll -i f1.txt.link
134840056 -rw-r--r-- 2 root root 697 Mar 26 18:15 f1.txt.link
挂盘
[root@localhost opt]# mount /dev/sdb1 /opt
不可跨分区创建硬链接
4.2 软连接
格式

[root@localhost opt]# ln -s /data/aa /opt/aaaa
[root@localhost opt]# rm -f aaaa #删除软连接
带-r / 误操作删除数据
[root@localhost opt]# ln -s /data/ cc #链接到文件夹
[root@localhost opt]# rm -rf cc/ #慎用-r / [root@localhost opt]# ll total 0 lrwxrwxrwx 1 root root 6 Mar 26 18:50 cc -> /data/ [root@localhost data]# ll total 0
[root@localhost opt]# rm -f cc/ #不加r不能删除目录 rm: cannot remove 'cc/': Is a directory [root@localhost opt]# rm -f cc #删除软链接
相对路径针对软连接
[root@localhost data]# ln -s appv1 /opt/app [root@localhost data]# ll drwxr-xr-x 2 root root 6 Mar 26 19:02 appv1 drwxr-xr-x 2 root root 6 Mar 26 19:02 appv2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具