2014-07-02 34 views
1

Unix命令使用
删除一个文件夹中包含超过14行的文件

wc -l * | grep -v "14" | rm -rf

然而,这组似乎并没有做的工作。任何人都可以用正确的方式指向我吗? 感谢

+0

rm未读取标准输入。 – aioobe

+0

无法为rm形成任何正则表达式,这可能表明该文件超过14行。 – dhvlnyk

+0

@ diablo8226:正则表达式不做算术运算。字符串99与字符串14不匹配,但是字符串2148不匹配。 – rici

回答

1

这里的打印出的所有文件与至少15行名称的一种方法(假设你了GNU AWK,为nextfile命令):

awk 'FNR==15{print FILENAME;nextfile}' * 

这将产生一个错误的任何子目录,所以这并不理想。

虽然你并不想打印文件名。你想删除它们。你可以这样做,在awksystem功能:

# The following has been defanged in case someone decides to copy&paste 
awk 'FNR==15{system("echo rm "FILENAME);nextfile}' * 
0

有几个问题,你的解决方案:rm不从标准输入接受输入,而且你只grep发现没有究竟 14谁的文件线。试试这个:

find . -type f -maxdepth 1 | while read f; do [ `wc -l $f | tr -s ' ' | cut -d ' ' -f 2` -gt 14 ] && rm $f; done 

下面是它如何工作的:

find . -type f -maxdepth 1 #all files (not directories) in the current directory 
[        #start comparison 
wc -l $f      #get line count of file 
tr -s ' '      #(on the output of wc) eliminate extra whitespace 
cut -d ' ' -f 2    #pick just the line count out of the previous output 
-gt 14 ]      #test if all that was greater than 14 
&& rm $f      #if the comparison was true, delete the file 

我试图找出只是用find-exec一个解决方案,但我不能想出一个办法来测试行数。也许别人可以想出办法

+2

不要使用'for'遍历行,使用它遍历**字**。这将打破任何与空格的文件名。 'read line'是阅读线条的基本成语。 –

2
wc -l * 2>&1 | while read -r num file; do ((num > 14)) && echo rm "$file"; done 

如果您对结果满意,请删除“echo”。

1
for f in *; do if [ $(wc -l $f | cut -d' ' -f1) -gt 14 ]; then rm -f $f; fi; done 
相关问题