Originally Posted by
George Heine
[...]
For example: typing
Code:
newest.sh . -iname \*.pdf
in a directory which happens to contain a file named "file.pdf" results in the error
Code:
find: paths must precede expression: file.pdf
How can I insulate the argument to "-iname" from being interpreted by the shell?
Thanks for any help.
The problem here is not '-iname', but the stripping of the backslash from the shell, causing the unwanted expansion of '*.pdf' after find's '-iname' option, and the command line would have looked like:
Code:
find . -iname file.pdf other_file.pdf ...
which is a syntax error, (but it'd have worked fine if it were only one pdf file in the search path). One way to avoid shell quoting issues is using "$@":
Code:
#!/bin/bash
set -x
format="%T@\t%s\t%Tx %TH:%TM:%TS\t%p\n"
find "$@" -printf "$format" | sort -k1n | cut -f2-
Or using arrays:
Code:
...
arr=("$@")
find "${arr[@]}" -printf "$format" | sort -k1n | cut -f2-
And running it:
Code:
./newest.sh . -iname "*.pdf"
P.S. For more convenience, I'd use quotes instead of backslashes when escaping special characters in the command line, for example:
Code:
./newest.sh "dir with space/" -iname "*.pdf" -type f
Bookmarks