开发者

Is `xargs` the way to go with this problem?

开发者 https://www.devze.com 2023-04-07 07:15 出处:网络
I\'ve got a text file with a listing of file lo开发者_Python百科cations I want to move, first to temporary directory, do some in the source directory then move the files back to the original location.

I've got a text file with a listing of file lo开发者_Python百科cations I want to move, first to temporary directory, do some in the source directory then move the files back to the original location.

The listing file is of the format generated by, say, a find command, such as:

~/$ find . -name "*.[ch]" > ~/tmp/my_file_list.txt

Currently I'm using sed to manipulate the file on the screen, and cutting and pasting its output to issue the commands. I find this offensive, but it works:

~/$ sed -r 's_.+_cp & ~/tmp/_' ~/tmp/my_file_list.txt

(copy and paste outputs, then to put the files back)

~/$ sed -r 's_.+/(.+)_cp ~/tmp/\1 &_' ~/tmp/my_file_list.txt

(copy ... paste ... wince)

How could I do the above two lines simply without the copy and paste ... I'm thinking xargs might hold the solution I yearn.

[Edit] To deal with spaces:

~/$ sed -r 's_.+_cp "&" ~/tmp/_' ~/tmp/my_file_list.txt

and

~/$ sed -r 's_.+/(.+)_cp "~/tmp/\1" "&"_' ~/tmp/my_file_list.txt


Simply pipe the output into bash:

~/$ sed -r 's_.+_cp & ~/tmp/_' ~/tmp/my_file_list.txt | bash


Although I see you've already gotten an answer that solves the copy-paste problem, I would actually suggest using rsync for this. The sequence would go as follows:

~/$ find . -name "*.[ch]" > ~/tmp/my_file_list.txt

To backup:

~/$ rsync --files-from=tmp/my_file_list.txt . tmp/

Do whatever with the original files, then to restore:

~/$ rsync --files-from=tmp/my_file_list.txt tmp/ .

This has the (negligible) advantage of not copying files that you haven't modified, thus saving a bit of disk activity. It has the less negligible advantage that if you have multiple files with the same name in different directories, they won't clash because rsync preserves the relative paths.

0

精彩评论

暂无评论...
验证码 换一张
取 消