As I stated in my comment, I assume that your question is actually
How do I use the find command to test a single file to see
whether it satisfies a find test, such as size = some specified number?
where the name of the file is specified as a parameter to a script
(and thus is not known to you in advance).
You’re building up an answer around the command
if find . -name "$1" -size "$2"c | grep -q .
then
echo OK
fi
I’ve taken the liberty of inserting the quotes that dhag recommended.
This is not a bad start, but it has some problems:
- If the user runs the script with a parameter of
test.txt, and
there is a file called test.txt in a subdirectory of the current directory,
then find will find it and print its pathname,
and so the grep will succeed.
But, if your script is trying to do something with a file called test.txt
in the current directory, it will fail, because there is no such file.
You can fix this by adding -maxdepth 1
to prevent find from looking in subdirectories.
- If the user runs the script with a parameter of
my_subdir/test.txt,
then find will fail, because the argument to -name may not contain slashes.
- If you have a really mischievous user,
he might type
"*" just to cause trouble.
Using find . seems a little silly
when you don’t actually want to search a directory tree.
You know the name of the file; you just want to test its properties.
Well, if you read between the lines of find(1) with one eye closed,
you realize that it never actually says that the path argument(s)
have to be directories — it just implies it.
It turns out that you can say
find filename [expression]
Applied to your question, this becomes
find "$1" -size "$2"c
As a sanity check, I recommend:
if [ -f "$1" ] && find "$1" -size "$2"c | grep -q .
then
echo OK
fi
to avoid getting an error message from find
if the specified file does not exist at all
(and to prevent the aforementioned mischievous user
from giving the script the name of a directory).
"$1","$2"c; otherwise the script will break if any of its parameters contain white space.findcommand to test a single file to see whether it satisfies afindtest, such assize= some specified number."