简体   繁体   中英

How to create separate tar files from separate directories and to copy to /tmp directory

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.

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

@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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM