tags:

views:

78

answers:

3

i am trying to make a very simple bash script to find files matching the given name in the directory structure of the current directory. So, I used the find function like this

ARGS=1
E_BADARGS=65
E_NOFILE=66

if [ $# -ne "$ARGS" ]  # Correct number of arguments not passed
then
  echo "Usage: `basename $0` filename"
  exit $E_BADARGS
fi

echo  `find ./ -type f -name \$1`

this works fine but unlike when I use the find command in the command line, the resulting file paths are not separated by newline but just by a space. This naturally isn't too easy to see in the screen. How can I echo so that each file it finds will be separated by a newline.

+1  A: 

Try changing

echo  `find ./ -type f -name \$1`

to

find ./ -type f -name $1
codaddict
@codaddict initially i just had the command there without the echo, but that didn't show any results. so i added the echo.
sfactor
remove the echo and the backticks.
codaddict
And also remove the \ infront of `$`
codaddict
+1  A: 

As @codaddict noted, echo is unnecessary here. But it's also a good exercise to understand why does your code behave in such a way. Hint: compare

echo  `find ./ -type f -name \$1`

and

echo  "`find ./ -type f -name \$1`"
Roman Cheplyaka
@Roman well when i did it with the "" quotes it works fine. so, that solved the problem, but as both you and @codaddict said echo is unnecessary, but removing the backticks and the echo gives me nothing, the script simply runs and then stops. Although thanx for helping me make it work though :).
sfactor
+2  A: 

I would change your find command to this one:

find . -maxdepth 1 -type f -name "$1"

Note that the double quotes are kind important in find command, to treat regular expressions correclty. Also I added maxdepth 1 to search files only in the current directory

jyzuz