使用单个文件在当前目录中创建新的tar文件

时间:2015-06-04 20:24:11

标签: bash tar

我正在编写一个bash脚本,用于检查特定fileName.log是否存在tar存档,如果不存在,则使用fileName.log创建一个。如果tar已经存在,那么我需要将fileName.log附加到它。我从来没有真正使用tar档案,除了解压缩和解压缩已经给我的.tar.gz文件。我确定我的问题是我的语法,但是我无法根据手册页找出正确的语法。

我的代码:

      # check if tarball for this file already exists. If so, append it. If not, create new tarball
      if [ -e "$newFile.tar" ];
      then
              echo "tar exists"
              tar -cvf "$newFile" "$newFile.tar"
      else
              echo "no tar exists"
              tar -rvf "$newFile"
      fi

2 个答案:

答案 0 :(得分:1)

如果您想以这种方式将$newfile添加到$newfile.tar

if [ -f "$newFile.tar" ];
then
        echo "tar exists"
        tar -rvf "$newFile.tar" "$newFile"
else
        echo "no tar exists"
        tar -cvf "$newFile.tar" "$newFile"
fi

答案 1 :(得分:1)

非常接近,您的-c-r标记已反转(c已创建,r附加)并且您希望首先放置文件名,如下所示:< / p>

if [ -e "$newFile.tar" ];
then
    echo "tar exists"
    tar -rvf "$newFile.tar" "$newFile"
else
    echo "no tar exists"
    tar -cvf "$newFile.tar" "$newFile"
fi