10

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.

  • You mean, e.g. have ls ignore all files that start with ignore- or all files that end in .bak? – Kevin Dec 15 '11 at 15:54
  • Yes, preferably with the ability to do wildcard pattern matching as well, ignore-*.bak, etc – Tim Brigham Dec 15 '11 at 16:02
9

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 three a-1 a-2 a-3? I would have expected it to hide the a-1? – sdaau Oct 11 '14 at 14:29
  • 1
    The a* is expanded by the shell and includes a-1. The hide option doesn't ignore files explicitly named as arguments. – Kevin Oct 11 '14 at 16:23
0

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.