Excluding a list of files from tar with spaces in filenames
Meir Kriheli
meir at mksoft.co.il
Fri Feb 5 12:25:50 IST 2010
On 02/04/2010 11:11 PM, Dotan Cohen wrote:
> On 4 February 2010 22:59, Meir Kriheli<meir at mksoft.co.il> wrote:
>> On 02/04/2010 09:12 PM, Dotan Cohen wrote:
>>>
>>> I hope there is a bash guru out there today. I need to script a backup
>>> plan which has a list of directories that should not be backed up.
>>> Some of these directories have spaces in their filenames. The
>>> following script works so long as there are no directories with spaces
>>> in DONT_COPY, however, I cannot figure out what to do to get it to
>>> work _with_ spaces in filenames:
>>>
>>> #!/bin/bash
>>>
>>> DONT_COPY="
>>> .adobe/
>>> File with Spaces
>>> .bin/google-earth
>>> Yet Another Annoying Filename
>>> "
>>>
>>> DONT_COPY_LIST=""
>>> for pattern in $DONT_COPY ; do DONT_COPY_LIST="$DONT_COPY_LIST
>>> --exclude=$pattern "; done
>>> tar -zcvf - * $DONT_COPY_LIST
>>>
>>>
>>> Any ideas? Thanks!
>>>
>>
>> Set IFS to newlines only:
>>
>> IFS=$'\n'
>>
>> BTW, why don't you use -X or --exclude-from ? From man:
>>
>> -X, --exclude-from=FILE
>> exclude files matching patterns listed in FILE
>>
>
> Thanks, Meir, I just got this to work:
>
> EXCLUDES=`tempfile`
> cat>$EXCLUDES<<EOLIST
> $DONT_COPY
> EOLIST
>
> tar -zcvf - * --exclude-from $EXCLUDES> out.tar
>
> rm -f $EXCLUDES
>
>
>
> By the way, because of the "v" flag, tar outputs all the directories
> that it copies to the terminal (even though this is running in a
> script). How can I capture that output to a variable or to another
> temp file?
>
>
You can use the tee command for that.
Cheers
--
Meir
More information about the Linux-il
mailing list