LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2019, 11:17 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 07-08-2019 at 11:44 AM.
 
Old 07-08-2019, 11:57 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
 
Old 07-08-2019, 12:39 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 07-08-2019, 10:34 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
Old 07-09-2019, 02:34 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
tar dir and sub dir removing files but not existing not empty dir j-me Linux - General 2 08-12-2013 11:37 AM
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration