Read list of files and copy deleted files back

2019-08-14 06:40发布

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 {}'

Can somebody tell me how do i correct this? I just get an error from cut saying that the file does not exist and the whole line which i understand to be is cut is treating it as a file.

EDIT: I created the list of files and saved it to a text file named deletdfiles using hg status. In the script i'll replace echo at the end with cp command this was just to test if i can atleast get it to echo.

The deltedfiles looks likes this

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 {}'

Can somebody tell me how do i correct this? I just get an error from cut saying that the file does not exist and the whole line which i understand to be is cut is treating it as a file.

EDIT: I created the list of files and saved it to a text file named deletdfiles using hg status. In the script i'll replace echo at the end with cp command this was just to test if i can atleast get it to echo.

The deltedfiles looks likes this

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

After running the above command this is the output

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

标签: bash shell sh
1条回答
太酷不给撩
2楼-- · 2019-08-14 07:04

If I understand the problem correctly, something like this could work:

$ 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

Replace echo with cp if it produces the correct output.

If you only want to copy the files that are in a line that starts with the word R you can do it like this:

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

Or in a more readable format:

while read -r letter file _ 
do 
    [[ $letter = "R" ]] && echo "$file" "otherdir/$file"
done < deltedfiles
查看更多
登录 后发表回答