0

I would like to copy a list of files in the form test/path/to/file/filename.xyz to their corresponding production path prod/path/to/file/filename.xyz. If the test path and the prod path are the same except for the presence of "prod" instead of "test", can I use the following command? Will this overwrite the destination file?

$ xargs -I % --arg-file=input.txt cp  /test/%  /prod/%

2 Answers 2

0

I don't quite understand your use of xargs, and particularly the meaning of %.

But about cp, yes it will overwrite the destination files. However, cp has several options to take care of this:

  • -b or --backup[=CONTROL] : make a backup of each existing destination file
  • -i or --interactive : prompt before overwrite (overrides a previous -n option)
  • -n or --no-clobber : do not overwrite an existing file
  • -u or --update : copy only when the SOURCE file is newer than the destination file or when the destination file is missing

See man cp for more.

As for your ultimate goal, I think that what you mean is to copy some files under "test/" listed in your input.txt file, to the same path under "prod/". For that, it seems rsync might be better suited. It has tons of options. A few of those which may be relevant :

-b, --backup            make backups (see --suffix & --backup-dir)
--backup-dir=DIR        make backups into hierarchy based in DIR
--suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)
--files-from=FILE       read list of source-file names from FILE
-R, --relative`         use relative path names
-u, --update            skip files that are newer on the receiver
-n, --dry-run           perform a trial run with no changes made
[etc]
0

Using --files-from= option with rsync allows you to specify the exact list of files to transfer:

$ rsync -av --ignore-existing \
  --files-from=input.txt test/ prod/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .