Sunday, October 5, 2008

Create 'tar' file from a list

Creating a 'tar' file is simple, but creating a 'tar' file from some files have different paths/directories is another case.

Example, when I want to create a tar file from path /home/eshabe/mydocs, I can use command: tar cvf mydoc.tar /home/eshabe/mydocs

But when I want to create a tar file from files:
/home/eshabe/mydocs/howto-create-tar-file-from-a-list
/etc/apache2/site-enables/000-default
/home/eshabe/mydocs/yes/i/am
/home/eshabe/.gnome2
...
?

I can create a tar file from command line: tar cvf mydoc.tar /home/eshabe/...

Another way is create a shell (sh) script to read a list file then create a tar file:

#!/bin/sh
# Create a 'tar' file from a list


n_program=`basename $0`
v_program=1.0.0d1
p_program=eshabe

t_program='2003.08.12'


RUNNAME='./shb-tarlist-run'

info_program ()
{
cat <<>
$n_program v$v_program
by: $p_program - $t_program
----------------------------------------------------------------------------
Function : create a tar file from a list file
Command : $nama_program LIST_FILE FILE.tar

LIST_FILE = a file contains list of files or directories
FILE.tar = result

pesan_program

}

if [ $# != 2 ]
then
info_program
exit
fi

printf "tar cvf $2 " > $RUNNAME
awk '{
printf "%s ", $0
}' $1 >> $RUNNAME

chmod a+x $RUNNAME
$RUNNAME
rm $RUNNAME

echo Done.

No comments: