Search code examples
bashcopying

Copying list of files to a directory


I want to make a search for all .fits files that contain a certain text in their name and then copy them to a directory.

I can use a command called fetchKeys to list the files that contain say 'foo'

The command looks like this : fetchKeys -t 'foo' -F | grep .fits

This returns a list of .fits files that contain 'foo'. Great! Now I want to copy all of these to a directory /path/to/dir. There are too many files to do individually , I need to copy them all using one command.

I'm thinking something like:

fetchKeys -t 'foo' -F | grep .fits > /path/to/dir

or

cp fetchKeys -t 'foo' -F | grep .fits /path/to/dir

but of course neither of these works. Any other ideas?


Solution

  • The xargs tool can execute a command for every line what it gets from stdin. This time, we execute a cp command:

    fetchkeys -t 'foo' -F | grep .fits | xargs -P 1 -n 500 --replace='{}' cp -vfa '{}' /path/to/dir
    

    xargs is a very useful tool, although its parametrization is not really trivial. This command reads in 500 .fits files, and calls a single cp command for every group. I didn't tested it to deep, if it doesn't go, I'm waiting your comment.