check if file exists that has digits in it

I have following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>config_path="mypath/"
outD="outDir/"
for doms in Germ500 Port500 Spai500;do
echo "animating for: ${doms}"
config_name="${doms}_config.toml"
# Check if the Germ500_titt_20240903T0000_animation.html exists, otherwise make new
#
[ ! -f "${outD}${doms}_titt_[????????]T[????]_animation.html" ] && echo "File does not exist: make new" || echo "${outD}${doms}_titt_[????????]T[????]_animation.html exist"
done
</code>
<code>config_path="mypath/" outD="outDir/" for doms in Germ500 Port500 Spai500;do echo "animating for: ${doms}" config_name="${doms}_config.toml" # Check if the Germ500_titt_20240903T0000_animation.html exists, otherwise make new # [ ! -f "${outD}${doms}_titt_[????????]T[????]_animation.html" ] && echo "File does not exist: make new" || echo "${outD}${doms}_titt_[????????]T[????]_animation.html exist" done </code>
config_path="mypath/"
outD="outDir/"

for doms in Germ500 Port500 Spai500;do
    echo "animating for: ${doms}"
    config_name="${doms}_config.toml"
    # Check if the Germ500_titt_20240903T0000_animation.html exists, otherwise make new
    # 
    [ ! -f "${outD}${doms}_titt_[????????]T[????]_animation.html" ] && echo "File does not exist: make new" || echo "${outD}${doms}_titt_[????????]T[????]_animation.html exist"

done

How can I fix the command?

I tried the following:

[ ! -f "${outD}${doms}_titt_[0-9]{8}T[0-9]{4}_animation.html" ] && echo "File does not exist: make new" || echo "${outD}${doms}_titt_[????????]T[????]_animation.html exist"

2

The wildcard [????????]T[????] is equivalent to [?]T[?] (character classes only match a single character), which will only match the literal filename ?T?. If you want to match digits, you must use [0-9], i.e. [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9].

But test -f ([ -f … ]) can only handle a single path. Quoting an expression will prevent glob expansion, but even if the glob were expanded, your script would break because, again, -f only handles a single path.

Can I interest you in a find-based solution instead?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if test "$(find "$outD" -maxdepth 1 -name "${doms}_titt_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html" -print -quit)"; then
echo 'file matching pattern exists'
else
echo 'no file matching pattern found'
fi
</code>
<code>if test "$(find "$outD" -maxdepth 1 -name "${doms}_titt_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html" -print -quit)"; then echo 'file matching pattern exists' else echo 'no file matching pattern found' fi </code>
if test "$(find "$outD" -maxdepth 1 -name "${doms}_titt_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html" -print -quit)"; then
  echo 'file matching pattern exists'
else
  echo 'no file matching pattern found'
fi

or a loop-based one?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for file in "$outD/${doms}_titt_"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html; do
if test -f "$file"; then
echo "file $file exists"
else
echo "no file matching pattern found"
fi
break
done
</code>
<code>for file in "$outD/${doms}_titt_"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html; do if test -f "$file"; then echo "file $file exists" else echo "no file matching pattern found" fi break done </code>
for file in "$outD/${doms}_titt_"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9]_animation.html; do
  if test -f "$file"; then
    echo "file $file exists"
  else
    echo "no file matching pattern found"
  fi
  break
done

It’s not bullet proof and could lead to unexpected results if you have a file with the literal pattern as name.

Find additional (portable or bash-only) solutions in Test whether a glob has any matches in Bash

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật