Linux 检查目录中是否存在某种文件类型/扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3856747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:41:07  来源:igfitidea点击:

Check whether a certain file type/extension exists in directory

linuxbashshell

提问by Wurlitzer

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

您将如何使用 bash 判断目录中是否存在特定扩展名的文件?

Something like

就像是

if [ -e *.flac ]; then 
echo true; 
fi 

采纳答案by JeremyWeir

#!/bin/bash

count=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
echo true
fi 

回答by dtlussier

You need to be carful which flag you throw into your ifstatement, and how it relates to the outcome you want.

你需要小心你在你的if陈述中加入哪个标志,以及它与你想要的结果的关系。

If you want to check for only regular filesand not other types of file system entries then you'll want to change your code skeleton to:

如果您只想检查常规文件而不检查其他类型的文件系统条目,那么您需要将代码框架更改为:

if [ -f file ]; then
echo true;
fi

The use of the -frestricts the ifto regular files, whereas -eis more expansive and will match all types of filesystem entries. There are of course other options like -dfor directories, etc. See http://tldp.org/LDP/abs/html/fto.htmlfor a good listing.

的使用将-f限制if为常规文件,而-e更广泛并且将匹配所有类型的文件系统条目。当然还有其他选项,如-d目录等。请参阅http://tldp.org/LDP/abs/html/fto.html以获得好的列表。

As pointed out by @msw, test(i.e. [) will choke if you try and feed it more than one argument. This might happen in your case if the glob for *.flacreturned more than one file. In that case try wrapping your iftest in a loop like:

正如@msw 所指出的,如果您尝试提供多个参数,test(即[)会窒息。如果 glob*.flac返回多个文件,则可能会在您的情况下发生这种情况。在这种情况下,尝试将您的if测试包装在一个循环中,例如:

for file in ./*.pdf
do
    if [ -f "${file}" ]; then
    echo 'true';
    break
    fi
done

This way you breakon the first instance of the file extension you want and can keep on going with the rest of the script.

这样您break就可以使用所需文件扩展名的第一个实例,并且可以继续执行脚本的其余部分。

回答by Ruel

#!/bin/bash
files=$(ls /home/somedir/*.flac 2> /dev/null | wc -l)
if [ "$files" != "0" ]
then
echo "Some files exists."
else
echo "No files with that extension."
fi

回答by wkl

#/bin/bash

myarray=(`find ./ -maxdepth 1 -name "*.py"`)
if [ ${#myarray[@]} -gt 0 ]; then 
    echo true 
else 
    echo false
fi

回答by ghostdog74

   shopt -s nullglob
   set -- $(echo *.ext)
    if [ "${#}" -gt 0 ];then
      echo "got file"
    fi

回答by glenn Hymanman

bash only:

仅重击:

any_with_ext () ( 
    ext=""
    any=false
    shopt -s nullglob
    for f in *."$ext"; do
        any=true
        break
    done
    echo $any 
)

if $( any_with_ext flac ); then
    echo "have some flac"
else 
    echo "dir is flac-free"
fi

I use parentheses instead of braces to ensure a subshell is used (don't want to clobber your current nullglobsetting).

我使用括号而不是大括号来确保使用子shell(不想破坏当前nullglob设置)。

回答by Paused until further notice.

shopt -s nullglob
if [[ -n $(echo *.flac) ]]    # or [ -n "$(echo *.flac)" ]
then 
    echo true
fi

回答by frayser

This uses ls(1), if no flac files exist, ls reports error and the script exits; othewise the script continues and the files may be be processed

这里使用ls(1),如果不存在flac文件,ls报错,脚本退出;否则脚本会继续,文件可能会被处理

#! /bin/sh
ls *.flac  >/dev/null || exit
## Do something with flac files here

回答by Erik Buitenhuis

The top solution (if [ -e *.flac ];)did not work for me, giving: [: too many arguments

顶级解决方案(if [ -e *.flac ];)对我不起作用,给出:[: too many arguments

if ls *.flac >/dev/null 2>&1;then it will work.

if ls *.flac >/dev/null 2>&1;然后它会起作用。

回答by jochen

Here is a solution using no external commands (i.e. no ls), but a shell function instead. Tested in bash:

这是一个不使用外部命令(即 no ls),而是使用 shell 函数的解决方案。在 bash 中测试:

shopt -s nullglob
function have_any() {
    [ $# -gt 0 ]
}

if have_any ./*.flac; then
    echo true
fi

The function have_anyuses $#to count its arguments, and [ $# -gt 0 ]then tests whether there is at least one argument. The use of ./*.flacinstead of just *.flacin the call to have_anyis to avoid problems caused by files with names like --help.

该函数have_any用于$#对其参数进行计数,[ $# -gt 0 ]然后测试是否至少存在一个参数。在调用中使用./*.flac而不仅仅是*.flac在调用中have_any是为了避免名称为--help.