Authorization in Linux can be very fine-grained, a feature that admins take advantage of to keep the non-admins from making a mess of things. This is generally a good thing, though it can occasionally be frustrating. One such occasion is when newbies need to tar up a folder they have permissions on but do not have permissions to create files in that folder’s parent. For example, as a developer role on a machine I have ownership of myapp that is inside apps, which is owned by root. This would look something like:
[scott@mylinuxbox /]$ ls -l drwxr-xr-x. 7 root root 4096 Aug 11 16:13 apps [scott@mylinuxbox /]$ cd apps [scott@mylinuxbox apps]$ ls -l drwxr-xr-x. 7 devs devs 4096 Jan 9 14:07 myapp
The tar command creates a tar file from where it is run. If I wanted to create myapp.tar.gz I would normally run tar czf myapp.tar.gz myapp from inside the /apps path. But with no create permissions in that folder, I just get a snarky response from Linux.
[scott@mylinuxbox apps]$ tar -czf myapp.tar.gz myapp tar (child): myapp.tar.gz: Cannot open: Permission denied tar (child): Error is not recoverable: exiting now
Skipping the details of head banging on desk and key banging on Google, I found the following approach that does the trick.
From a path you have write permissions to (almost always your home directory if no where else), run
tar -czf [TARFILENAME].tar.gz -C [PARENT_DIR]/ [DIR_TO_TAR]
For example:
tar czf folder.tar.gz -C /var/www/ folder
The ‘-C’ tells tar to start from the path following rather than where you are at. So the following steps:
[scott@mylinuxbox apps]$ cd ~ [scott@mylinuxbox ~]$ tar czf myapp.tar.gz -C /apps/ myapp [scott@mylinuxbox ~]$ ls -l *.gz -rw-rw-r--. 1 devs devs 675453427 Jan 9 14:33 myapp.tar.gz
creates myapp.tar.gz in my home directory.
Problem solved. Of course, I only made this tar file because there were problems with the app, so there is still the problem of debugging, but I think we can both do that already.
Cheers
© Scott S. Nelson