Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On the Dev server, we have a tree of files all owned by either the "webdev" or "webserv" user.

I would like to tar up this file tree and extract it on the prod server, with the change that any files owned by "webdev" would now be owned by "webprod", and any owned by webserv would remain owned by webserv.

Is there any way to do this kind of "swapping"?

share|improve this question
If webprod is the user id used by the webserver, you may not want to change the owner. Ensuring the are group readable by the webserver may be a better option. – BillThor Mar 26 '11 at 6:58
up vote 4 down vote accepted

You can accomplish this most efficiently using find after untarring your archive:

# find /path -user webdev -exec chown webprod '{}' +
share|improve this answer
Am I right that they should be extracted with the --same-owner flag to make this next step possible? Otherwise, they all have owner of webprod. – feedbackloop Mar 26 '11 at 18:30
--same-owner is the default if you're root (and if you want to extract files as anyone other than the userid you're logged in as, you have to do it as root) – MikeyB Mar 31 '11 at 13:44
thanks! ............. – feedbackloop Apr 1 '11 at 21:08

If webprod runs the tar command, the files will be owned by webprod. The files in the tar file may need to be readable by webprod.

In most cases this webprod should not be the user id the web server runs as. This will limit the ability to modify your content by compromising the web server.

share|improve this answer

I think you might be able to do something clever with the --to-command option in recent versions of gnu tar (around 1.25) to deal with this. That should allow you to do arbitrary transforms of tarfile output by piping it through a custom filter program. Theoretically that could check the owner of each file being extracted from the archive and change it if necessary.

However there's no good examples of how to use this functionality out on the net right now and my attempts at a filter script proved fruitless.

Then it struck me that you could solve this dilemma by splitting the archive into two separate files. This could be done with tar, but this is really something best handled by cpio, because cpio has more flexible input handling.

So, here's my attempt at a solution using find and cpio. First, make a cpio archive of all the files owned by webserv:

cd /var/www && find . -owner webserv | cpio -ov --format=crc | gzcat webserv-files.cpio.gz

then, make a cpio archive of the files owned by webdev, changing the owner to webprod:

cd /var/www && find . -owner webdev | cpio -ov --format crc --owner webprod | gzcat webdev-files.cpio.gz

(note that will only work if run as root according to the cpio manpage).

Now at your destination, extract both archives:

cd /var/www
gzcat webserv-files.cpio.gz | cpio -iv
gzcat webdev-files.gpio.gz | cpio -iv

I think that will have the desired result, although I have not tested too thoroughly.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.