44

How can I copy files/folders from an ubuntu computer on a ext4 filesystem to another ubuntu computer which also is on an ext4 filesystem, using a usb stick which uses a vfat filesystem without losing file permissions?

I've tried the basic ctrl-c from source computer, then ctrl-v to usb, then ctrl-c from usb then ctrl-v to target computer, and none of the file/folder permissions stay intact.

Nathan Osman
  • 32,155
oshirowanen
  • 3,977

3 Answers3

62

You can make a tar archive of the source, copy that to the other computer using the USB drive, and extract it there. Tar preserves file permissions.

1 - On the source computer:

cd /path/to/folder/to/copy
tar cvpzf put_your_name_here.tar.gz .

2 - Copy put_your_name_here.tar.gz to the USB drive and then to the other computer

3 - On the destination computer:

cd /path/to/destination/folder
tar xpvzf put_your_name_here.tar.gz

tar will recreate the archived folder structure with all permissions intact.

Those commands will archive the contents of the source folder and then extract them into the destination folder. If you want to copy the folder itself, then you should, at step 1:

cd /path/to/parent/folder
tar cvpzf put_your_name_here.tar.gz folder_to_copy

The same mechanism could be used for single files.


If you can connect from one computer to the other using ssh, @siddharthart answer (rsync) might be more practical.

Marco Ceppi
  • 48,101
sierrasdetandil
  • 2,671
  • 1
  • 25
  • 25
  • Don't forget the -p flag to tar to ensure permissions are preserved. – haziz Dec 05 '12 at 23:47
  • @haziz Actually, the -p isn't really needed, see my comment to @mikewhatever's answer. The -z also isn't necessary when untarring, as tar is able to figure out the type of compression itself, unless it's an old version. Including the flags in the command doesn't hurt, tough. – sierrasdetandil Dec 06 '12 at 00:54
  • 4
    I don't think you can count on this behavior. BTW quoting from the man tar page "-p, --preserve-permissions, --same-permissions extract information about file permissions (default for superuser)" this appears to be the default behavior only if tar is ivoked as superuser. Would not count on this as default behavior. I would also always explicitly spell out the -z flag when compressing or decompressing (or -j if using bzip2). – haziz Dec 06 '12 at 01:07
  • Could (basic) encryption be easily added? (I guess there'd be many ways of doing it, but I'm wondering, but if done via the command line, how would the password / encryption key/passphrase be supplied? Like so ******** or like the sudo-password, where no key-strokes are made visible?) – nutty about natty Apr 29 '13 at 05:58
  • tar suffers from a very serious problem - it chokes on paths longer than 100 characters. – Nathan Osman Nov 10 '15 at 04:30
  • @nuttyaboutnatty just use gnupg to encrypt it to yourself ;-). – binki Oct 19 '16 at 02:19
  • @NathanOsman GNU tar does not have any limit on path length AFAIK – Timothy Zorn Jul 25 '17 at 05:49
  • You will likely need superuser privileges to preserve ownership as well. – kojow7 Oct 16 '18 at 23:17
13

You could try rsync with -a flag to maintain all permissions while copying. I'm not aware of a simpler solution, but I had used it for a purpose in the past.

Rsync gives brilliant support to repeated copying, updating folders etc. while remaining blazingly fast.

SiddharthaRT
  • 6,066
  • 6
    I do like rsync (love it, in fact :-]), but the ext4 file permissions will be lost if the destination device is vfat formatted, like oshirowanen described. The tar solution is better in this case. – yuric Jan 31 '13 at 14:17
6

I think taring and then untaring should work on both files and directories.

to tar:

tar cvpfz /target.tar.gz /source/

to untar:

tar xvpfz /source/

The p flag stands for --preserve-permissions.

You should see man tar for more info.

mikewhatever
  • 32,638
  • I just did a quick test to be sure and -p is not needed -- tar preserves permissions by default, at least when the user owns the files being archived/extracted. – sierrasdetandil Dec 05 '12 at 21:05
  • 2
    True, but you've not told us what files you wanted to copy, and I had no reason to assume anything on my own. – mikewhatever Dec 05 '12 at 21:38