gpt4 book ai didi

linux - wbash -- 检查目录是否有文件夹和相应的文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:30 26 4
gpt4 key购买 nike

我是 bash 的新手,我想看看我的目录是否包含一个文件夹及其对应的 txt 文件。不是所有的文件夹都有对应的txt文件,目录下有很多文件夹和txt文件,所以想自动化。

示例目录:

a_1/
a_1.txt
b_1/
b_1.txt
c_1/
c_1.txt
d_1/

我了解如何单独检查目录中是否存在文件或文件夹。但是我无法检查目录中是否存在与其对应的 txt 文件的文件夹。

我想要一些可以打印出文件夹存在但文件不存在的东西。从上面的示例中,它将打印具有相应文件夹的文件:folder a_1/and a_1.txt both exist 并且没有相应的 txt 文件:d_1/exist but txt 文件没有。

这是我目前拥有的,但它似乎不起作用。 :(

#!/bin/bash
for x in *; do
if [[ -d "$DIRECTORY" ]] && [[ -f ${f%.txt} ]]; then
echo -n "$x ${x%%.dir}.txt are both present ";
else
echo "directory '$DIRECTORY' existed but didn't find txt file"

最佳答案

根据您的规范,脚本已更正:

#!/bin/bash

for x in *; do
if [[ -d "$x" ]]; then
if [[ -f $x.txt ]]; then
echo "$x and $x.txt are both present "
else
echo "Directory '$x' exists but $x.txt doesn't"
fi
fi
done

关于linux - wbash -- 检查目录是否有文件夹和相应的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com