I needed to copy the contents of one directory belonging to a user from one
Linux system to another. While logged into her account on the source system, I
created a tar file, aka a "tarball", of the directory with the command
tar -cvf game.tar game
to copy the contents of her "game"
directory and all of its subdirectories to the tar file. The tar file was about
20 MB in size, so I compressed it with the
gzip
command gzip game.tar
resulting in a game.tar.gz file about 5 MB
in size, which I transferred to the destination system. While logged
into her account on the destination system, I uncompressed the .gz file
with gunzip game.tar.gz
and then attempted to extract the
contents of the tar file into the same directory on the destination system as
on the source system. The directory already existed on the destination server
because I had many months ago copied everything in her home directory from the
source to the destination system. When I ran the command tar -xvf
game.tar
to extract the contents of the tar file, I saw files
extracted, but I also saw several "Cannot open: File exists" lines
in the output from the command, which terminated prematurely with the
following lines:
game/Update tar: game/Update: Cannot open: File exists game/FAQ tar: game/FAQ: Cannot open: File exists game/CONVERT.22 tar: game/CONVERT.22: Cannot open: File exists game/BETA tar: game/BETA: Cannot open: File exists tar: game: Cannot utime: Operation not permitted tar: Exiting with failure status due to previous errors
I thought I could just add the --overwrite
option to the tar
command, so then tried tar -xvf game.tar --overwrite
. I saw
the following line in the output:
tar: game/src/gdbm-1.8.0/gdbm.h: Cannot open: Permission denied
I didn't see the "Cannot open: File exits" messages that I had previously seen, but the command still terminated prematurely with the following lines at the end of the output:
game/Update game/FAQ game/CONVERT.22 game/BETA tar: game: Cannot utime: Operation not permitted tar: Exiting with failure status due to previous errors $
When I checked the ownership of the BETA file, I saw it belonged to the user, but I saw that there was another file in the same directory as the BETA file that belonged to the root account, which probably was the cause of the premature termination of the restoral of the files from the tar archive.
$ ls -l BETA -rw-r--r-- 1 ann 501 58618 Jun 21 2004 BETA $ ls -l | grep -v ann total 1172 drwxr-xr-x 2 root root 4096 Oct 4 2014 media $
Since there was no reason why the root account needed to retain ownership of that file, I changed the ownership and group for the file to the user's account. I had to do that from the root account.
# chown ann media; chgrp ann media #
I found that the game directory itself was owned by root, so I changed the ownership and group for it as well.
# chown ann game; chgrp ann game #
I then made the working directory the game directory again and checked to see if there were any other files owned by root within it or any of its subdirectories, but there were no others.
# cd game # find . -user root #
I then logged off the root account and went back to the user's account
and retried extracting files from the tar file again with
tar -xvf game.tar --overwrite
. Again I saw the message below:
tar: game/src/gdbm-1.8.0/gdbm.h: Cannot open: Permission denied
And the command terminated with the following lines:
game/Update game/FAQ game/CONVERT.TINY22 game/BETA tar: game: Cannot utime: Operation not permitted tar: Exiting with failure status due to previous errors $
I checked the ownership and permissions on the gdbm.h
file and
found it was owned by the user's account, but her account had only read
permission to the file. So I granted write permission for the user's account
to the file.
$ ls -l game/src/gdbm-1.8.0/gdbm.h -r--r--r-- 1 ann ann 4744 Apr 11 2008 game/src/gdbm-1.8.0/gdbm.h $ chmod u+w game/src/gdbm-1.8.0/gdbm.h $ ls -l game/src/gdbm-1.8.0/gdbm.h -rw-r--r-- 1 ann ann 4744 Apr 11 2008 game/src/gdbm-1.8.0/gdbm.h $
I also noticed that, though the user account number was the same between the source and destination systems, the group number differed being 501 on the source system and 1001 on the destination system.
$ cat /etc/passwd | grep ann ann:x:501:1001:Ann:/home/ann:/bin/bash $ grep ann /etc/group ann:x:1001: $
So I changed the group for the directory and all its files and subdirectories to be the appropriate one for the destination system.
$ chgrp --recursive ann game $
This time when I extracted files from the tar file, there were no errors and the command completed successfully.
$ tar -xvf game.tar --overwrite game/ game/src/ <text snipped> game/Update game/FAQ game/CONVERT.TINY22 game/BETA $
And the ownership and group of files remained what they should be on the new system.