Stupid Bash Question

Home » CentOS » Stupid Bash Question
CentOS 11 Comments

the relevant snippet is…

cd $IN
if test -n “$(find . -maxdepth 1 -name $NAME -print -quit)”

and if there is one file in this directory – ie, this works fine

but if there are two (or more) files in this directory –,

then I get an error… find: paths must precede expression

So my code is evidently wrong. I just want a test for 1 or more files ending in .mov in the directory?

Any one want to toss me a bone here?

11 thoughts on - Stupid Bash Question

  • Run the script with -x to see what’s happening. $NAME is probably getting expanded. You might have to set noglob.

  • The substitution of $NAME is expanding the wild card, giving you a single
    -name with two arguments. You probably want something like:



  • I gess you could also avoid the expension with:
    if test -n “$(find . -maxdepth 1 -name \”$NAME\” -print -quit)”

    2012/8/15 Steve Thompson :

  • This worked for me:

    $ cat set -o noglob NAME=”*.mov”
    find . -maxdepth 1 -name $NAME -print

    $ touch
    $ bash

  • how about something (seemingly simple) like this:

    find out how many there are:

    count=`ls * | grep -c .MOV$`

    then diagnose the result:

    if [ $count -ge 1 ]
    do your stuff here else
    echo oops. nothing to do!

    of course, there are pitfalls… we’re asuming that there are only FILES
    that would match the pattern “.MOV”, no directories.

  • Are you trying to find out if there are one or more files that match, or are you trying to list the name(s) of the files?

    NAME=`ls *.mov`

    for FILE in $NAME
    do echo “$FILE”

    ^ ^ Mark LaPierre Registerd Linux user No #267004

  • The problem is the outermost double quotes in the “$(…)” expression and figuring out how to pass the appropriate quotes into the subshell created by the $(). One trick is to let the outer shell do the interpolation first.

    The following script may be informative:


    echo $NAME
    echo “$NAME”

    echo $(echo $NAME)
    echo $(echo “$NAME”)
    echo $(echo \”$NAME\”)
    echo $(echo ‘$NAME’)

    echo “$(echo $NAME)”
    echo “$(echo “$NAME”)”
    echo “$(echo \”$NAME\”)”
    echo “$(echo ‘$NAME’)”

    if test -n “$(find . -name “$NAME”)”
    echo FOUND IT
    Hope this helps,