Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting file permissions on contents of tarfile

Tags:

unix

tar

Is there a way to chmod 777 the contents of a tarfile upon creation (or shortly thereafter) before distributing? The write permissions of the directory that's being tar'd is unknown at the time of tar'ing (but typically 555). I would like the unrolled dir to be world writable without the users who are unrolling the tar to have to remember to chmod -R 777 <untarred dir> before proceeding.

The clumsy way would be to make a copy of the directory, and then chmod -R 777 <copydir> but I was wondering if there was a better solution.

I'm on a Solaris 10 machine.

BACKGROUND:

The root directory is in our ClearCase vob with specific file permissions, recursively. A tarfile is created and distributed to multiple "customers" within our org. Most only need the read/execute permissions (and specifically DON'T want them writable), but one group in particular needs their copy to be recursively writable since they may edit these files, or even restore back to a "fresh" copy (i.e., in their original state as I gave them).

This group is somewhat technically challenged. Even though they have instructions on the "how-to's" of the tarfile, they always seem to forget (or get wrong) the setting of the files to be recursively writable once untarred. This leads to phone calls to me to diagnose a variety of problems where the root cause is that they forgot to do (or did incorrectly) the chmod'ing of the unrolled directory.

And before you ask, yes, I wrote them a script to untar/chmod (specific just for them), but... oh never mind.

So, I figured I'd create a separate, recursively-writable version of the tar to distribute just to them. As I said originally, I could always create a copy of the dir, make the copy recursively writable and then tar up the copy dir, but the dir is fairly large, and disk space is sometimes near full (it can vary greatly), so making a copy of the dir will not be feasable 100% of the time.

like image 420
splungebob Avatar asked Aug 08 '13 15:08

splungebob


People also ask

How do I set permissions to all files in a directory?

To change directory permissions for everyone, use “u” for users, “g” for group, “o” for others, and “ugo” or “a” (for all). chmod ugo+rwx foldername to give read, write, and execute to everyone. chmod a=r foldername to give only read permission for everyone.

How do I give permission to a specific file?

To change the file permissions using chmod, run chmod <permission> <directory or filename> , swapping in the desired file permissions and the directory or file. The owner can change file permissions for any user, group or others by adding - to remove or + to add certain permissions.

How do you preserve permissions on a tar backup?

By default, tar will preserve file permissions and ownership when creating the archive. To extract file permissions and ownership, you will need to run tar as root when extracting, since changing file ownership usually requires superuser privileges.

How do I give permission to a file for a specific group in Linux?

To change file and directory permissions, use the command chmod (change mode). The owner of a file can change the permissions for user ( u ), group ( g ), or others ( o ) by adding ( + ) or subtracting ( - ) the read, write, and execute permissions.


1 Answers

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
like image 199
Todd Owen Avatar answered Sep 20 '22 01:09

Todd Owen