2

We got a filesystem: /foo

And we got another filesystem: /bar

Q: How can I copy all the files from /foo to /bar?

We don't have space to compress them, only to copy them. But there could be symlinks, hardlinks in the filesystem. So need to preserve these and the owner, permission, structure, hidden files! What is the best way?

extra: files with strange characters or block/character files too

UPDATE: can we use gzip with this? if need to copy via network, compression would be very-very useful.

pepite
  • 1,053
  • 3
  • 14
  • 32

2 Answers2

2

Rui's comment is nearly enough; this should copy everything, including hidden files:

cd /foo; tar cpf - . | (cd /bar; tar xpvf -)

(run as root).

If you want to compress the data that's copied between the two tar processes, you can add a compressor and decompressor in the pipeline:

cd /foo; tar cpf - . | gzip -9 | (cd /bar; gunzip | tar xpvf -)

If your version of tar supports it, you can use the z flag instead:

cd /foo; tar cpzf - . | (cd /bar; tar xpzvf -)

(The -p flag is enabled by default for root on some platforms, I'm not sure about AIX.)

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • The default shell on AIX is rsh, I am not sure it will support commands grouped in brackets – MAQ Oct 29 '16 at 13:43
  • That may be a local setup, @KWubbufetowicz; [link for AIX 6.1](http://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.osdevice/avail_shells.htm) says "The Korn shell (/usr/bin/ksh) is set up as the default shell" – Jeff Schaller Oct 30 '16 at 01:07
  • Great answer! Many thanks. only one addition: can we put gzip in it too? updated question. AIX has ksh for default shell. – pepite Oct 30 '16 at 10:55
  • @pepite I've added compressing variants (you can substitute other compressors instead of `gzip`/`gunzip` in the second set of commands above, as long as the chosen tool can compress and uncompress from its standard input to its standard output). – Stephen Kitt Oct 30 '16 at 11:25
  • Can you please extend this answer with an example for SCP between two machines? Thanks!! – pepite Dec 06 '16 at 10:34
  • @pepite that's not how it works — you should [ask a new question](http://unix.stackexchange.com/questions/ask). (You might want to [take the tour](http://unix.stackexchange.com/tour) too!) – Stephen Kitt Dec 06 '16 at 15:01
0

Tar - while very versatile - does not know about many underlying issues related to inode data. I highly recommend a variation of

cd $src; find . | backup -if - | (cd $target; restore -xqf -)

Years ago I worked this into a 'pretty script' - but mainly I just use the above on the command line.

For pretty script (from 2009! eeks!) see: http://www.rootvg.net/content/view/301/309/

Michael Felt
  • 1,198
  • 2
  • 12
  • 21