从`find`循环文件名? [英] Loop over file names from `find`?

查看:162
本文介绍了从`find`循环文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行这个命令:

  sudo查找。 -name * .mp3 

然后我可以得到很多mp3文件的列表。



现在我想对每个mp3文件做一个循环。例如,我可以在循环中创建一个,并在第一个文件名中指定变量 file 。然后我可以用这个文件做一些事情。接下来,我可以将第二个文件的名字赋给变量 file ,然后做这个,等等。用linux shell命令来实现这个?任何帮助表示赞赏,谢谢!为此,使用读取内置:

  sudo find。 -name * .mp3 | 
读取文件名

echo$ filename#...或任何其他命令使用$文件名
完成

假如你的文件名不使用新行( \\\
)字符, 。


If I run this command:

sudo find . -name *.mp3

then I can get a listing of lots of mp3 files.

Now I want to do something with each mp3 file in a loop. For example, I could create a while loop, and inside assign the first file name to the variable file. Then I could do something with that file. Next I could assign the second file name to the variable file and do with that, etc.

How can I realize this using a linux shell command? Any help is appreciated, thanks!

解决方案

For this, use the read builtin:

sudo find . -name *.mp3 |
while read filename
do
    echo "$filename"    # ... or any other command using $filename
done

Provided that your filenames don't use the newline (\n) character, this should work fine.

这篇关于从`find`循环文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆