我怎样才能读取在bash文件的文件名列表? [英] How can I read a list of filenames from a file in bash?

查看:115
本文介绍了我怎样才能读取在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屋!

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