Bash:查找没有给定文件的任何子目录

时间:2017-06-25 23:54:14

标签: bash shell subdirectory

我想知道我的文件是否存在于下面的任何子目录中。子目录是在我的shell脚本中的上述步骤中创建的,下面的代码总是告诉我文件不存在(即使它确实存在),我也希望打印路径。

#!/bin/bash
....

if ! [[ -e [ **/**/somefile.txt && -s **/**/somefile.txt ]]; then
    echo "===> Warn: somefile.txt was not created in the following path: " 
    # I want to be able to print the path in which file is not generated
    exit 1
fi

我知道文件名是somefile.txt,它将在所有子目录中创建,但子目录名称会发生​​很大的变化。因此会发生变化。

1 个答案:

答案 0 :(得分:4)

#!/bin/bash

shopt -s extglob   ## enable **, which by default has no special behavior

for d in **/; do
  if ! [[ -s "$d/somefile.txt" ]]; then
    echo "===> WARN: somefile.txt was not created (or is empty) in $d" >&2
    exit 1
  fi
done