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 {} \;
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
@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.
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.