0

I have a requirement like I have to create the tars from folders like

"/xyz/test1/abc1/"
"/xyz/test1/abc2/"
"/xyz/test3/abc3/"

I have to create tars like abc1.tar , abc2.tar , abc3.tar in each respective directories and copy to ~/tmp directory any bash script would help me out. I have like 16 such folders.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate of [Tar archiving that takes input from a list of files](https://stackoverflow.com/questions/8033857/tar-archiving-that-takes-input-from-a-list-of-files) – MackM Nov 30 '17 at 17:27

3 Answers3

0

We can tar all abc directories together with below,

tar -cvf abc.tar /xyz/test*/abc*

if need to have separate tar files, then

cat DirList.txt
   /xyz/test1
   /xyz/test2
   /xyz/test3

feed the directory list into a loop,

    i=1                          // **i** - variable to to make the file names unique
    while read dirName
    do
       cd ${dirName}                 // --> change directory
       tar -cvf abc${i}.tar abc${i} // --> tar abc directory
       mv abc.tar /tmp               // --> move tar file to  /tmp
       i=$(expr $i + 1)              //--> increase variable i by one for next iteration.

    done < DirList.txt
Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
  • What if I have "/xyz/test1/a1/" "/xyz/test2/b2/" "/xyz/test3/c3/" directories and I need some a.tar b.tar and c.tar respectively – user8895496 Dec 01 '17 at 07:25
  • Either, we can remove `&quot (')` from directory list, may be with --> `sed -i "s/'//g" DirList.txt` or can use --> `cd $(echo $dirName | sed "s/'//g" )` in script itself to change the directory. and to get `a.tar, b.tar` names, we can use a for loop for `tarfileName` as below `for tarFileName in {a..z}` `do` `# --> while loop goes inside - with changed tar command` `done` , thanks – Jithin Scaria Dec 01 '17 at 08:41
  • Sorry that how did that quots came . Please find find my query below "/xyz/test1/abc1/" "/xyz/test2/abc2/" "/xyz/test3/abc3/" I need to tar some dir1.tar , dir2.tar and dir3.tar in respective locations – user8895496 Dec 01 '17 at 09:01
  • Sorry, i am not clear on "Sorry that how did that quots came" in your comment, can you explain a little more, a screenshot perhaps ? – Jithin Scaria Dec 01 '17 at 10:39
  • I have a requirement like below. I need to go into each application target directory and generate a tar of application home.tar and copy the same to /tmp on the same machine. The application directories look like below $/artifactserver/com/xyz/abc/ ls -lrt App1/ to App16/ cd App1/target/ tar -cvf App1Home.tar App1Home/ cp -r App1Home.tar /tmp cd ../.. cd App2/target/ tar -cvf App2Home.tar App2Home/ cp -r App2Home.tar /tmp – user8895496 Dec 02 '17 at 17:14
0

@JithinScaria please find my query below

I have a requirement like below I need to do a build for multiple apps after doing the below

  1. Git clone , git fetch and build by maven with a shell script.—> all these were taken care with a shell script.

    1. Now after all the apps were build I need to go into each application target directory and generate a tar of application home.tar and copy the same to /tmp on the same machine.

The application directories look like below $/artifactserver/com/xyz/abc/ ls -lrt App1/
App2/ App3/
App4/

Till

App16/

cd App1/target/

tar -cvf App1Home.tar App1Home/

cp -r App1Home.tar /tmp

cd ../..

cd App2/target/

tar -cvf App2Home.tar App2Home/

cp -r App2Home.tar /tmp

cd ../..

So on How can I achieve all these .tars and copy them to /tmp with block.

0

I hope either one below will do your work,

option 1:

create a file with the list of 16 directories as below,

$cat DirList.txt
/artifactserver/com/xyz/abc/App1/target/
/artifactserver/com/xyz/abc/App2/target/
/artifactserver/com/xyz/abc/App3/target/
...
/artifactserver/com/xyz/abc/App16/target/

use the this file as input to while loop below,

i=1                              // **i** - incremental variable
while read dirName
do
   cd ${dirName}                         # --> change directory
   tar -cvf App${i}Home.tar App${i}Home/ # --> tar directory
   cp App${i}Home.tar /tmp               # --> copy tar file to  /tmp
   i=$(expr $i + 1)                      # --> increase variable i by one.
done < DirList.txt 

option 2:

create a file with the list of 16 directories, tar filename and directory name to be tarred with a delimiter, here i used | as the delimiter

$cat DirList.txt
/artifactserver/com/xyz/abc/App1/target/|App1Home.tar|App1Home
/artifactserver/com/xyz/abc/App2/target/|App2Home.tar|App2Home
/artifactserver/com/xyz/abc/App3/target/|App3Home.tar|App3Home
...
/artifactserver/com/xyz/abc/App16/target/|App16Home.tar|App16Home

use the this file as input to while loop below,

while read line
do
   dirName=$(echo $line | awk -F'|' '{print $1}')  # --> assign first var to directory
   tarFile=$(echo $line | awk -F'|' '{print $2}')  # --> tar file name
   dirToTar=$(echo $line | awk -F'|' '{print $3}') # --> directory name to be tarred
   cd ${dirName}                                   # --> change directory
   tar -cvf ${tarFile} ${dirToTar}                 # --> tar directory
   cp ${tarFile} /tmp                              # --> copy tar file to  /tmp
done < DirList.txt 
Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26