Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2013
Registered User
 
Join Date: Jul 2011
Posts: 89
Thanks: 45
Thanked 1 Time in 1 Post
RedHat 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

Code:
/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: -

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

But I need to put this in the script:-

Last edited by Scrutinizer; 02-04-2013 at 05:37 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-04-2013
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts

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

The Following User Says Thank You to balajesuri For This Useful Post:
manalisharmabe (02-04-2013)
Sponsored Links
    #3  
Old 02-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 1,881
Thanks: 25
Thanked 434 Times in 420 Posts
Had you a recent bash, you could use extended globbing:
Code:
$ shopt -s extglob
$ tar cf myArch.tar  /var/spool/cron/crontabs/!(*.au)

The Following User Says Thank You to RudiC For This Useful Post:
manalisharmabe (02-04-2013)
    #4  
Old 02-04-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,291
Thanks: 27
Thanked 450 Times in 351 Posts
Quote:
Originally Posted by balajesuri View Post
Code:
find /var/spool/cron/crontabs -not -name "*.au" -exec tar cvf myArchive.tar {} \;

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


Code:
tar cvf myarchive.tar 1st.file

and the second file found will trigger execution of


Code:
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:


Code:
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>:


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

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
manalisharmabe (02-04-2013)
Sponsored Links
    #5  
Old 02-04-2013
Registered User
 
Join Date: Jul 2011
Posts: 89
Thanks: 45
Thanked 1 Time in 1 Post
Thanks Guys for reply,

I will soon post what I did to get this done.
Sponsored Links
    #6  
Old 02-05-2013
Chubler_XL's Avatar
Registered User
 
Join Date: Oct 2010
Posts: 2,100
Thanks: 64
Thanked 608 Times in 586 Posts
As your on redhat you probably have GNU tar so you could use the --exclude option:


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

Sponsored Links
    #7  
Old 02-05-2013
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,599
Thanks: 122
Thanked 716 Times in 599 Posts
Quote:
Originally Posted by bakunin View Post
But it would probably be easier to feed "tar" the list of file names via <stdin>:


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

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

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
vbe (02-06-2013)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List files older that 7 days in a dir, excluding all subdirs jwbijl UNIX for Dummies Questions & Answers 6 06-04-2012 09:38 AM
Excluding file from tar chriss_58 Shell Programming and Scripting 3 11-17-2010 07:28 AM
Excluding a file from tar... lwif UNIX for Advanced & Expert Users 2 05-11-2010 09:23 AM
help writing rm script excluding specific titled dir nomados Shell Programming and Scripting 1 01-15-2010 07:30 PM
Excluding files using tar cXzf sampipe UNIX for Dummies Questions & Answers 11 07-28-2005 09:46 AM



All times are GMT -4. The time now is 04:50 AM.