Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Ok, this question targets at Unix/Linux shells! I want a shell globbing- (aka wildchard-) expression that matches all files including hidden files in current directory non-recursively (maxdepth == 1). So far I have to perform two commands or use long workarounds (see below):

ls -lad *vim*
ls -lad .*vim*

while using the zsh. But if I remember its the same for dash and bash, right?

Short workarounds:

  • ls -la | grep vim
  • find . -maxdepth 1 | grep vim

But I've wondered a hundred times, isn't there a simple globbing solution to this, and why * does not match the dot character?

share|improve this question
add comment

5 Answers

up vote 9 down vote accepted

* does match a . character.

It simply doesn't match . when it is the first character of the name. This provides a so-called "dot file" mechanism for "hiding" files.

In zsh:

Set the GLOB_DOTS shell option. This is in § 14.8 of the zsh user manual. Note that . and .. are always excluded even if this option is turned on.

In bash:

Set the dotglob shell option. This is in § 3.5.8 of the bash user manual. Note that setting the GLOBIGNORE shell variable implicitly sets dotglob; that bash (unlike zsh) doesn't automatically exclude . and .. when dotglob is enabled; but that bash will do that when GLOBIGNORE is set. So setting GLOBIGNORE=. will have the effect of turning on dotglob and excluding . and ...

In GNU find:

Don't do anything. As of findutils 4.2.2, the globbing for -name and -iname already matches names with dots as the first character. This is in § 2.1.1 of the findutils user manual.

share|improve this answer
3  
BE CAREFUL with this! In Bash .* matches everything including one directory up. –  Corporal Touchy Jan 2 '12 at 10:52
add comment

To build on the answer of @daniel kullman, this seems to accomplish what you want with a fairly simple glob, though its not strictly a single gob.

Recursively search for the string "blah" in all files in the current directory, including hidden files, and excluding the ".." that causes the parent directory structure to be search:

grep blah {,.}[!.]*
share|improve this answer
add comment

In ZSH (without changing GLOB_DOTS) you could do

  ls -lad *vim*(D)
share|improve this answer
add comment

If you don't want to change any options, any of these will do:

ls -ld {,.}*vim*
ls -ld *vim* .*vim*
find . -maxdepth 1 -name "*vim*"
share|improve this answer
    
why there is a comma in between the braces? Does the * also don't match the comma in the front of the file name? The other two I already gave as workarounds. –  math Jan 2 '12 at 15:57
2  
@math: in bash you can use things like {a,b,c}xax bx cx, so {,.}*vim* expands to two arguments *vim* .*vim* –  grawity Jan 2 '12 at 16:31
add comment

In bash, enable the dotglob option:

shopt -s dotglob

By default, hidden files are hidden to not cause surprises and annoyances – for example, you run ls in your home directory, see some files from last month, try to remove them with rm *net* and unknowingly nuke your carefully written .nethackrc.

share|improve this answer
1  
Yes, indeed, enabling it by default is not a good idea!, but fortunately Zsh is able to set and unset options via: setopt and unsetopt, so I can enable it when I need it more often and disable it afterwards. –  math Jan 2 '12 at 12:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.