LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash: script to tar up a bunch of sub-dir separately (https://www.linuxquestions.org/questions/programming-9/bash-script-to-tar-up-a-bunch-of-sub-dir-separately-4175657052/)

BW-userx 07-08-2019 11:17 AM

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.

BW-userx 07-08-2019 11:57 AM

changed it to,
Code:

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


rtmistler 07-08-2019 12:39 PM

Quote:

Originally Posted by BW-userx (Post 6013104)
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:
  1. You no longer need the dash to pre-pend the command.
  2. You no longer need the z option, and instead the output filename extension will cause tar to detect that you wish to use gzip.
  3. 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.

grail 07-08-2019 10:34 PM

Glad to see you found a solution :)

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

BW-userx 07-09-2019 02:34 PM

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
}



All times are GMT -5. The time now is 02:06 AM.