views:

208

answers:

3

I understand that the wildcard * (by itself) will expand in such a way that it means "all non-hidden files in the current folder" with hidden files being those prefixed by a period.

There are two use cases that I would think are useful, but I don't know how to properly do:

  1. How can you glob for... "All files in the current folder, including hidden files, but not including . or .."?

  2. How can you glob for... "All hidden files (and only hidden files) in the current folder, but not including . or .."?

+1  A: 

The Bash Cookbook suggests a solution to your 2nd requirement.

.[!.]*

as a way of specifying 'dot files' but avoiding . and ..

Of course, ls has the -A option, but that's not globbing.

pavium
`touch ..blah; echo {.,}[!.]* | grep blah` fail.
ephemient
+1  A: 

To expand on paviums answer and answer the second part of your question, all files except . and .. could be specified like this:

{.[!.]*,*}

Depending on your exact use case it might be better to set the dotglob shell option, so that bash includes dotfiles in expansions of * by default:

$ shopt -s dotglob
$ echo *
.tst
sth
`{..?*,.[^.]*,[^.]*}` works regardless of `dotglob` setting and avoids missing filenames starting with two dots, which everybody here seems to have missed.
ephemient
A: 

To meet your first case:

echo {.,}[^.]*

or

echo {.,}[!.]*

Edit:

This one seems to get everything, but is shorter than ephemient's

echo {.*,}[^.]*
Dennis Williamson
`touch ..blah; echo {.,}[^.]* | grep blah` fail.
ephemient