执行find命令搜索文件

[root@desktop3 etc]# find /etc/cron.d* -type f
/etc/cron.d/0hourly
/etc/cron.d/raid-check
/etc/cron.d/sysstat
/etc/cron.daily/tmpwatch
/etc/cron.daily/mlocate.cron
/etc/cron.daily/0logwatch
/etc/cron.daily/prelink
/etc/cron.daily/logrotate
/etc/cron.daily/cups
/etc/cron.daily/readahead.cron
/etc/cron.daily/makewhatis.cron
/etc/cron.daily/rhsmd
/etc/cron.deny


如果我们只想要文件名怎么办

方法1、用awk分割

[root@desktop3 etc]# find /etc/cron.d* -type f | awk -F"/" '{ print $NF }'
0hourly
raid-check
sysstat
tmpwatch
mlocate.cron
0logwatch
prelink
logrotate
cups
readahead.cron
makewhatis.cron
rhsmd
cron.deny



命令有点长,换个简单点的

方法2、用find的printf输出

[root@desktop3 etc]# find /etc/cron.d* -type f -printf "%f\n"
0hourly
raid-check
sysstat
tmpwatch
mlocate.cron
0logwatch
prelink
logrotate
cups
readahead.cron
makewhatis.cron
rhsmd
cron.deny


-printf还有很多参数,比如

-printf "%u %g\n" 输出所属用户和所属组

-printf "%s\t%f\n" 输出文件大小和文件名,以制表符分割

其他的还有类似inode号、更改时间、UID、链接数之类的,想用的时候man一下吧


如果需要输出的信息比较多,可以用-exec来执行其他命令控制输出,以下就是一个典型的例子

[root@desktop3 Desktop]# find /etc/cron.d* -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 113 Mar  4  2011 /etc/cron.d/0hourly
-rw-r--r--. 1 root root 108 Dec  7  2012 /etc/cron.d/raid-check
-rw-r--r--. 1 root root 235 Mar 28  2012 /etc/cron.d/sysstat
-rwxr-xr-x. 1 root root 365 Oct 16  2009 /etc/cron.daily/tmpwatch
-rwxr-xr-x. 1 root root 174 Sep 24  2012 /etc/cron.daily/mlocate.cron
-rwxr-xr-x. 1 root root 265 Feb 28  2011 /etc/cron.daily/0logwatch
-rwxr-xr-x. 1 root root 2126 Apr 23  2010 /etc/cron.daily/prelink
-rwxr-xr-x. 1 root root 196 Aug  6  2012 /etc/cron.daily/logrotate
-rwxr-xr-x. 1 root root 118 Nov  6  2012 /etc/cron.daily/cups
-rwxr-xr-x. 1 root root 563 Mar 25  2010 /etc/cron.daily/readahead.cron
-rwxr-xr-x. 1 root root 905 Nov 17  2012 /etc/cron.daily/makewhatis.cron
-rwxr-xr-x. 1 root root 256 Jan 21  2013 /etc/cron.daily/rhsmd
-rw-r--r--. 1 root root 0 Mar  4  2011 /etc/cron.deny