Author Topic: Linux / Unix systemsadministration: copy /overwrite multiple existing files  (Read 1204 times)

0 Members and 1 Guest are viewing this topic.

Offline hostmaster

  • Administrator
  • Newbie
  • *****
  • Posts: 20
  • Karma: +1/-0
    • View Profile
Problem: you have a number of files in a directory, now you want to overwrite all of them with one single source file.
Since the command cp (copy) only copies to one file at a time you need to invoke cp multiple times and actually do a copy (overwrite) several times, once per existing file.

In bash you can overwrite multiple (existing) files in a directory. This works in most Linux distributions and should work in most BSD's as well:

# ls | xargs -n 1 cp <source file>

what the command does is actually:

1. ls lists all files in the directory and pipes it to xargs
2. xargs runs once and copies the source file in order to overwrite the original fil.

Result: you have the same files names existing in your directory but now each file contains the contents from the source file.