3

ls -d .* lists only hidden "items" (files & directories). (I think) technically it lists every item beginning with ., which includes the current . and above .. directories.

I also know that ls -A lists "almost all" of the items, listing both hidden and un-hidden items, but excluding . and ... However, combining these as ls -dA .* doesn't list "almost all" of my hidden items.

How can I exclude . and .. when listing hidden items?

3 Answers3

3

From the second answer in:

This works on my machine (I'm not using SSH like the OP though):

ls -d .!(|.)

If there are no hidden files or directories you will get an error message:

$ ls -d .!(|.)
ls: cannot access '.!(|.)': No such file or directory

The error message occurs on directories with no hidden files because . and .. are excluded.

shopt consideration

From comments:

ls -d .[!.]* works without extglob

1

You can use any set of options, and search the output stream for a matching string or not a matching string using grep.

From grep man page:

   grep  searches the named input FILEs (or standard input if no files are
   named, or if a single hyphen-minus (-) is given as file name) for lines
   containing  a  match to the given PATTERN.  By default, grep prints the
   matching lines.

So for example if my ls -A output is:

   . .. Desktop Documents Downloads

My ls -A |grep "Do" would be:

Documents
Downloads

I can also use invert search using -v to search for anything that is not my expressions.

From grep man page:

-v, --invert-match select non-matching lines

So in your case the expression would be: ls -d .* |grep "[.][a-z]\|[0-9]"

Pizza
  • 1,512
0

You can use also find:

find . -maxdepth 1 -type f -name '.*'

which prints out full path, or:

find . -maxdepth 1 -type f -name '.*' -printf "%f\n"

This works also for empty directories / no such files.

Martin Flaska
  • 243
  • 2
  • 9