检查子目录是否存在压缩文件-如果不存在则进行压缩

问题描述 投票:0回答:1

[我正在尝试编写一个脚本,该脚本将检查子目录中是否存在压缩文件,如果文件不存在,则压缩必要的文件。

我尝试了两个版本的脚本:

版本1

for directory in /pool/folder/ID*/; do
    echo "Starting for loop"
    if [ -e /pool/folder/ID*/*.txt.gz ]
    then
        echo "file already exists"
    else
        echo "File does not exist"
        echo "file is being compressed"
        gzip *.txt
    fi
done

这没有用,我认为这与我的-e标志有关:http://www.ducea.com/2009/03/05/bash-tips-if-e-wildcard-file-check-too-many-arguments/

一旦我的“ if”语句开始,终端输出看起来就像它正在尝试检查计算机上的每个子目录:

显示所有4854种可能性? (是或否)

然后继续列出每个目录。因此,我尝试删除我的-e标志并使用ls:

版本2

for directory in /pool/folder/ID*/; do
    echo "Starting for loop"
    files=$(ls /pool/folder/ID*/*.txt.gz 2> /dev/null | wc -l)
    if [ **"$files" != "0"** ]
    then
        echo "zipped file already exists"
    else
        echo "Zipped file does not exist"
    fi
done

但是发生了同样的事情(列出所有目录)。>>

我正在尝试编写一个脚本,该脚本将检查子目录中是否存在压缩文件,如果文件不存在,则压缩必要的文件。我已经尝试了两个版本的脚本:...

linux bash subdirectory
1个回答
0
投票

这是您的追求吗?

© www.soinside.com 2019 - 2024. All rights reserved.