find . -type f -exec sh -c \' if [cat ${1} == \"abc\" ] then echo ${1} fi\' _ {} \\;
I got this error:
_: -c: line 1: syntax error:
Put spaces after '[' and before ']' and a ';' after the ']'
another way is to use
[ `cat ${1}` == "abc" ] && echo ${1}
The problem here is that the [
(aka 'test') needs to be separated from cat
by a space, like so:
find . -type f -exec sh -c 'if [ cat ${1} = "abc" ]; then echo ${1}; fi' _ {} \;
You're also missing semicolons after the if
statement and before the fi
statement (it looks like you had these on separate lines originally, based on the spacing that was there, probably due to the way that you had it indented)
As pointed out in the comment below, running this will give the error message too many arguments in if []
. Using double quotes around "cat ${1}"
will avoid the error message, but this simply compares the expansion of "cat ${1}"
with "abc"
. What you're probably looking for is [ "$(cat ${1})" == "abc" ]
. Unfortunately, this is likely to cause problems when you compare a very long file with "abc"
.
You could use grep
to do the test, or take a checksum (e.g. shasum
) of the file, and test that against the checksum of "abc"
, depending on what why you're doing the test.