How to avoid TAR of directory with no permissions?

Hi Users,

i have a situation as below;

-rw-r--r--   1 t1elkpe  cmptl          0 Mar 18 12:00 file2
----------   1 t1elkpe  cmptl          0 Mar 14 16:07 file1
d---------   3 t1elkpe  cmptl          3 Mar 18 11:23 fd

now when i am tarting the above files below is what i get as output.

[t1elkpe@ /u01/appl/t1elkpe/pst/max/SCRIPTDIR$]> tar cf test.tar f*
tar: /u01/appl/t1elkpe/pst/max/SCRIPTDIR/fd: Permission denied
tar: file1: Permission denied
[t1elkpe@ /u01/appl/t1elkpe/pst/max/SCRIPTDIR$]> tar tvf test.tar
tar: blocksize = 4
d--------- 1005/250      0 Mar 18 11:23 2013 fd/
-rw-r--r-- 1005/250      0 Mar 18 12:00 2013 file2

As visible fd directory is still tar in the final tar. Is there any way i can avoid this completely.

Any help is appreciated.

Thanks and Regards
Rajan gupta

---------- Post updated at 07:52 AM ---------- Previous update was at 06:10 AM ----------

Hi Experts,

any suggestions???

Thanks.

Depending on your OS version, there can be a -L flag on the tar command. This refers to a list file that you can populate beforehand. In you case:-

$ cat /tmp/my_tar_list
file2
$ tar -cL /tmp/my_tar_list -f /tmp/my_tar_file

It's not too common, but a really good option and it's in AIX at least. What OS are you using?

It's extremely useful for extracting lots of files from a huge tar-file, i.e.

$ tar -tf my_tar_file > /tmp/my_tar_list

...edit the list file created to keep just the ones you want, then ....

$ tar -xvL /tmp/my_tar_list -f my_tar_file

Another option might be to ignore or handle the backup error. The tar-file you create won't have it anyway, so is that all you really need?

I hope that this helps,
Robin
Liverpool/Blackburn
UK

Why not use the -X (exclude file) option to tar, or, extended globbing in bash to unselect that directory?
You will now say "I don't use bash" or "My tar doesn't have -X" to which I'll respond "You didn't mention your system or versions or implementations"

Hi Guys,

Thanks for reply. I am using solaris. And i am in process of design a shell script in bash which tar all the files mentioned as a regular expression and removes them from the location(just like recycle bin functionality) and when a particular file or directory is not able to tar completely it should ignore and tar rest of them. i have a seprate log which shows which files/dir have been tar or not tar.
Rest everything is working fine except that the when unable to tar the directory the tar file still kept the empty directory which on restoring replaces the original directory with contents which is incorrect.

If tar is not done it should not store this directory at all. How can i prevent the tar of such directories.

Hope this helps in better understanding of the issue here.

Thanks and Regards
Rajan Gupta

---------- Post updated at 09:50 AM ---------- Previous update was at 09:22 AM ----------

Another option that came in my mind is to remove the directory in tar. I know it is possible througfh exclude folder but in my case this is not working. Can anyone highlight the case how can i remove the exesting non permission empty directory from tha tar created so that i have a clean tar.

Regards
Rajan

Solaris (along with the majority of UNIX systems) has pax. You could just use find with the appropriate arguments to exclude all directories with mode 0. Since pax can read the file list on stdin, you can use a simple pipe:

find . \! \( -type d -perm 0 -prune \) | pax -w > test.tar

Regards,
Alister

Is the \

necessary before the ! ?

In case you didn't get the memo, today is International Gratuitous Backslash Quoting Day. Don't forget to celebrate. :slight_smile:

All joking aside, no; it's not necessary. I'd edit it, but it won't hurt anything. Thank you for bringing attention to it. It may have confused a novice.

However, in some circumstances an exclamation point can cause problems, e.g. in bash when history expansion is enabled (although even in this instance, if whitespace follows it, no expansion occurs).

Regards,
Alister

\T\h\a\n\k\s \! :smiley:

Hi,

Thanks for the reply. But let me put my issue clear here;

  1. When i TAR Directory with no permissions a TAR file is created containing empty directory.
  2. now during untar i can retrieve this directory but with no contents. Contenets have not been tar due to no permissions.
  3. Ideally it should have tar the directory since it had no permissions.
  4. In order to clean this TAR i need to remove tyhis directory from the same TAR file.
  5. Using PAX gives me same command error that permission denied and nothing changes.
 
find /u01/appl/t1elkpe/pst/max/SCRIPTDIR/fd/ \! \( -type d -perm 0 -prune \) | pax -w > 20130314154440.tar
find: cannot read dir /u01/appl/t1elkpe/pst/max/SCRIPTDIR/fd/: Permission denie

Can you please highlight how can i delete unwanted directories from my TAR files?

Thanks and Regards
Rajan Gupta

One wonders why you have a directory with no permissions. I suppose, if you can't see in it, then you can't back it up and you can't extract it anyway. As backups need to be able to set permissions when they are restored, this is a process for the superuser really.

If you have superuser access, then it seems to work fine:-

RBATTE1 @ /usr/home/RBATTE1/uf>sudo find . -exec ls -ld {} \;
drwxr-xr-x   3 RBATTE1  SYSSUP          256 19 Mar 09:29 .
d---------   2 RBATTE1  SYSSUP          256 19 Mar 09:31 ./dufdir
-rw-r--r--   1 RBATTE1  SYSSUP            5 19 Mar 09:31 ./dufdir/file3
-rw-r--r--   1 RBATTE1  SYSSUP            6 19 Mar 09:29 ./file1
----------   1 RBATTE1  SYSSUP            4 19 Mar 09:29 ./file2
RBATTE1 @ /usr/home/RBATTE1/uf>sudo tar -cvf ../uf.tar .     
a .
a ./dufdir
a ./dufdir/file3 1 blocks
a ./file1 1 blocks
a ./file2 1 blocks
RBATTE1 @ /usr/home/RBATTE1/uf>sudo rm -r *
RBATTE1 @ /usr/home/RBATTE1/uf>sudo find . -exec ls -ld {} \;
drwxr-xr-x   2 RBATTE1  SYSSUP          256 19 Mar 09:35 .
RBATTE1 @ /usr/home/RBATTE1/uf>sudo tar -xvf ../uf.tar 
x .
x ./dufdir
x ./dufdir/file3, 5 bytes, 1 tape blocks
x ./file1, 6 bytes, 1 tape blocks
x ./file2, 4 bytes, 1 tape blocks
RBATTE1 @ /usr/home/RBATTE1/uf>sudo find . -exec ls -ld {} \;
drwxr-xr-x   3 RBATTE1  SYSSUP          256 19 Mar 09:29 .
d---------   2 RBATTE1  SYSSUP          256 19 Mar 09:31 ./dufdir
-rw-r--r--   1 RBATTE1  SYSSUP            5 19 Mar 09:31 ./dufdir/file3
-rw-r--r--   1 RBATTE1  SYSSUP            6 19 Mar 09:29 ./file1
----------   1 RBATTE1  SYSSUP            4 19 Mar 09:29 ./file2
RBATTE1 @ /usr/home/RBATTE1/uf>

Is this an option?

Robin
Liverpool/Blackburn
Uk

Hi,

I am designing a script that worlks like recycle bin funtcionality in windows.
So if you accidently delete a directory which has no permission you would not able to do it and the tar file created should not contain any traces of this directory.

I am testing a senario. below are the cronological events i am testing for my script.

  1. i created a directory with contents. change permissions to 0.
  2. tried deleting it. TAR file contain the empty directory with the same name.
  3. Now if i try to extract the tar and lets suppose the original directory has regained it access then this empty directory replaces the directory with contents.

So i am trying to identifyu this directory and remove from the tar that is created.

Can you please help me in this.

Best Regards,
Rajan Gupta