将数字添加到find命令返回的文件列表中

时间:2012-02-24 11:29:57

标签: linux

我正在运行find命令获取具有特定大小的文件列表然后我将输出保存在文件中,现在我逐个遍历此文件并询问用户要删除哪一个。我想做一些事情,比如在列表中的每个文件旁边添加一个数字,以便用户可以直接输入与该文件关联的数字并删除而不必遍历整个文件。请帮忙。

2 个答案:

答案 0 :(得分:1)

select f in $(find . -name '*.txt'); do
    if [ -n "$f" ]; then
        # put your command here
        echo "rm $f"
    fi
done

答案 1 :(得分:0)

find . -size 5k -okdir rm {} ";"

在没有中间文件的情况下,询问您是否执行操作的每个文件。

-okdir是要查找的Gnu扩展名,并不适用于所有实现。

另一种精益方法是使用select

select fno in $(find . -size 5k); 
do  
    echo rm $fno
done 

这是一种基础,可能不存在于你的shell中。

help select显示其用法。不幸的是,它不像find-solution那样允许一次选择多个条目,但是你可以反复选择一些东西,直到你点击 Ctrl + D ,这很安静。

select: select NAME [in WORDS ... ;] do COMMANDS; done

Select words from a list and execute commands.

The WORDS are expanded, generating a list of words.  The
set of expanded words is printed on the standard error, each
preceded by a number.  If `in WORDS' is not present, `in "$@"'
is assumed.  The PS3 prompt is then displayed and a line read
from the standard input.  If the line consists of the number
corresponding to one of the displayed words, then NAME is set
to that word.  If the line is empty, WORDS and the prompt are
redisplayed.  If EOF is read, the command completes.  Any other
value read causes NAME to be set to null.  The line read is saved
in the variable REPLY.  COMMANDS are executed after each selection
until a break command is executed.

Exit Status:
Returns the status of the last command executed.

这就是它的样子:

select fno in  *scala ; do  echo "fno: "  $fno; done  
1) Cartesian.scala       6) MWzufall.scala  
2) Haeufigkeit.scala     7) Shuffle.scala   
3) HelloChain.scala      8) eHWCChain.scala
4) Lychrel.scala         9) equilibrum.scala    
5) M.scala              10) scala
#? 3
fno:  HelloChain.scala
#? 3 4 
fno: 
#? 

请注意,单词由空格分隔,因此如果必须使用文件名中的空格,则必须注意第二个示例。