我想显示所有的目录,不包含具有特定文件结尾的文件。 所以我尝试使用下面的代码:
find . -type d \! -exec test -e '{}/*.ENDING' \; -print
在这个例子中,我想显示所有的目录,不包含结尾.ENDING文件,但是这不起作用。
我的错误在哪里?
这是三个步骤的解决scheme:
temeraire:tmp jenny$ find . -type f -name \*ENDING -exec dirname {} \; |sort -u > /tmp/ending.lst temeraire:tmp jenny$ find . -type d |sort -u > /tmp/dirs.lst temeraire:tmp jenny$ comm -3 /tmp/dirs.lst /tmp/ending.lst
开始了!
#!/usr/bin/env python3 import os for curdir,dirnames,filenames in os.walk('.'): if len(tuple(filter(lambda x: x.endswith('.ENDING'), filenames))) == 0: print(curdir)
或者交替(和更pythonic):
#!/usr/bin/env python3 import os for curdir,dirnames,filenames in os.walk('.'): # Props to Cristian Cliupitu for the better python if not any(x.endswith('.ENDING') for x in filenames): print(curdir)
奖金DLC内容!
find命令的(大部分)更正版本:
find . -type d \! -exec bash -c 'set nullglob; test -f "{}"/*.ENDING' \; -print
shell扩展了* ,但是在你的情况下没有涉及shell,只是由find执行的testing命令。 因此,存在被testing的文件名字就是*.ENDING 。
相反,你应该使用这样的东西:
find . -type d \! -execdir sh -c 'test -e {}/*.ENDING' \; -print
当执行testing时,会导致sh扩展*.ENDING 。
资料来源: 在UX.SE上findglobbing
受到Dennis Nolte和MikeyB的回答的启发,我提出了这个解决scheme:
find . -type d \ \! -exec bash -c 'shopt -s failglob; echo "{}"/*.ENDING >/dev/null' \; \ -print 2>/dev/null
它基于事实工作
如果设置了failglob shell选项,并且找不到匹配项,则将打印一条错误消息,并且不执行该命令。
顺便说一句,这就是为什么stderr被redirect到/dev/null 。
我会亲自做perl
#!/usr/bin/perl use strict; use warnings; use File::Find; #this sub is called by 'find' on each file it traverses. sub checkdir { #skip non directories. return unless ( -d $File::Find::name ); #glob will return an empty array if no files math - which evaluates as 'false' if ( not glob ( "$File::Find::name/*.ENDING" ) ) { print "$File::Find::name does not contain any files that end with '.ENDING'\n"; } } #kick off the search on '.' - set a directory path or list of directories to taste. # - you can specify multiple in a list if you so desire. find ( \&checkdir, "." );
应该做的伎俩(工作在我非常简单的testing案例)。
inheritance人find一个class轮。
find ./ -type d ! -regex '.*.ENDING$' -printf "%h\n" | sort -u
编辑 :哎呀,不会工作。
find . -type d '!' -exec sh -c 'ls -1 "{}"|egrep -q "*ENDING$"' ';' -print
在egrep q是安静的
使用egrep你可以交换你需要的正则expression式
ls -1 "{}"从find命令输出文件名