I recently changed my emacs config to hide backup files by prepending a dot to the file name. This got me to thinking - is there a ready way to change the behavior within Bash to use a list of arbitrary globs for what files to hide? A quick scan of the bash and ls man pages didn't turn up anything.
2 Answers
ls has a --hide=PATTERN option that looks like it does what you want and can be overridden to show them with -a or -A. If you want this to happen automatically, add an alias in your ~/.bashrc (or, in the likely case that there is already an alias for it, add it to that alias).
$ touch {a,b,c}-{1,2,3}
$ ls
a-1 a-2 a-3 b-1 b-2 b-3 c-1 c-2 c-3
$ ls --hide=a*
b-1 b-2 b-3 c-1 c-2 c-3
$ ls --hide=*1
a-2 a-3 b-2 b-3 c-2 c-3
$ ls --hide=a*1
a-2 a-3 b-1 b-2 b-3 c-1 c-2 c-3
$ ls --hide=a*1 -A
a-1 a-2 a-3 b-1 b-2 b-3 c-1 c-2 c-3
There is also an --ignore=PATTERN that isn't overridden by the -a and -A options.
-
Thanks for the answer, I have just one question: why doesn't
ls --hide=*1 a*not hide anything, but still lists all threea-1 a-2 a-3? I would have expected it to hide thea-1?sdaau– sdaau2014-10-11 14:29:52 +00:00Commented Oct 11, 2014 at 14:29 -
1The
a*is expanded by the shell and includesa-1. The hide option doesn't ignore files explicitly named as arguments.Kevin– Kevin2014-10-11 16:23:19 +00:00Commented Oct 11, 2014 at 16:23
Here's my alias for ls:
alias ls="ls --color=auto --hide='*~' --hide='#*#'"
That hides files like backup.c~ and #autosave.h#. (And, opinionated as I am, I like colors, so it does that too.)
lsignore all files that start withignore-or all files that end in.bak?