我有一个文件folders.txt
编写shell脚本扫描文件夹列表
one
two
three
four
...
具有文件夹名称的列表。 [one
,two
,three
和four
是文件夹的名称]。
这些文件夹中的每一个都有许多不同类型(不同扩展名)的文件。我想要一个特定扩展名的所有文件夹中的所有文件的列表,如.txt
。
我的shell脚本应该怎么样?
我有一个文件folders.txt
编写shell脚本扫描文件夹列表
one
two
three
four
...
具有文件夹名称的列表。 [one
,two
,three
和four
是文件夹的名称]。
这些文件夹中的每一个都有许多不同类型(不同扩展名)的文件。我想要一个特定扩展名的所有文件夹中的所有文件的列表,如.txt
。
我的shell脚本应该怎么样?
鉴于后期简直是要求的文件列表,这很简单:
tmp=$IFS
IFS=$(echo -en "\n\b")
for i in `cat folders.txt` ; do
ls -l "$i/*.txt"
done
IFS=$tmp
单程
while read -r folders
do
# add -maxdepth 1 if recursive traversal is not required
find "$folders" -type f -iname "*.txt" | while read -r FILE
do
echo "do something with $FILE"
done
done <"file"
或
folders=$(<file)
find $folders -type f -iname "*.txt" | while read -r FILE
do
echo "do something with $FILE"
done
猛砸4.0(如果需要递归查找)
shopt -s globstar
folders=$(<file)
for d in $folders
do
for file in $d/**/*.txt
do
echo "do something with $file"
done
done
我想补充的-prune选项来查找,这样子目录也没有搜索 – ennuikiller 2010-02-25 05:17:01
OP并不表示,但他可以随时添加'-maxdepth 1'如果他不想遍历目录 – ghostdog74 2010-02-25 05:21:06
简单地做它的命令行:
xargs ls -l < folders.txt | grep '.txt$'
我喜欢这个答案的简单性。 – 2010-02-25 05:32:57
失去了猫,它更简单。 'xargs ls -l <folder.txt'。或'xargs -a folder.txt ls -l' – ghostdog74 2010-02-25 05:36:45
@Ehan,我也很喜欢简单,但只是为了显示那些txt文件。如果需要对每个文件进行进一步处理,则必须对其进行增强。 – ghostdog74 2010-02-25 05:45:09
请注意带空格的文件夹名称。在必要时加上引号 – ghostdog74 2010-02-25 05:16:24
即使原始文章没有要求,我已经添加了它。请注意,您的示例并不都是考虑空间的。 – 2010-02-25 05:27:21
真的吗?哪个。告诉我,我会编辑它。 – ghostdog74 2010-02-25 05:28:23