命令文件: which ls  (echo $PATH)


任意文件:locate

-  (updatedb)


find 

find [options] [path...] [expression]


按文件名:

[root@station230 ~]# find /etc -name "ifcfg-eth0"

[root@station230 ~]# find /etc -iname "ifcfg-eth0"//-i忽略大小写

[root@station230 ~]# find /etc -iname "ifcfg-eth*"


按大小:

[root@station230 ~]# find /etc -size +5M//大于5M

[root@station230 ~]# find /etc -size 5M

[root@station230 ~]# find /etc -size -5M

[root@station230 ~]# find /etc -size +5M -ls//-ls选项


按时间找(atime,mtime,ctime)

[root@station230 ~]# find /etc -mtime +5//修改时间超过5天

[root@station230 ~]# find /etc -mtime 5//修改时间等于5天

[root@station230 ~]# find /etc -mtime -5//修改时间5天以内


按文件属主、属组找

[root@station230 ~]# find /home -user jack//属主是jack的文件

[root@station230 ~]# find /home -group jack//属组是jack组的文件



[root@station230 ~]# find /home -user jack -group alice

[root@station230 ~]# find /home -user jack -a  -group alice

[root@station230 ~]# find /home -user jack -o  -group alice


[root@station230 ~]# find /home -nouser

[root@station230 ~]# find /home -nogroup

[root@station230 ~]# find /home -ls -nouser -o -nogroup 


按文件类型:

[root@station230 ~]# find /dev -type f//f普通

[root@station230 ~]# find /dev -type d//d目录

[root@station230 ~]# find /dev -type l//l链接

[root@station230 ~]# find /dev -type b//b块设备

[root@station230 ~]# find /dev -type c//c字符设备

[root@station230 ~]# find /dev -type s//s套接字

[root@station230 ~]# find /dev -type p//p管道文件


-inum n 

find / -inum 31064284           //根据inode 查找



按权限:

[root@station230 dir8]# find . -perm 644 -ls

[root@station230 dir8]# find . -perm -644 -ls

[root@station230 dir8]# find . -perm -600 -ls

[root@station230 ~]# find /sbin -perm -4000 -ls//包含set uid

[root@station230 ~]# find /sbin -perm -2000 -ls//包含set gid

[root@station230 ~]# find /sbin -perm -1000 -ls//包含sticky


找到后处理的动作:

[root@station230 ~]# find /etc -name "ifcfg-eth0" -exec cp -rf {} /tmp \;

[root@station230 dir8]# find . -name "file*" -exec rm -rvf {} \;


1. 将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变

[root@station230 ~]# find /etc -type d -exec mkdir -p /tmp/{}  \;


2. 将/etc目录复制到/var/tmp/,

   将/var/tmp/etc中的所有目录设置权限777(仅目录)

   将/var/tmp/etc中所有文件权限设置为666

[root@station230 tmp]# find etc -type d -exec chmod 777 {} \;

[root@station230 tmp]# find etc -type f -exec chmod 666 {} \;


[root@station230 tmp]# find etc ! -type d -exec chmod 666 {} \;


3. 创建目录/dir1,在/dir1中创建文件file1 file2 file3

  使用find查找并删除 除了file2以外的所有文件

[root@station230 ~]# mkdir /dir4    




[root@station230 ~]# touch /dir4/file{1..100}


[root@station230 etc]# find . ! -name ntp.conf -exec rm -rf {} \;

[root@station230 dir4]# find . ! -name file50 -a ! -name file70 -exec rm -rf {} \;



===文件打包、压缩

==打包

[root@station230 ~]# tar -cvf etc1.tar /etc//c创建 v详细 f打包后文件名

[root@station230 ~]# tar -cf etc2.tar /etc

[root@station230 ~]# ll -h etc*

-rw-r--r-- 1 root root 104M 10-29 12:10 etc1.tar

-rw-r--r-- 1 root root 104M 10-29 12:11 etc2.tar


==压缩

[root@station230 ~]# gzip etc1.tar

[root@station230 ~]# bzip2 etc2.tar 

[root@station230 ~]# 

[root@station230 ~]# ll -h etc*

-rw-r--r-- 1 root root  12M 10-29 12:10 etc1.tar.gz

-rw-r--r-- 1 root root 8.0M 10-29 12:11 etc2.tar.bz2


==解压

[root@station230 ~]# gzip -d etc1.tar.gz

[root@station230 ~]# bzip2 -d etc2.tar.bz2


==解包

[root@station230 ~]# tar -xvf etc1.tar//解包到当前目录

[root@station230 ~]# tar -xf etc2.tar -C /var/tmp///-C重定向到/var/tmp目录


=====================================================================================

===打包,压缩===

[root@station230 ~]# tar -czf etc1.tar.gz /etc//-z 调用gzip

[root@station230 ~]# tar -cjf etc2.tar.bz2 /etc//-j 调用bzip2


===解包,解压===

[root@station230 ~]# tar -xvf etc1.tar.gz//无需指定解压工具,tar会自动判断gzip或bzip2

[root@station230 ~]# tar -xvf etc2.tar.bz2  -C /tmp


[root@station230 ~]# tar xvf etc1.tar.gz


# unzip 1.zip

# *.rar