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.

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

2 Answers 2

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.

2
  • 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? Commented Oct 11, 2014 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. Commented Oct 11, 2014 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.)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.