Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
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 at 14:29
    
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 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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