我怎样才能读取在bash文件的文件名列表? [英] How can I read a list of filenames from a file in bash?
问题描述
我试图写一个bash脚本,将处理的文件,其名称都存储每行一个输入文件,东西喜欢的列表
I'm trying to write a bash script that will process a list of files whose names are stored one per line in an input file, something the likes of
find . -type f -mtime +15 > /tmp/filelist.txt
for F in $(cat /tmp/filelist.txt) ; do
...
done;
我的问题是Filelist.txt中文件名可能包含空格,因此上述剪断将扩大该行
My problem is that filenames in filelist.txt may contain spaces, so the snipped above will expand the line
my text file.txt
三个不同的文件名,我的
,文本
和 file.txt的
。我该如何解决呢?
to three different filenames, my
, text
and file.txt
. How can I fix that?
推荐答案
使用读
:
while read F ; do
echo $F
done </tmp/filelist.txt
或者使用IFS来改变shell如何分隔列表:
Alternatively use IFS to change how the shell separates your list:
OLDIFS=$IFS
IFS="
"
for F in $(cat /tmp/filelist.txt) ; do
echo $F
done
IFS=$OLDIFS
另外(由@tangens的建议),将您的循环体为一个单独的脚本,然后使用找到
的-exec选项,如果为每个文件运行直接找到。
Alternatively (as suggested by @tangens), convert the body of your loop into a separate script, then use find
's -exec option to run if for each file found directly.
这篇关于我怎样才能读取在bash文件的文件名列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!