Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to glob every hidden file and directory, but not the current (.) and parent directory (..).

I am using bash.

Observe current behaviour:

$ ls -a
.  ..  ...a  ...aa  ..a  ..aa  .a  .aa  .aaa  a
$ echo *
a
$ echo .*
. .. ...a ...aa ..a ..aa .a .aa .aaa

I would like .* to behave like this

$ echo .*
...a ...aa ..a ..aa .a .aa .aaa

There is the shell option dotglob

$ shopt -s dotglob

that works in a way; now I can use * to glob everything (hidden or not) but not . and ..

$ echo *
...a ...aa ..a ..aa .a .aa .aaa a

but now I can't differentiate between hidden or not. Also, .* still globs . and ..

$ echo .*
. .. ...a ...aa ..a ..aa .a .aa .aaa

Is there a way to make .* not expand to . and ..?

share|improve this question

3 Answers

up vote 13 down vote accepted

You can use the GLOBIGNORE variable to hide the . and .. directories. This does automatically also set the dotglob option, so * now matches both hidden and non-hidden files. You can again manually unset dotglob, though, this then gives the behavior you want.

See this example:

$ ls -a
.  ..  a  .a  ..a
$ GLOBIGNORE=". .."
$ shopt -u dotglob
$ echo * #all (only non-hidden)
a
$ echo .* #all (only hidden)
.a ..a
share|improve this answer
This only works if you include the glob in the command; env GLOBIGNORE=". .." ls -a returns . and .. but ... ls -ad .* doesn't. – gvkv Aug 24 '10 at 13:04
I don't get this comment... The question was not about the ls command, but about a glob -- "including the glob in the command" is exactly what the OP wanted to do... – Marcel Stimberg Aug 24 '10 at 13:31
It's informational not critical. – gvkv Aug 24 '10 at 13:48
No problem, I'm not afraid of criticism. It's just that I didn't understand the connection between the comment and my answer. But as I did also not understand your the connection between your answer and the original question that's consistent at least ;) – Marcel Stimberg Aug 24 '10 at 13:55
Yeah, I got caught up with trying to make extended globbing patterns work with . the way I think it should using ls as a canonical command and forgot about the details of the post. – gvkv Aug 24 '10 at 14:03
show 5 more comments

Are you just looking for files? Are you in a position to use find?

Something like:

find . -maxdepth 1 -name ".*" -f -printf "%P \n"
share|improve this answer
ls -1a|egrep -v '^(\.|\.\.)$'
share|improve this answer
1  
ls should never be parsed or used in scripts, and certainly not as a replacement to globbing. – MestreLion Mar 28 at 7:19
Right, see mywiki.wooledge.org/ParsingLs for more information – ignis Apr 25 at 9:45

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.