how to keep file ownership while using tar
Supply the option --same-owner
to tar while extracting.
tar --help
tells:
--same-owner
create extracted files with the same ownership
After tar extract, Changing Permissions
If you want to keep the permissions on files then you have to add the -p (or --preserve-permissions or --same-permissions) switch when extracting the tarball. From the tar man pages :
--preserve-permissions
--same-permissions
-p
When `tar' is extracting an archive, it normally subtracts the
users' umask from the permissions specified in the archive and
uses that number as the permissions to create the destination
file. Specifying this option instructs `tar' that it should use
the permissions directly from the archive.
So PHP code should be :
exec("tar -xzfp foo.tar.gz");
How to extract correct owner permission by tar?
Separate the archive into two steps, the first step archive as previous
tar cf a.tar a/b
The second step to append the directory without recursion
tar -rf a.tar --no-recursion a
Permission with tar command
why there is en empty folder in a config001.tgz file?
There is the "root folder" included in the tar archive. The folder the tar was in. The privileges, owner and group, permissions and creation/modification (or one of them, I am not sure) dates are included. You can create such archive with:
mkdir -p /tmp/a
cd /tmp/a
echo 123 > 1
echo 234 > 2
tar cfvp /tmp/test.tar .
# and inspect with:
tar -tvf ./test.tar
drwxr-xr-x kamil/kamil 0 2019-07-15 12:50 ./
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./2
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./1
By specifing the .
, ie. the current directory, the information about the current directory itself will be included in he tar
. Ie. the information about the owner and group, permissions and dates.
if I don't append --no-overwrite-dir after command,it will raise error, what permission does "tar" want to change?
tar
wants to change the permissions of the directory you are inside. The hpc_dir
directory. The hpc_dir
is owned by root
, so tar
can't change/touch it.
why --no-overwrite-dir option can fix the problem?
Because then tar
sees the the currect directory hpc_dir
exists. Because of that, tar
doesn't try to create the directory, nor tries to change the owner and group permissions of the directory, nor tries to restore the creation date of the directory.
You could just go with mkdir somedir; tar xzfv archive.tar -C somedir
- that way the somedir
will be created by current user, so tar
will be able to change it's properties.
Or you could just change the owner of hpc_dir
directory, letting your user modify it.
-C option in tar changes parent directory permission
The command
tar -cf a/b/c/tarfile.tar -C a/b/c .
has an entry for .
(the directory which you are tarring). When you extract the tar, that directory's permissions apply. If you do not want this behavior, you can either
- specify the names (other than just ".") that you put into the tar file, e.g.,
tar -cf a/b/c/tarfile.tar -C a/b/c foo
- specify the names of the items you want to extract. You can see what is available using
tar tvf
using your tarfile as the parameter.
tarfile and user,group information problem
As guettli says, you have to be root to be able to change the ownership of a file to somebody else. Otherwise, you open a huge security hole. This is true when using the tar(1) program or when trying to use the tarfile package from python.
Note, though, that some earlier version of Python have a bug (see issue in comments below) that means files extracted by root are owned by root instead of the real owner (user and group).
Setting file permissions on contents of tarfile
With GNU tar, use the --mode
option when creating the archive, e.g.:
tar cf archive.tar --mode='a+rwX' *
But note that when the archive is extracted, the umask will be applied by default. So unless the user's umask is 000, then the permissions will be updated at that point. However, the umask can be ignored by using the -p
(--preserve
) option, e.g.:
tar xfp archive.tar
Related Topics
Cross-Compilation to X86_64-Unknown-Linux-Gnu Fails on MAC Osx
Why Does Wget Output to Stderr Rather Than Stdout
Linux-Based Solution for Domain Management
Restart Process on File Change in Linux
What Server-Side Web Technology Should I Use in an Embedded System
How to Find All Image Tags of a Running Docker Container
Use Grep to Remove Words from Dictionary Whose Roots Are Already Present
Docker Installation on Linux Mint 19.2 Doesn't Work
Updating Shiny Server Config to Change Timeout Error
Stop a Running Dotnet Core Website Running on Kestrel
Difference Between Different Ways of Running Shell Script
Gnu Octave, Round a Number to Units Precision
Qemu Hosting Mte Enabled Kernel Does Not Raise Fault
Differences Between Sponge and Tee
How Existing Kernel Driver Should Be Initialized as Pci Memory-Mapped