I want the program to look like this :
read a
if[ find . name $a ]; then
echo "You found the file"
else "You haven t found the file"
fi
Whether or not anything was found, find
always returns true. You can use grep
to determine if find
found something:
read -r a
if find . -maxdepth 1 -name "$a" -print -quit | grep -q .
then
echo "You found the file"
else
echo "You haven't found the file"
fi
Quitting after the first match (-print -quit
) should improve performance, as Eliah has noted. Use -maxdepth 1
to limit results to the current directory - but then find
is overkill for this.
-print -quit
can be added to the end of the find
command to slightly improve performance. You could even print only a blank line: -printf '\n' -quit
This doesn't change find
's exit status, of course; piping to grep -q
remains necessary.
May 12, 2015 at 17:15
grep
will quit after the first line is read, the pipe will be closed and find
will have to quit anyway.
find
will continue walking the directory hierarchy. I suppose that, if there are many more directories to search but no other matches, the performance improvement could be significant.
May 12, 2015 at 17:21
-mindepth 1
too. If "$a"
expands to a literal filename, it won't be needed here: that name is unlikely to be .
. But to search a single directory with find
whose name is not given as .
, or (sometimes) when "$a"
expands to a glob rather than a literal filename, -mindepth 1 -maxdepth1
may need to be used in a script. Btw, I wonder if perhaps find
is not overkill for searching the current directory after all: if the value of a
is a glob, find -name "$a"
expands it; I don't know of any elegant way to achieve such double-indirection with the shell alone.
May 14, 2015 at 16:40
If don't have to use the find
command, using the test
command (or its short form [
...]
) would be easier, IMHO. With test
, the e
switch does what you're looking for.
#!/bin/bash
read -r a
if [[ -e $a ]]; then
echo "You found the file"
else
echo "You haven't found the file"
fi
But be aware that test
only looks for the file in the current directory, not in any subdirectories (thanks to EliahKagan for the reminder).
You can find a good overview about test
at the Bash Hackers Wiki
find
command in the question. So I'd assume that's what he's asking for.
May 12, 2015 at 16:46
search the current directory
in the heading although used find
in the example..its a bit confusing ..i think as far as the heading is concerned this is a valid answer..
find
is recursive. Without -maxdepth
, it will search as far down as it can.
**/$a
wouldn't find $a
if it's in the current dir directly, if I remember correctly. So I'd have to combine two tests, and then it wouldn't be easier than using find
anymore.
May 12, 2015 at 17:21
find
is a poor tool to use.-maxdepth 1
while usingfind
so thatfind
won't traverse to any subdirectory, also note thatfind
would be a overkill in that case..you could just usetest -e
withif
..a
contain the literal name of a single file whose presence you're trying to test for? Or could$a
expand to a glob pattern likefoo*
? Or does the shell variable expansion$a
not actually appear in your script all, and is only a metasyntactic variable representing a filename or glob pattern that appears literally in the script?