Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ]
         echo "There have been $count txt files created in $MYDIR in the last hour"
         echo "There have been no txt files created in the last hour in $MYDIR "

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 '14 at 13:55
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 '14 at 13:40

Your Answer


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.