读取文件列表并将已删除的文件复制回来

时间:2014-05-26 08:37:46

标签: bash shell sh

我做了一个mercurial merge,它删除了我在新分支中需要的一些文件,所以我生成了一个已删除文件的列表,现在我正在尝试构建一个shell脚本来复制已删除的文件,但到目前为止它还没有working.The script is question is

cat deltedfiles | xargs -I {} bash -c 'cut -d" " -f1 "{}"|grep -Fq "R" && echo {}'

有人可以告诉我如何更正此问题? 我只是从剪切中得到一个错误,说该文件不存在,并且我理解的整条线被切割为将其视为文件。

编辑: 我创建了文件列表并使用hg status将其保存到名为deletdfiles的文本文件中。 在脚本中,我将用cp命令替换最后的echo,这只是为了测试我是否至少可以让它回显。

detedfiles看起来像这样

cat deltedfiles
I did a mercurial merge which deleted a few files which i need in the new branch so i generated a list of the deleted files and am now trying to build a shell script to copy the deleted files back but so far it is not working.The script is question is

cat deltedfiles | xargs -I {} bash -c 'cut -d" " -f1 "{}"|grep -Fq "R" && echo {}'

有人可以告诉我如何更正此问题? 我只是从剪切中得到一个错误,说该文件不存在,并且我理解的整条线被切割为将其视为文件。

编辑: 我创建了文件列表并使用hg status将其保存到名为deletdfiles的文本文件中。 在脚本中,我将用cp命令替换最后的echo,这只是为了测试我是否至少可以让它回显。

detedfiles看起来像这样

cat deltedfiles
R python/cmsintegration/events/migrations/0002_improved_navigation.py
R python/cmsintegration/events/migrations/0003_sitemap_fields.py
R python/cmsintegration/events/migrations/0004_cache_differentiation.py
R python/cmsintegration/events/migrations/0005_cms_displayables_seo.py
R python/cmsintegration/events/migrations/0006_nav_has_changed.py
R python/cmsintegration/events/migrations/0007_compute_nav_order.py
R python/cmsintegration/events/migrations/0008_adding_mob_reg_link.py

运行上述命令后,这是输出

cut: R python/cmsintegration/events/migrations/0002_improved_navigation.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0003_sitemap_fields.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0004_cache_differentiation.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0005_cms_displayables_seo.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0006_nav_has_changed.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0007_compute_nav_order.py: No such file or directory
cut: R python/cmsintegration/events/migrations/0008_adding_mob_reg_link.py: No such file or directory

1 个答案:

答案 0 :(得分:1)

如果我理解正确的问题,这样的事情可能有效:

$ while read -r _ file; do echo "$file" "otherdir/$file"; done < deltedfiles
python/cmsintegration/events/migrations/0002_improved_navigation.py newfolder/python/cmsintegration/events/migrations/0002_improved_navigation.py
python/cmsintegration/events/migrations/0003_sitemap_fields.py newfolder/python/cmsintegration/events/migrations/0003_sitemap_fields.py
python/cmsintegration/events/migrations/0004_cache_differentiation.py newfolder/python/cmsintegration/events/migrations/0004_cache_differentiation.py
python/cmsintegration/events/migrations/0005_cms_displayables_seo.py newfolder/python/cmsintegration/events/migrations/0005_cms_displayables_seo.py
python/cmsintegration/events/migrations/0006_nav_has_changed.py newfolder/python/cmsintegration/events/migrations/0006_nav_has_changed.py
python/cmsintegration/events/migrations/0007_compute_nav_order.py newfolder/python/cmsintegration/events/migrations/0007_compute_nav_order.py
python/cmsintegration/events/migrations/0008_adding_mob_reg_link.py newfolder/python/cmsintegration/events/migrations/0008_adding_mob_reg_link.py

如果echo生成正确的输出,请将cp替换为R

如果您只想复制以单词while read -r letter file _ ; do [[ $letter = "R" ]] && echo "$file" "otherdir/$file"; done < deltedfiles 开头的行中的文件 可以这样做:

while read -r letter file _ 
do 
    [[ $letter = "R" ]] && echo "$file" "otherdir/$file"
done < deltedfiles

或者以更易读的格式:

{{1}}