|
uiopuiop
中级用户
  
积分 400
发帖 211
注册 2007-9-30
状态 离线
|
『楼 主』:
如何列出指定深度的目录树?
使用 LLM 解释/回答一下
如何列出某个文件夹内指定深度的目录树?最好还能显示文件夹大小(MB)。
How to list the directory tree of a specified depth within a certain folder? It would be better if it can also display the folder size in MB.
|
|
2008-10-13 17:36 |
|
|
tireless
银牌会员
    
积分 2025
发帖 1122
注册 2007-9-5
状态 离线
|
|
2008-10-13 19:31 |
|
|
uiopuiop
中级用户
  
积分 400
发帖 211
注册 2007-9-30
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
这个工具我也用的,但不能导结果来。
I also use this tool, but I can't export the results.
|
|
2008-10-13 19:41 |
|
|
23112656
中级用户
  
积分 220
发帖 99
注册 2008-6-26
状态 离线
|
|
2008-10-14 01:20 |
|
|
uiopuiop
中级用户
  
积分 400
发帖 211
注册 2007-9-30
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
TREE 没办法指定目录深度
TREE can't specify the directory depth
|
|
2008-10-14 21:56 |
|
|
uiopuiop
中级用户
  
积分 400
发帖 211
注册 2007-9-30
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
是否可以调用 TREE /A, 然后计算有多少个“|” 符号,比如第三层有3 个“|”
,希望有人能指点一下。
+---Powerword 2007
| +---dicts
| +---flw
| +---Fonts
| +---Home
| +---images
| +---Infomation
| +---Language
| +---newword
| | +---Images
| | \---Themes
| | \---DefaultTheme
| +---plugin
| +---scrollword
| | +---Skins
| | | +---Blue
| | | +---Default
| | | +---Green
| | | +---LightBlue
| | | \---Silver
| | \---WordList
| +---Sentence
| +---style
| +---sunti
| +---themes
| | +---ClassicBlue
| | \---ElegantWhite
| \---TTS
Can I call TREE /A and then count how many "|" symbols there are? For example, there are 3 "|" symbols in the third level. Hope someone can give me some pointers.
+---Powerword 2007
| +---dicts
| +---flw
| +---Fonts
| +---Home
| +---images
| +---Infomation
| +---Language
| +---newword
| | +---Images
| | \---Themes
| | \---DefaultTheme
| +---plugin
| +---scrollword
| | +---Skins
| | | +---Blue
| | | +---Default
| | | +---Green
| | | +---LightBlue
| | | \---Silver
| | \---WordList
| +---Sentence
| +---style
| +---sunti
| +---themes
| | +---ClassicBlue
| | \---ElegantWhite
| \---TTS
|
|
2008-10-15 20:18 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
findstr
|

S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore |
|
2008-10-15 20:21 |
|
|
uiopuiop
中级用户
  
积分 400
发帖 211
注册 2007-9-30
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by slore at 2008-10-15 08:21 PM:
findstr
能否给点具体的代码,谢谢!
Can you give some specific code, thank you!
|
|
2008-10-18 11:24 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
 『第 9 楼』:
使用 LLM 解释/回答一下
把 UnxUtils 工具包中的 find.exe 提取出来就可以了。
find -maxdepth 3 -name "*.exe"
查找当前目录及其子目录中的所有exe文件,最大目录深度不超过3。
Just extract find.exe from the UnxUtils toolkit.
find -maxdepth 3 -name "*.exe"
Find all .exe files in the current directory and its subdirectories, with a maximum directory depth of no more than 3.
|
|
2008-10-18 14:26 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
find.exe
http://upload.cn-dos.net/img/772.zip
相关帖子: http://www.cn-dos.net/forum/viewthread.php?tid=43057
C:\Test>find --version
GNU find version 4.1
C:\Test>find --help
Usage: find
default path is the current directory; default expression is -print
expression may consist of:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
options (always true): -daystart -depth -follow --help
-maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -ipath PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm MODE -regex PATTERN
-size N -true -type -uid N -used N -user NAME
-xtype
actions: -exec COMMAND ; -fprint FILE -fprint0 FILE -fprintf FILE FORMAT
-ok COMMAND ; -print -print0 -printf FORMAT -prune -ls
由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统(NFS),find命令在该文件系统中同样有效,只要你具有相应的权限。
在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。
find命令的一般形式为:
find pathname -options
让我们来看看该命令的参数:
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为' command' {} \;
注意{ }和\;之间的空格。
-ok: 和exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
先了解一下find所带的参数,能实现哪些功能
find命令选项
-name:按照文件名查找文件。
-perm:按照文件权限来查找文件。
-prune:使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
-user: 按照文件属主来查找文件。
-group:按照文件所属的组来查找文件。
-mtime -n +n:按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有- atime和- ctime选项,但它们都和- mtime选项。
-nogroup:查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser:查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2:查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size n: 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
下面这个选项我觉得这个很好用,俺以前都不知道,所以放在第一个说明:
使用exec或ok来执行shell命令
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。(在有些操作系统中只允许-exec选项执行诸如ls或ls -l这样的命令)。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。
exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。
为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中
# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:
$ find logs -type f -mtime +5 -exec rm {} \;
记住,在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
在下面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。
$ find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./conf/httpd.conf > ? n
按y键删除文件,按n键不删除。
任何形式的命令都可以在-exec选项中使用。
在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。
# find /etc -name "passwd*" -exec grep "sam" {} \;
sam:x:501:501::/usr/sam:/bin/bash
find命令的例子
查找当前用户主目录下的所有文件,下面两种方法都可以使用:
$ find $HOME -print
$ find ~ -print
为了查找在当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件,可以用:
$ find . -type f -perm 644 -exec ls -l {} \;
为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径,可以用:
$ find / -type f -size 0 -exec ls -l {} \;
查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们:
$ find /var/logs -type f -mtime +7 -ok rm {} \;
为了查找系统中所有属于root组的文件,可以用:
$find . -group root -exec ls -l {} \;
-rw-r--r-- 1 root root 595 10月 31 01:09 ./fie1
下面的find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。该命令只检查三位数字,所以相应文件的后缀不要超过999。
先建几个admin.log*的文件 ,才能使用下面这个命令
$ find . -name "admin.log" -atime -7 -ok rm {} \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n
为了查找当前文件系统中的所有目录并排序,可以用:
$ find . -type d | sort
为了查找系统中所有的rmt磁带设备,可以用:
$ find /dev/rmt -print
原书为:
为了查找当前文件系统中的所有目录并排序,可以用:
$ find . -type d -loacl -mount |sort
已更正为:
$ find . -type d |sort
xargs
在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。
在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;
而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。
来看看xargs命令是如何同find命令一起使用的,并给出一些例子。
下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory: ISO-8859 text\
......
在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:
$ find / -name "core" -print | xargs echo "" >/tmp/core.log
上面这个执行太慢,我改成在当前目录下查找
#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6
在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:
# ls -l
drwxrwxrwx 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 httpd.conf
# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
用grep命令在所有的普通文件中搜索hostname这个词:
# find . -type f -print | xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your
用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:
# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your
注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。
find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。
下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册
1、使用name选项
文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。
可以使用某种文件名模式来匹配文件,记住要用双引号将文件名模式引起来。
不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为pathname参数,波浪号~代表了你的$HOME目录。
想要在当前目录及子目录中查找所有的‘*.txt’文件,可以用:
$ find . -name "*.txt" -print
想要在当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:
$ find . -name "*" -print
想要在/etc目录中查找文件名以host开头的文件,可以用:
$ find /etc -name "host*" -print
想要查找$HOME目录中的文件,可以用:
$ find ~ -name "*" -print 或find . -print
要想让系统高负荷运行,就从根目录开始查找所有的文件。
$ find / -name "*" -print
如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是*.txt的文件,下面的命令就能够返回名为ax37.txt的文件:
$find . -name ".txt" -print
2、用perm选项
按照文件权限模式用-perm选项。按文件权限模式来查找文件的话。最好使用八进制的权限表示法。
如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:
$ find . -perm 755 -print
还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666
# ls -l
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19 temp
# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp
3、忽略某个目录
如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。
如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:
$ find /apps -path "/apps/bin" -prune -o -print
find 在路径列表的后面的是表达式
-path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o -print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune ,与逻辑表达式为假。 如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print 返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。
这个表达式组合特例可以用伪码写为
if -path "/usr/sam" then
-prune
else
-print
避开多个文件夹
find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print
圆括号表示表达式的结合。
\ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。
查找某一确定文件,-name等选项加在-o之后
#find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print
4、使用user和nouser选项
按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:
$ find ~ -user sam -print
在/etc目录下查找文件属主为uucp的文件:
$ find /etc -user uucp -print
为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名,find命令能够为你完成相应的工作。
例如,希望在/home目录下查找所有的这类文件,可以用:
$ find /home -nouser -print
5、使用group和nogroup选项
就像user和nouser选项一样,针对文件所属于的用户组,find命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:
$ find /apps -group gem -print
要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件
$ find / -nogroup -print
6、按照更改时间或访问时间等查找文件
如果希望按照更改时间来查找文件,可以使用mtime, atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。
用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。
希望在系统根目录下查找更改时间在5日以内的文件,可以用:
$ find / -mtime -5 -print
为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:
$ find /var/adm -mtime +3 -print
7、查找比某个文件新或旧的文件
如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:
newest_file_name ! oldest_file_name
其中,!是逻辑非符号。
查找更改时间比文件sam新但比文件temp旧的文件:
例:有两个文件
-rw-r--r-- 1 sam adm 0 10月 31 01:07 fiel
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19 temp
# find -newer httpd1.conf ! -newer temp -ls
1077669 0 -rwxrwxr-x 2 sam adm 0 10月 31 01:01 ./httpd.conf
1077671 4 -rw-rw-rw- 1 root root 2792 10月 31 20:19 ./temp
1077673 0 -rw-r--r-- 1 sam adm 0 10月 31 01:07 ./fiel
查找更改时间在比temp文件新的文件:
$ find . -newer temp -print
8、使用type选项
在/etc目录下查找所有的目录,可以用:
$ find /etc -type d -print
在当前目录下查找除目录以外的所有类型的文件,可以用:
$ find . ! -type d -print
在/etc目录下查找所有的符号链接文件,可以用:
$ find /etc -type l -print
9、使用size选项
可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为Nc;以块计量文件长度只用数字表示即可。
在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
在当前目录下查找文件长度大于1M字节的文件:
$ find . -size +1000000c -print
在/home/apache目录下查找文件长度恰好为100字节的文件:
$ find /home/apache -size 100c -print
在当前目录下查找长度超过10块的文件(一块等于512字节):
$ find . -size +10 -print
10、使用depth选项
在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。
在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。
它将首先匹配所有的文件然后再进入子目录中查找。
$ find / -name "CON.FILE" -depth -print
11、使用mount选项
在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。
从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:
$ find . -name "*.XC" -mount -print
12、使用cpio选项
cpio命令可以用来向磁带设备备份文件或从中恢复文件。可以使用find命令在整个文件系统中(更多的情况下是在部分文件系统中)查找文件,然后用cpio命令将其备份到磁带上。
如果希望使用cpio命令备份/etc、/home和/apps目录中的文件,可以使用下面所给出的命令,不过要记住你是在文件系统的根目录下:
#cd /
#find etc home apps -depth -print | cpio -ivcdC65535 -o
在上面的例子中,应当注意到路径中缺少/。这叫作相对路径。之所以使用相对路径,是因为在从磁带中恢复这些文件的时候,可以选择恢复文件的路径。例如,可以将这些文件先恢复到另外一个目录中,对它们进行某些操作后,再恢复到原始目录中。如果在备份时使用了绝对路径,例如/etc,那么在恢复时,就只能恢复到/etc目录中去,别无其他选择。在上面的例子中,我告诉find命令首先进入/etc目录,然后是/home和/apps目录,先匹配这些目录下
的文件,然后再匹配其子目录中的文件,所有这些结果将通过管道传递给cpio命令进行备份。
顺便说一下,在上面的例子中cpio命令使用了C65535选项,本可以使用B选项,不过这样每块的大小只有512字节,而使用了C65535选项后,块的大小变成了64K字节(65535/1024)。
Last edited by HAT on 2008-10-18 at 15:40 ]
find.exe
http://upload.cn-dos.net/img/772.zip
Related posts: http://www.cn-dos.net/forum/viewthread.php?tid=43057
C:\Test>find --version
GNU find version 4.1
C:\Test>find --help
Usage: find
default path is the current directory; default expression is -print
expression may consist of:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
options (always true): -daystart -depth -follow --help
-maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -ipath PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm MODE -regex PATTERN
-size N -true -type -uid N -used N -user NAME
-xtype
actions: -exec COMMAND ; -fprint FILE -fprint0 FILE -fprintf FILE FORMAT
-ok COMMAND ; -print -print0 -printf FORMAT -prune -ls
Because find has powerful functions, so it also has many options, and most of these options are worth our time to understand. Even if the system contains a network file system (NFS), the find command is still effective in that file system, as long as you have the corresponding permissions.
When running a very resource-consuming find command, many people tend to run it in the background, because traversing a large file system may take a long time (here it refers to a file system of 30 GB or more).
The general form of the find command is:
find pathname -options
Let's take a look at the parameters of this command:
pathname: The directory path to be searched by the find command. For example, use. to represent the current directory and / to represent the system root directory.
-print: The find command outputs the matched file to standard output.
-exec: The find command executes the shell command given by this parameter on the matched file. The form of the corresponding command is ' command' {} \;
Note the space between {} and \;.
-ok: It has the same function as exec, but it executes the shell command given by this parameter in a more secure mode. Before executing each command, it will prompt you to determine whether to execute it.
First understand the parameters carried by find and what functions can be achieved
find command options
-name: Find files according to the file name.
-perm: Find files according to file permissions.
-prune: Using this option can make the find command not search in the current specified directory. If the -depth option is used at the same time, then -prune will be ignored by the find command.
-user: Find files according to the file owner.
-group: Find files according to the group to which the file belongs.
-mtime -n +n: Find files according to the change time of the file. -n means that the file change time is within n days from now, and +n means that the file change time is before n days from now. The find command also has -atime and -ctime options, but they are all related to the -mtime option.
-nogroup: Find files with no valid belonging group, that is, the group to which the file belongs does not exist in /etc/groups.
-nouser: Find files with no valid owner, that is, the owner of the file does not exist in /etc/passwd.
-newer file1 ! file2: Find files whose change time is newer than file1 but older than file2.
-type Find files of a certain type, such as:
b - block device file.
d - directory.
c - character device file.
p - pipe file.
l - symbolic link file.
f - ordinary file.
-size n: Find files with a length of n blocks. When c is carried, it means that the file length is counted in bytes.
-depth: When searching for files, first search for files in the current directory, and then search in its subdirectories.
-fstype: Find files located in a certain type of file system. These file system types can usually be found in the configuration file /etc/fstab, which contains information about file systems in this system.
-mount: Do not cross the file system mount point when searching for files.
-follow: If the find command encounters a symbolic link file, it will track to the file pointed to by the link.
-cpio: Use the cpio command for the matched file and back up these files to the tape device.
The following option I think is very useful, I didn't know it before, so I put it in the first explanation:
Use exec or ok to execute shell commands
When using find, as long as you write the desired operation in a file, you can use exec to cooperate with find to search, which is very convenient. (In some operating systems, only the -exec option is allowed to execute commands such as ls or ls -l). Most users use this option to find old files and delete them. It is recommended that before actually using the rm command to delete files, it is best to first use the ls command to see and confirm that they are the files to be deleted.
The exec option is followed by the command or script to be executed, then a pair of {}, a space and a \, and finally a semicolon.
To use the exec option, the print option must be used at the same time. If you verify the find command, you will find that the command only outputs the relative path and file name from the current path.
For example: To list the matched files with the ls -l command, you can put the ls -l command in the -exec option of the find command
# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
In the above example, the find command matches all ordinary files in the current directory and uses the ls -l command in the -exec option to list them.
Find files in the /logs directory that were changed more than 5 days ago and delete them:
$ find logs -type f -mtime +5 -exec rm {} \;
Remember, before deleting files in any way in the shell, you should first check the corresponding files and be careful! When using commands such as mv or rm, you can use the safe mode of the -exec option. It will prompt you before performing any operation on each matched file.
In the following example, the find command finds all files ending with.log in the current directory and whose change time is more than 5 days ago, and deletes them, but first gives a prompt before deleting.
$ find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./conf/httpd.conf > ? n
Press y to delete the file and n to not delete it.
Any form of command can be used in the -exec option.
In the following example, we use the grep command. The find command first matches all files named "passwd*", such as passwd, passwd.old, passwd.bak, and then executes the grep command to see if there is a sam user in these files.
# find /etc -name "passwd*" -exec grep "sam" {} \;
sam:x:501:501::/usr/sam:/bin/bash
Examples of the find command
To find all files in the current user's home directory, both of the following methods can be used:
$ find $HOME -print
$ find ~ -print
To find files in the current directory where the file owner has read and write permissions, and the users of the file's belonging group and other users have read permissions, you can use:
$ find . -type f -perm 644 -exec ls -l {} \;
To find all ordinary files in the system with a length of 0 and list their complete paths, you can use:
$ find / -type f -size 0 -exec ls -l {} \;
Find ordinary files in the /var/logs directory that were changed more than 7 days ago and ask for them before deleting:
$ find /var/logs -type f -mtime +7 -ok rm {} \;
To find all files belonging to the root group in the system, you can use:
$find . -group root -exec ls -l {} \;
-rw-r--r-- 1 root root 595 10月 31 01:09 ./fie1
The following find command will delete the admin.log files with numeric suffixes in the directory whose access time is within 7 days. This command only checks three-digit numbers, so the suffix of the corresponding file should not exceed 999.
First create a few admin.log* files before using this command
$ find . -name "admin.log" -atime -7 -ok rm {} \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n
To find all directories in the current file system and sort them, you can use:
$ find . -type d | sort
To find all rmt tape devices in the system, you can use:
$ find /dev/rmt -print
The original book says:
To find all directories in the current file system and sort them, you can use:
$ find . -type d -loacl -mount |sort
It has been corrected to:
$ find . -type d |sort
xargs
When using the -exec option of the find command to process matched files, the find command passes all matched files to exec for execution at once. But some systems have a limit on the length of commands that can be passed to exec. After a few minutes of the find command running, an overflow error will occur. The error message is usually "Argument list too long" or "Argument list overflow". This is where the xargs command comes in, especially when used with the find command.
The find command passes the matched files to the xargs command, and the xargs command only gets a part of the files at a time instead of all, unlike the -exec option. In this way, it can process the first part of the files first, then the next batch, and so on.
In some systems, using the -exec option will start a corresponding process for each matched file to be processed, not all matched files as parameters to be executed at once; in this way, in some cases, there will be too many processes and the system performance will decrease. The problem is, so the efficiency is not high;
And using the xargs command has only one process. In addition, when using the xargs command, whether to get all parameters at once, or get parameters in batches, and the number of parameters obtained each time will be determined according to the options of this command and the corresponding adjustable parameters in the system kernel.
Let's see how the xargs command is used with the find command and give some examples.
The following example finds every ordinary file in the system, and then uses the xargs command to test which category each of them belongs to
#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory: ISO-8859 text\
......
Find memory information dump files (core dump) throughout the system, and then save the results to the /tmp/core.log file:
$ find / -name "core" -print | xargs echo "" >/tmp/core.log
This execution is too slow. I changed it to search in the current directory
#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6
Find all files in the current directory where the user has read, write and execute permissions, and withdraw the corresponding write permissions:
# ls -l
drwxrwxrwx 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 httpd.conf
# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
Use the grep command to search for the word hostname in all ordinary files:
# find . -type f -print | xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your
Use the grep command to search for the word hostnames in all ordinary files in the current directory:
# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your
Note that in the above example, \ is used to cancel the special meaning of * in the shell in the find command.
The find command can use exec and xargs to allow users to execute almost all commands on the matched files.
The following are examples of some commonly used parameters of find. You can check them when you need them. For example, in the previous few posts, some of these parameters are used, and you can also use man or check other posts in the forum for the command manual of find
1. Use the name option
The filename option is the most commonly used option of the find command, either used alone or in combination with other options.
You can use a certain filename pattern to match files, remember to enclose the filename pattern in double quotes.
No matter what the current path is, if you want to find files with the *.txt filename in your home directory $HOME, use ~ as the pathname parameter, and the tilde ~ represents your $HOME directory.
To find all '*.txt' files in the current directory and its subdirectories, you can use:
$ find . -name "*.txt" -print
To find files in the current directory and its subdirectories that start with an uppercase letter, you can use:
$ find . -name "*" -print
To find files starting with host in the /etc directory, you can use:
$ find /etc -name "host*" -print
To find files in the $HOME directory, you can use:
$ find ~ -name "*" -print or find . -print
To make the system run at high load, start searching for all files from the root directory.
$ find / -name "*" -print
If you want to find files in the current directory that start with two lowercase letters, followed by two numbers, and end with *.txt, the following command will return the file named ax37.txt:
$find . -name ".txt" -print
2. Use the perm option
Use the -perm option according to the file permission mode. When looking for files according to the file permission mode, it is best to use the octal permission notation.
For example, to find files with file permission bits 755 in the current directory, that is, files that the file owner can read, write, and execute, and other users can read and execute, you can use:
$ find . -perm 755 -print
There is another expression method: add a horizontal bar - in front of the octal number to indicate that all match, such as -007 is equivalent to 777, -006 is equivalent to 666
# ls -l
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19 temp
# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp
3. Ignore a certain directory
If you want to ignore a certain directory when searching for files because you know there are no files you are looking for in that directory, you can use the -prune option to indicate the directory to be ignored. Be careful when using the -prune option, because if you use the -depth option at the same time, the -prune option will be ignored by the find command.
If you want to search for files under the /apps directory but do not want to search under the /apps/bin directory, you can use:
$ find /apps -path "/apps/bin" -prune -o -print
find The expression after the path list is
-path "/usr/sam" -prune -o -print is a short form of -path "/usr/sam" -a -prune -o -print. The expression is evaluated in order. -a and -o are both short-circuit evaluations, similar to && and || in shell. If -path "/usr/sam" is true, then evaluate -prune, -prune returns true, and the logical expression is true; otherwise, do not evaluate -prune, and the logical expression is false. If -path "/usr/sam" -a -prune is false, then evaluate -print, -print returns true, and the logical expression is true; otherwise, do not evaluate -print, and the logical expression is true.
This special combination of expressions can be written in pseudocode as
if -path "/usr/sam" then
-prune
else
-print
Avoid multiple folders
find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print
Parentheses represent the combination of expressions.
\ means reference, that is, instruct the shell not to make special explanations for the following characters, but leave it to the find command to interpret its meaning.
Find a certain determined file, and the -name and other options are added after -o
#find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print
4. Use the user and nouser options
Find files according to the file owner. For example, to find files with the owner sam in the $HOME directory, you can use:
$ find ~ -user sam -print
To find files with the owner uucp under the /etc directory:
$ find /etc -user uucp -print
To find files whose owner account has been deleted, you can use the -nouser option. In this way, you can find those files whose owner does not have a valid account in the /etc/passwd file. When using the -nouser option, you don't need to give the user name, and the find command can do the corresponding work for you.
For example, if you want to find all such files under the /home directory, you can use:
$ find /home -nouser -print
5. Use the group and nogroup options
Just like the user and nouser options, the find command has the same options for the user group to which the file belongs. To find files belonging to the gem user group under the /apps directory, you can use:
$ find /apps -group gem -print
To find all files that do not have a valid belonging user group, you can use the nogroup option. The following find command finds such files from the root directory of the file system
$ find / -nogroup -print
6. Find files according to change time or access time, etc.
If you want to find files according to the change time, you can use the mtime, atime or ctime options. If the system suddenly has no available space, it is very likely that the length of a certain file has increased rapidly during this period. At this time, you can use the mtime option to find such files.
Use the minus sign - to limit files whose change time is within n days from now, and use the plus sign + to limit files whose change time is before n days from now.
To find files in the system root directory whose change time is within 5 days, you can use:
$ find / -mtime -5 -print
To find files in the /var/adm directory whose change time is more than 3 days ago, you can use:
$ find /var/adm -mtime +3 -print
7. Find files newer or older than a certain file
If you want to find all files whose change time is newer than a certain file but older than another file, you can use the -newer option. Its general form is:
newest_file_name ! oldest_file_name
Among them,! is the logical NOT symbol.
Find files whose change time is newer than the file sam but older than the file temp:
Example: There are two files
-rw-r--r-- 1 sam adm 0 10月 31 01:07 fiel
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19 temp
# find -newer httpd1.conf ! -newer temp -ls
1077669 0 -rwxrwxr-x 2 sam adm 0 10月 31 01:01 ./httpd.conf
1077671 4 -rw-rw-rw- 1 root root 2792 10月 31 20:19 ./temp
1077673 0 -rw-r--r-- 1 sam adm 0 10月 31 01:07 ./fiel
Find files whose change time is newer than the temp file:
$ find . -newer temp -print
8. Use the type option
To find all directories under the /etc directory, you can use:
$ find /etc -type d -print
To find all types of files except directories in the current directory, you can use:
$ find . ! -type d -print
To find all symbolic link files under the /etc directory, you can use:
$ find /etc -type l -print
9. Use the size option
You can find files according to the file length. The file length referred to here can be measured in blocks or in bytes. The expression form of measuring the file length in bytes is Nc; only the number is used to measure the file length in blocks.
When finding files according to the file length, generally use this file length expressed in bytes, because when viewing the size of the file system, it is easier to convert by using blocks.
To find files in the current directory whose length is greater than 1M bytes:
$ find . -size +1000000c -print
To find files in the /home/apache directory whose length is exactly 100 bytes:
$ find /home/apache -size 100c -print
To find files in the current directory that are more than 10 blocks long (one block is 512 bytes):
$ find . -size +10 -print
10. Use the depth option
When using the find command, you may want to match all files first and then search in the subdirectories. Using the depth option can make the find command do this. One reason for doing this is that when using the find command to back up the file system to a tape, you want to back up all files first and then back up the files in the subdirectories.
In the following example, the find command starts from the root directory of the file system and finds a file named CON.FILE.
It will first match all files and then search in the subdirectories.
$ find / -name "CON.FILE" -depth -print
11. Use the mount option
To find files in the current file system (not entering other file systems), you can use the mount option of the find command.
Find files ending with XC in the current directory and whose file name is located in this file system:
$ find . -name "*.XC" -mount -print
12. Use the cpio option
The cpio command can be used to back up files to a tape device or restore files from it. You can use the find command to find files in the entire file system (more often in part of the file system), and then use the cpio command to back up them to the tape.
If you want to use the cpio command to back up the files in the /etc, /home and /apps directories, you can use the following command, but remember that you are under the root directory of the file system:
#cd /
#find etc home apps -depth -print | cpio -ivcdC65535 -o
In the above example, you should notice that there is no / in the path. This is called a relative path. The reason for using a relative path is that when restoring these files from the tape, you can choose the path to restore the files. For example, you can restore the files to another directory first, perform some operations on them, and then restore them to the original directory. If an absolute path is used during backup, such as /etc, then during restoration, it can only be restored to the /etc directory, and there is no other choice. In the above example, I told the find command to first enter the /etc directory, then the /home and /apps directories, match the files under these directories first, and then match the files in their subdirectories. All these results will be passed through the pipeline to the cpio command for backup.
By the way, in the above example, the cpio command uses the C65535 option. The B option could have been used, but then the size of each block is only 512 bytes, and after using the C65535 option, the size of the block becomes 64K bytes (65535/1024).
Last edited by HAT on 2008-10-18 at 15:40 ]
|

 |
|
2008-10-18 15:36 |
|
|
wgmlzj
初级用户
 
积分 97
发帖 40
注册 2007-11-26
状态 离线
|
|
2008-10-18 16:35 |
|
|