How to tar this dir excluding some files .au?

Hi all,

Thanks for previous help.
How to include this in script,
I need to tar files which are present in /var/spool/cron/crontabs directory (used for crontab) excluding those files which are having extension .au

/var/spool/cron/crontabs>>ls -ltr | grep -v .au
total 438
-rw-------   1 root     root          16 Oct 19  2008 batch010
-rw-------   1 root     root          58 Oct 19  2008 batch451
-rw-------   1 root     root         752 Oct 19  2008 lp
-rw-------   1 root     root         287 Oct 19  2008 sys
-rw-r--r--   1 root     root           1 Feb 11  2009 root
-rw-------   1 root     qad         2217 Apr  2  2010 batch471
-rw-------   1 root     qad          462 Apr 18  2010 ibi

Please advice,
I guess this might work: -

find /var/spool/cron/crontabs -grep -v .au -exec tar cvf {} \;

But I need to put this in the script:-

find /var/spool/cron/crontabs -not -name "*.au" -exec tar cvf myArchive.tar {} \;
1 Like

Had you a recent bash, you could use extended globbing:

$ shopt -s extglob
$ tar cf myArch.tar  /var/spool/cron/crontabs/!(*.au)
1 Like

I don't think this will work at all, because the "-exec" clause of find executes the command for every file found by "find" separately. This means, the first file found will execute

tar cvf myarchive.tar 1st.file

and the second file found will trigger execution of

tar cvf myarchive.tar 2nd.file

and so on. Because of the "c"-option in tar every time a new file is found it will create "myarchive.tar" anew, overwriting the previous one. To go with this method one would have to create the tar archive previously and then add to it:

tar cvf myArchive.tar
find /var/spool/cron/crontabs -not -name "*.au" -exec tar Af myArchive.tar {} \;

But it would probably be easier to feed "tar" the list of file names via <stdin>:

find /var/spool/cron/crontabs -not -name "*.au" -print | tar -cf myArchive.tar 

I hope this helps.

bakunin

1 Like

Thanks Guys for reply,

I will soon post what I did to get this done.

As your on redhat you probably have GNU tar so you could use the --exclude option:

tar cvf myArchive.tar --exclude='*.au' /var/spool/cron/crontabs

I don't think any tar implementations read filenames from stdin. Perhaps you're thinking of the wonderful pax utility.

Regards,
Alister

1 Like

Well I use it almost that way when I transfer filesystems, directories.. to a remote destination e.g.

cd sourcedir; 
tar -cf -  * | remsh dest_node "(cd destdir; tar -xvf - )" 

But locally, for same usage I privilege cpio ....

@vbe: I stumbled over exactly the same stone, as I did transfers a lot in the way you mention. BUT - alister said "tar doesn't read FILENAMES from stdin"; this is different to "tar reading archivable contents from stdin", and I believe he's right.

2 Likes

Hi all,
I used this

cd /var/spool/cron/crontabs
sudo tar cvf /users/mahesh/all_cron.tar `ls -ltr * |grep -v .au |awk '{print $9}'`

This is a Sun Solaris 10 machine.

Ah yes, I misunderstood... Thanks for noticing...

That solution is incorrect. Your grep invocation can exclude files which should not be excluded. While the regular expression .au will correctly exclude files ending with ".au", it will also exclude a file which contains any character followed by "au" at any point in the file name, such as myaudio.sh .

The correct approach requires backslash escaping the dot so that it is taken literally, and anchoring the regular expression to the end of the line:

grep -v '\.au$'

Not of much importance, but there's no need to sort ls' output; -t and -r can be omitted. Further, since only the file name is of interest, no need for the long listing format, so -l can be dropped as well. Dropping the long listing format obviates the need for AWK, reducing your approach to:

ls | grep -v '\.au$'

However, I wouldn't use this command substitution approach. For years now, Solaris (and nearly every UNIX-like system, except perhaps for a few linux distributions) has shippied with pax. I'd just use that.

cd /var/spool/cron/crontabs && pax -ws '/.*\.au$//' . > /users/mahesh/all_cron.tar

As added bonuses, now neither a large number of files nor files with whitespace will break the archiving attempt.

Regards,
Alister