Linux 向find命令返回的文件列表添加编号

Linux 向find命令返回的文件列表添加编号,linux,Linux,我运行find命令来获取具有特定大小的文件列表,然后将输出保存在一个文件中,现在我逐个遍历该文件,并询问用户要删除哪个文件。我想在列表中的每个文件旁边添加一个数字,这样用户就可以直接输入与该文件相关联的数字并删除,而不必查看整个文件。请帮忙 select f in $(find . -name '*.txt'); do if [ -n "$f" ]; then # put your command here echo "rm $f" fi done

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

select f in $(find . -name '*.txt'); do
    if [ -n "$f" ]; then
        # put your command here
        echo "rm $f"
    fi
done
在没有中间文件的情况下,询问每个文件是否执行操作

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

另一种精益方法是使用
选择

select fno in $(find . -size 5k); 
do  
    echo rm $fno
done 
这是一种巴什主义,可能不存在于你的外壳中

帮助选择
显示其用法。不幸的是,它也不像find解决方案那样,允许一次选择多个条目,但是您会得到重复选择某个条目的机会,直到您点击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: 
#? 
请注意,单词由空格分隔,因此在第二个示例中,如果必须在文件名中使用空格,则必须小心

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

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

另一种精益方法是使用
选择

select fno in $(find . -size 5k); 
do  
    echo rm $fno
done 
这是一种巴什主义,可能不存在于你的外壳中

帮助选择
显示其用法。不幸的是,它也不像find解决方案那样,允许一次选择多个条目,但是您会得到重复选择某个条目的机会,直到您点击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: 
#? 

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

为什么不在
的输出上循环查找
并询问用户?如果我理解正确,为什么不使用
find
exec
组合并使用
rm-I
,这是一种交互式删除,在删除文件之前会询问用户[y/n]查询。因此,不需要将输出保存到文件中,除非您将该文件用于除列出要删除的文件之外的其他用途。为什么不在
find
的输出上循环并询问用户?如果我理解正确,为什么不使用
find
exec
组合并使用
rm-I
,这是一种交互式删除,在删除文件之前会询问用户[y/n]查询。不需要将输出保存到文件中,除非您将该文件用于除列出要删除的文件之外的其他用途