[英]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 我有一个要求,就像我必须从类似的文件夹创建tar
"/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. 我必须在每个目录中分别创建
abc1.tar
, abc2.tar
, abc3.tar
类的abc1.tar
, abc2.tar
任何bash脚本都可以帮助我的工作复制到~/tmp
目录中。 I have like 16 such folders. 我喜欢16个这样的文件夹。
We can tar all abc directories together with below, 我们可以将下面的所有abc目录压缩
tar -cvf abc.tar /xyz/test*/abc*
if need to have separate tar files, then 如果需要单独的tar文件,则
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 @JithinScaria请在下面找到我的查询
I have a requirement like below I need to do a build for multiple apps after doing the below 我有如下要求,在完成以下操作后,我需要为多个应用程序进行构建
Git clone , git fetch and build by maven with a shell script.—> all these were taken care with a shell script. Git clone,git获取并由maven使用Shell脚本进行构建。—>所有这些操作均通过Shell脚本进行。
The application directories look like below $/artifactserver/com/xyz/abc/ ls -lrt App1/ 应用程序目录如下所示:$ / artifactserver / com / xyz / abc / ls -lrt App1 /
App2/ App3/ App2 / App3 /
App4/ App4 /
Till 直到
App16/ App16 /
cd App1/target/ cd App1 / target /
tar -cvf App1Home.tar App1Home/ tar -cvf App1Home.tar App1Home /
cp -r App1Home.tar /tmp cp -r App1Home.tar / tmp
cd ../.. cd ../ ..
cd App2/target/ cd App2 / target /
tar -cvf App2Home.tar App2Home/ tar -cvf App2Home.tar App2Home /
cp -r App2Home.tar /tmp cp -r App2Home.tar / tmp
cd ../.. cd ../ ..
So on How can I achieve all these .tars and copy them to /tmp with block. 因此,我该如何实现所有这些.tars并将它们复制到带有块的/ tmp中。
I hope either one below will do your work, 我希望下面的任何一个都能做好您的工作,
option 1: 选项1:
create a file with the list of 16 directories as below, 创建一个包含以下16个目录的列表的文件,
$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, 使用此文件作为以下while循环的输入,
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: 选项2:
create a file with the list of 16 directories, tar filename and directory name to be tarred with a delimiter, here i used |
创建一个文件,其中包含16个目录的列表,tar文件名和要用定界符覆盖的目录名,在这里我使用了
|
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循环的输入,
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.