简体   繁体   English

如何从单独的目录创建单独的tar文件并将其复制到/ tmp目录

[英]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.tarabc2.tarabc3.tar类的abc1.tarabc2.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 我有如下要求,在完成以下操作后,我需要为多个应用程序进行构建

  1. 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脚本进行。

    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. 现在,在构建完所有应用程序之后,我需要进入每个应用程序目标目录并生成应用程序home.tar的tar,然后将其复制到同一台计算机上的/ tmp中。

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.

相关问题 如何将多个文件(在另一个文件中列出)tar到不同目录中它们自己的tar中? - How do I tar multiple files (which are listed in another file) to their own separate tar in a different directory? 将mp3文件从一个目录移动到按艺术家,专辑,歌曲名称的单独目录 - Move mp3 files from one directory to separate directories by artist, album, song name 如何使用tar来存档不同目录结构的不同目录中的文件 - How to use tar to archive files from different directories without directory structure bash将日志文件复制到单独的目录中 - bash copying log files into separate directories 如何创建备用的tmp目录? - how to create an alternative tmp directory? 如何从不同的文本文件中复制字符串以在单独的列中并排显示到 Bash 中的第三个文件中 - How to copy strings from different text files to show side by side, in separate columns, into a third file in Bash tar:添加当前目录下的所有文件和目录 INCLUDING.svn 等 - tar: add all files and directories in current directory INCLUDING .svn and so on 修剪目录和当前目录的文件以用于tar - Pruning directories and current directory's files for use with tar 将多个cpanel帐户压缩为单独的.tar.gz文件 - Compressing multiple cpanel account into separate .tar.gz files 如何对目录中的所有文件进行tar压缩并将该tar移至另一个目录 - how to tar all files in a directory and move that tar to another directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM