03-目录管理和软连接-find-xargs
[root@localhost data]# rm -rf /data /* #有空格删根
1,find | xargs
文件相关信息:metadata, data
每个文件有三个时间戳: access time 访问时间,atime,读取文件内容 modify time 修改时间,mtime,改变文件内容(数据) change time 改变时间,ctime,元数据发生改变 xargs是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。 由于很多命令不支持管道| 来传递参数,xargs用于产生某个命令的参数,xargs可以读入stdin的数据,并以空格符或回车符将stdin的数据分隔成为参数 许多命令不能接受过多参数,命令执行可能会失败,xargs可以解决 注意: 文件名或者其他意义的名词内含有空格符的情况 find / -name "*.txt" -delete root@ubuntu2004:~/data# find / -name “*.conf” -ok ls -l {} \; #注意{} 和\;之间有空格 find / -name “a.conf” -ok mv {} /data \; #每次询问 find -name "*.log" -exec mv {} /data \; #批量执行不再询问 find / -name "*.log" -mmin -10 -exec mv {} /opt \; #小于10分钟批量执行不再询问 find / -name "*.log" -mmin +10 -exec mv {} /data_bak \; #大于10分钟批量执行不再询问 echo user{1..10} |xargs -n1 useradd #一次创建10个user find / -name "*.log" |xargs mv -t /gg #查找*.log并移动到/gg目录下,mv -t 将源和目标转换顺序-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 更像一个筛选器,将符合管道传递过来文件名的内容一并处理掉,这是一个极度高效的方法
2, copy scp -a 归档,相当于-dR --preserv=all,常用于备份功能 -r, -R 递归复制目录及内部的所有内容 3,文件元数据和节点表结构 每个文件的属性信息,比如:文件的大小,时间,类型等,称为文件的元数据(meta data)。这此元数据是存放在inode(index node)表中。inode 表中有很多条记录组成,第一条记录对应的存放了一个文件的元数据信息 每一个inode表记录对应的保存了以下信息: inode number 节点号 文件类型 权限 UID GID 链接数(指向这个文件名路径名称个数) 该文件的大小和不同的时间戳 指向磁盘上文件的数据块指针 有关文件的其他数据 mkdir /boot/test echo f{1..500000} | xargs touch #创建多文件,涉及管道 echo f{1..500000} | xargs rm -ftouch 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 硬链接
硬链接本质上就给一个文件起一个新的名称,实质是同一个文件 硬链接特性: 创建硬链接会在对应的目录中增加额外的记录项以引用文件 对应于同一文件系统上一个物理文件 每个目录引用相同的inode号 创建时链接数递增 删除文件时:rm命令递减计数的链接,文件要存在,至少有一个链接数,当链接数为零时,该文件被删除 不能跨越驱动器或分区 不支持对目录创建硬链接[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 软连接
一个符号链接指向另一个文件,就像 windows 中快捷方式,软链接文件和原文件本质上不是同一个文件 软链接特点 一个符号链接的内容是它引用文件的名称 可以对目录创建软链接 可以跨分区的文件实现 指向的是另一个文件的路径;其大小为指向的路径字符串的长度;不增加或减少目标文件inode的引用计数 在创建软链接时, 如果源文件使用相对路径,是相对于软链接文件的路径,而非相对于当前工作目录,但是软链接的路径如果是相对路径,则是相对于当前工作目录格式
ln -s filename [linkname][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