Check whether a certain file type/extension exists in directory

前端 未结 15 1559
时光取名叫无心
时光取名叫无心 2021-01-30 06:12

How would you go about telling whether files of a specific extension are present in a directory, with bash?

Something like

if [ -e *.flac ]; then 
echo t         


        
15条回答
  •  囚心锁ツ
    2021-01-30 06:36

    Here's a fairly simple solution:

    if [ "$(ls -A | grep -i \\.flac\$)" ]; then echo true; fi

    As you can see, this is only one line of code, but it works well enough. It should work with both bash, and a posix-compliant shell like dash. It's also case-insensitive, and doesn't care what type of files (regular, symlink, directory, etc.) are present, which could be useful if you have some symlinks, or something.

提交回复
热议问题