Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script that is asking a user for input, then passes that variable to a find command. I've tried quoting/escaping the variable every way I know, but it keeps failing.

read -p "Please enter the directory # to check: " MYDIR
count=`/usr/bin/find /path/to/$MYDIR -name *.txt -mmin -60 | wc -l`
if [ $count != 0 ]
    then 
         echo "There have been $count txt files created in $MYDIR in the last hour"
    else 
         echo "There have been no txt files created in the last hour in $MYDIR "
    fi

When running, I get this:

Please enter the directory # to check: temp_dir

/usr/bin/find: paths must precede expression
Usage: /usr/bin/find [-H] [-L] [-P] [path...] [expression]
There have been no txt files created in the last hour in temp_dir 
share|improve this question
    
Use single or double quotes around *.txt ie. "*.txt" to prevent it getting interpreted by the shell. –  Sami Laine May 3 at 13:55

1 Answer 1

up vote 1 down vote accepted

You must quote the pattern in -name option:

count=`/usr/bin/find /path/to/$MYDIR -name '*.txt' -mmin -60 | wc -l`

If you don't use the quote, so the shell will expand the pattern. Your command become:

/usr/bin/find /path/to/$MYDIR -name file1.txt file2.txt ... -mmin -60 | wc -l

You feed all files, which has name end with .txt to -name option. This causes syntax error.

share|improve this answer
    
Ah...so I was trying to quote the wrong problem child... thanks! –  user66771 May 3 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.