[SOLVED] bash: script to tar up a bunch of sub-dir separately
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash: script to tar up a bunch of sub-dir separately
first time trying this in a script, and in succession, so I am lacking in understanding how tar actually works in that situation.
my script
Code:
#!/bin/bash
#set -xv
#read -p "Enter Working Dir path. " path
path="/run/media/userx/1TB/Adam & The Ants"
cd "$path"
echo $(pwd)
#get all sub dir in parent dir to tar up separately
mapfile -t tars < <(find . -type d)
to="${path%/*}/JustCreated-Tars-Gzs"
mkdir -pv "$to"
for i in ${tars[@]} ;
do
#remove leading ./
targz="${i#*/}".tar.gz
tardir="${i#*/}"
tar -zcvf "$targz" "$tardir"
mv -v "$targz" "$to"
echo "NEXT"
echo;echo;echo;echo
unset targz
unset tardir
exit
done
it does not even tar one sub dir then exit. It just keeps going tarting all of them into one, and then changes its name.
part of the output as it is a too much to post.
Code:
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/8. A.N.T.S. [Unreleased].mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/21. Friend Or Foe [Single].mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/14. Friend Or Foe (Alt. Version).mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/4. Killer In The Home.mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/20. Man Called Marco.mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/19. Desperate But Not Serious [Single].mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/6. Magnificent Five (Alternate Mix).mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/18. Zydyco [Unreleased].mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/CD2/13. Stand And Deliver [Single].mp3
./2001 Adam & The Ants - AntBox [The Definitive Story]/Adam & The Ants - AntBox [The Definitive Story] Back.jpg
tar: .: file changed as we read it
renamed '..tar.gz' -> '/run/media/userx/1TB/JustCreated-Tars-Gzs/..tar.gz'
then the last it changes the name from what is suppose to be the sub dir name to that ..tar.gz When it is suppose to be each sub dir in its own tar file.
to="/run/media/userx/1TB/JustCreated-Tars-Gzs"
mkdir -pv "$to"
for dir in `find . -maxdepth 1 -type d | grep -v "^\.$" `; do tar -cvzf "${dir}".tar.gz "${dir}"; done
for dir in ./*.tar.gz ; do mv -v "$dir" "$to" ; done
first time trying this in a script, and in succession, so I am lacking in understanding how tar actually works in that situation.
tar works.
How tar already does work. And it's the inputs you've given it which yield the undesired result. However I think you've already reached this conclusion.
As tar has evolved:
You no longer need the dash to pre-pend the command.
You no longer need the z option, and instead the output filename extension will cause tar to detect that you wish to use gzip.
Sometimes it is helpful to designate a directory to place the resultant tar files as opposed to the same location as the original files. This because when people use wildcards, it can involve the tar file itself.
Your original for loop was of an array which should ALWAYS be quoted unless you have good reason for not doing so, and seeing you have not linux friendly file names
this would also cause issues
I changed it to use an array like I orginally wanted to do.
Code:
TarGzUpFiles()
{
chto=${PathArray[0]}
chto=${chto%/*}
#change to that dir
cd $chto
echo $(pwd)
for dir in ${PathArray[*]##*/} ; do tarme=$dir.tar.gz ; dir1=${dir##*/} ; echo "$tarme $dir1" ; tar cvf "$tarme" "$dir1" ; done
for dir in ./*.tar.gz ; do mv -v "$dir" "$FinalTarGzDest" ; done
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.