【问题标题】:How to create separate tar files from separate directories and to copy to /tmp directory如何从单独的目录创建单独的 tar 文件并复制到 /tmp 目录
【发布时间】:2018-05-14 16:51:41
【问题描述】:

我有一个要求,比如我必须从文件夹中创建焦油

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

我必须在每个目录中创建像 abc1.tarabc2.tarabc3.tar 这样的焦油,然后将任何 bash 脚本复制到 ~/tmp 目录中。我有 16 个这样的文件夹。

【问题讨论】:

标签: linux shell scripting


【解决方案1】:

我们可以将所有 abc 目录连同下面一起 tar,

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

如果需要单独的tar文件,那么

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

将目录列表输入循环,

    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

【讨论】:

  • 如果我有“/xyz/test1/a1/”怎么办? “/xyz/test2/b2/” “/xyz/test3/c3/”目录,我分别需要一些 a.tar b.tar 和 c.tar
  • 要么,我们可以从目录列表中删除&amp;quot ('),可以使用 --> sed -i "s/'//g" DirList.txt 或者可以在脚本本身中使用 --> cd $(echo $dirName | sed "s/'//g" ) 来更改目录。为了得到a.tar, b.tar的名字,我们可以使用tarfileName的for循环如下for tarFileName in {a..z}do# --&gt; while loop goes inside - with changed tar commanddone,谢谢
  • 对不起,那些报价是怎么来的。请在“/xyz/test1/abc1/”“/xyz/test2/abc2/”“/xyz/test3/abc3/”下面找到我的查询各自的位置
  • 对不起,我不清楚你评论中的“对不起,那些引号是怎么来的”,你能解释一下,也许是截图?
  • 我有如下要求。我需要进入每个应用程序目标目录并生成应用程序 home.tar 的 tar 并将其复制到同一台机器上的 /tmp 中。应用程序目录如下所示 $/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
【解决方案2】:

@JithinScaria 请在下面找到我的查询

我有一个像下面这样的要求,我需要在完成以下操作后为多个应用程序构建

  1. Git clone , git fetch and build by maven with a shell script.-->所有这些都由 shell 脚本处理。

    1. 现在,在构建完所有应用程序之后,我需要进入每个应用程序目标目录并生成应用程序 home.tar 的 tar,并将其复制到同一台机器上的 /tmp。

应用程序目录如下所示 $/artifactserver/com/xyz/abc/ ls -lrt 应用程序1/
应用2/ 应用程序3/
应用程序4/

直到

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

等等 我怎样才能实现所有这些 .tars 并将它们复制到 /tmp with block.

【讨论】:

    【解决方案3】:

    我希望下面的任何一个都能完成你的工作,

    选项 1:

    创建一个包含 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/
    

    使用这个文件作为下面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 
    

    选项 2:

    16 目录列表、tar 文件名和要使用分隔符 tar 的目录名创建一个文件,这里我使用 | 作为分隔符

    $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
    

    使用这个文件作为下面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 
    

    【讨论】:

    • Option2 已满足我的要求。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多