I'm trying to check if a file exists, but with a wildcard. Here is my example:
if [ -f "xorg-x11-fonts*" ]; then
printf "BLAH"
fi
I have also tried it without the double quotes.
Join Stack Overflow to learn, share knowledge, and build your career.
|
The simplest should be to rely on
I redirected the EDIT: Since this answer has got a bit of attention (and very useful critic remarks as comments), here is an optimization that also relies on glob expansion, but avoids the use of
This is very similar to @grok12's answer, but it avoids the unnecessary iteration through the whole list. |
|||||||||||||||||||||
|
|
If your shell has a nullglob option and it's turned on, a wildcard pattern that matches no files will be removed from the command line altogether. This will make ls see no pathname arguments, list the contents of the current directory and succeed, which is wrong. GNU stat, which always fails if given no arguments or an argument naming a nonexistent file, would be more robust. Also, the &> redirection operator is a bashism.
Better still is GNU find, which can handle a wildcard search internally and exit as soon as at it finds one matching file, rather than waste time processing a potentially huge list of them expanded by the shell; this also avoids the risk that the shell might overflow its command line buffer.
Non-GNU versions of find might not have the -maxdepth option used here to make find search only the /dir/to/search instead of the entire directory tree rooted there. |
|||||||||||||||||||||
|
|
Here is my answer -
|
|||||||||||||
|
This will work with multiple files and with white space in file names. |
|||||||||
|
|
UPDATE: Okay, now I definitely have the solution:
|
|||||||||||||||||||||
|
|
Maybe this will help someone:
|
|||||||||||||||||||||
|
|
You can do the following:
This works with sh and derivates: ksh and bash. It doesn't create any sub-shell. $(..)and `...` commands create a sub-shell : they fork a process, and they are inefficient. Of course it works with several files, and this solution can be the fastest, or second the fastes one. |
|||||
|
|
The question wasn't specific to Linux/Bash so I thought I would add the Powershell way - which treats wildcards different - you put it in the quotes like so below:
I think this is helpful because the concept of the original question covers "shells" in general not just Bash or Linux, and would apply to Powershell users with the same question too. |
|||
|
|
|
The bash code I use
Thanks! |
|||
|
|
|
Strictly speaking, if you only want to print "Blah" here is the solution :
Here is another way :
But I think the most optimal is as follow, because it won't try to sort file names :
|
|||
|
|
|
Here's a solution for your specific problem that doesn't require
As you can see, it's just a tad more complicated than what you were hoping for, and relies on the fact that if the shell is not able to expand the glob, it means no files with that glob exist and If we were to generalize the procedure, though, we should take into account the fact that files might contain spaces within their names and/or paths and that the glob char could rightfully expand to nothing (in your example, that would be the case of a file whose name is exactly xorg-x11-fonts). This could be achieved by the following function, in bash.
Going back to your example, it could be invoked like this.
Glob expansion should happen within the function itself for it to work properly, that's why I put the argument in quotes and that's what the first line in the function body is there for: so that any multiple arguments (which could be the result of a glob expansion outside the function, as well as a spurious parameter) would be coalesced into one. Another approach could be to raise an error if there's more than one argument, yet another could be to ignore all but the 1st argument. The second line in the function body sets the The third line in the function body does two things:
|
|||||||||||||||||
|
|
I use this:
|
|||||||||
|
|
IMHO it's better to use The following example tests if a directory has entries:
When
When
Replace This approach works nicely in shell scripts. The originally question was to look for the glob
Note that the else-branched is reached if |
|||||
|
|
|||
|
|
|
Try this
edit 2014-04-03 (removed dbl-quotes and added test file 'Charlie 22.html' (2 spaces)
Test
Note that this only works in the current directory, or where the var |
|||||||||||||||||||||
|
|
If there is a huge amount of files on a network folder using the wildcard is questionable (speed, or command line arguments overflow). I ended up with:
|
||||
|
|
|
You can also cut other files out
|
||||
|
|
|
How about
|
|||||
|
|
man test
will work for dir\file. regards |
|||||||||||||
|
[command, most likely causing[to exit with an error and therefore be interpreted as no files matching. – Richard Hansen Jun 17 '11 at 6:31