Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tarring files from a list that was created with the find command

Status
Not open for further replies.

nwo4life

Technical User
May 16, 2002
23
US
I creat a backlist with the files that I need to archive.
Then I want to tar those files but not sure how. Any suggestions?

for FILE in `find /dir/test/* -mtime +90`
do
echo $FILE >> backlist
continue;
done
for FILE in `find /dir/testb/* -mtime +90`
do
echo $FILE >> backlist
continue;
done
 
Something like:
Code:
timestamp=`date +%d-%b-%Y`
tar cvf backlist.$timestamp.tar `cat backlist`
Cheers, Neil
 
nwo4life ...
find and tar are an awkward mixture!

Basically, this is what you want:

find /dir/test/* -mtime +90 -print > backlist
find /dir/testb/* -mtime +90 -print >>backlist

tar cvf backlist.$(date +%d-%b-%Y).tar -I backlist

HOWEVER - you will have trouble! find will write-out an entry for all qualifying files AND every directory. tar will archive every discrete file in the backlist file AND every directory. If there are nested directories then tar WILL ARCHIVE THE SAME FILE MULTIPLE TIMES! Your tar archive file could be HUGE.

One way to stop this from happening - so each qualifying file will only be written to the tar archive once is like this:

#!/usr/bin/ksh
typeset -i Bytes TotalBytes RecordCount
find /dir/test/* -mtime +90 -print > backlist.1
find /dir/testb/* -mtime +90 -print >>backlist.1
>backlist
for ff in $(ls backlist.1)
do
if [[ -f $ff ]]
then
Bytes=$(ls -l $ff|awk '{print $5}') #
(( TotalBytes=TotalBytes+Bytes )) #
(( RecordCount=RecordCount+1 )) #
echo "$ff">>backlist
fi
done
tar cvf backlist.$(date +%d-%b-%Y).tar -I backlist

The for-loop will filter-out all directories and leave you with only the file entries. The three lines with # at the end are optional - they will give you a file count and total byte count.
 
You could also just add a [tt]-type f[/tt] to the find command and it won't include the directories. You can also put more than one directory in the find command. So it would look like...
Code:
TS=`date +%d-%b-%Y`
tar cvf backup.${TS}.tar `find /dir/test /dir/testb -type f -mtime +90 -print`
Hope this helps.

 
Yes, use the -type f option witin the find cmd to eliminate directories. Also, if you know of things you want to exclude, add an egrep cmd with the -v option to exclude matches. Also you can look at the beginning of txt with the "^" option or at the end with "$" within the egrep cmd.
EX:
'find /dir/test -type f | egrep -v (somefile|log$)'

This will exclude any file called exactly "somefile" and also any file that ends in "log". The "|" is a pipe before the egrep; the "|" after the -v represents an or.
Hope this helps ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top