2

I would like to use an ls alias (like l.) to output a colored list, consisting of only hidden files & directories, whether or not they begin with a dot. Any suggestions?

Example of desired results...

## Listing all contents of $PWD
$ ls -AF
file                                directory/
.dot-file                           .dot-directory/
hidden-file                         hidden-directory/

## Listing all hidden contents of $PWD, using alias `l.`
$ l.
.dot-file                           .dot-directory/
hidden-file                         hidden-directory/

Research...

I've seen answers to similar questions. These answers are awesome, but don't quite accomplish what I'm asking because:

DETAILS

## Current `ls` alias
alias ls="ls -h ${COLORFLAG}"

I use both bash & zsh.

1 Answers1

2

This will use the find command to retrieve dot files and files with the "hidden" flag set.

The matching files are fed as an argument list into ls via sed (to remove the "." result as well as leading "./" prefixes) and xargs. This allows for the specification of additional ls parameters (e.g. -l).

alias l.="find . \( -flags +hidden -or -name '.*' \) -maxdepth 1 | sed 's/^\.\/*//' | xargs ls -d"

The whole construct is designed to list the current directory only (-maxdepth 1 parameter to find); if this is not desired, a conditional would need to be introduced to check for a -R option and suppress the maxdepth option to find accordingly.

Example:

$ ls -alO
total 8
drwxr-xr-x  9 guido  staff  -      306 Apr 13 22:43 .
drwxr-xr-x+ 6 guido  staff  -      204 Apr 13 19:00 ..
drwxr-xr-x  2 guido  staff  -       68 Apr 13 22:43 .hiddendir
-rw-r--r--  1 guido  staff  -        0 Apr 13 21:48 .xx
-rw-r--r--  1 guido  staff  -        0 Apr 13 21:17 file
-rw-r--r--  1 guido  staff  -       18 Apr 13 21:03 file.b
drwxr-xr-x@ 2 guido  staff  hidden  68 Apr 13 22:43 hidden2
-rw-r--r--@ 1 guido  staff  hidden   0 Apr 13 22:35 hide
drwxr-xr-x  2 guido  staff  -       68 Apr 13 21:51 tst

$ l.
.hiddendir  .xx     hidden2     hide

$ l. -alO
drwxr-xr-x  2 guido  staff  -      68 Apr 13 22:43 .hiddendir
-rw-r--r--  1 guido  staff  -       0 Apr 13 21:48 .xx
drwxr-xr-x@ 2 guido  staff  hidden 68 Apr 13 22:43 hidden2
-rw-r--r--@ 1 guido  staff  hidden  0 Apr 13 22:35 hide
Guido
  • 4,114