I would like to list all files with extension .log except the file backup.log.

I have tried using this command:

ls *.log -I "backup.log"

But all the log files are listed, even backup.log!

How could I list all the log files except backup.log?


The shell expands the wildcard, so ls gets backup.log as one of the parameters.

Use an extended pattern (enabled by shopt -s extglob):

ls !(backup).log
| improve this answer | |
  • I did not think of this shell trick yet, simple and useful! – Jake Jul 7 '15 at 16:00
  • I just tried this and I didn't work. It lists all files except backup.log. What I want is to list all log files except backup.log. – Javier Cintrón Jul 7 '15 at 16:42
  • @JavierCintrón: Did you use !(backup).log or !(backup.log)? – choroba Jul 7 '15 at 16:45
  • 1
    Geez man :) I used ls !(backup.log). I confirm that ls !(backup).log is the solution for my problem. – Javier Cintrón Jul 7 '15 at 16:50

One possibility would be:

 find . -maxdepth 1 -mindepth 1 -name \*.log -a -not -name backup.log

i.e. find all files in the current directory or below, with an exact depth of 1 (so really only in the current directory and not the name of the current directory itself) with name matching the pattern *.log and not backup.log

| improve this answer | |
  • This is exactly what I wanted. – Javier Cintrón Jul 7 '15 at 16:49
  • But kind of complicated – Javier Cintrón Jul 7 '15 at 16:58
  • 1
    @JavierCintrón: yes, but once you want to do anything with the files, it's easier to find -exec than to worry about parsing the output of ls, which you cannot usually do in a safe manner. – Ulrich Schwarz Jul 7 '15 at 18:04
  • You have the reason man. In my case I just needed to count the amount of log files except backup.log, which I did using this command: ls -laht !(backup).log | wc -l. If I had needed to do anything with these files inside a loop, I would have used your solution. – Javier Cintrón Jul 7 '15 at 22:38
  • Well, the point where things get ugly is when you have spaces or newlines in file names, which throws off wc -l. – Ulrich Schwarz Jul 8 '15 at 5:24


$ ls *.patch
install-tests.patch  match.patch  netbsd.patch  specials.patch  wlist.patch
$ txr -t '(glob "*.patch")'
$ txr -t '(set-diff (glob "*.patch") (glob "[mn]*.patch"))'
| improve this answer | |

you can skip Backup keyword from shell expansion by using !(keyword)

ubuntu@vm-ubuntu:~$ ls *.log

1.log  2.log  backup.log

ubuntu@vm-ubuntu:~$ ls  !(backup).log

1.log  2.log
| improve this answer | |

What about parsing the output of ls through grep:

ls -l *.log | grep -v backup.log
| improve this answer | |
  • 1
    What if you have symlinks pointing to backup.log? How about other files with names ending in backup.log? – lcd047 Jul 7 '15 at 15:44
  • 1
    ... or even containing backup.log? – G-Man Says 'Reinstate Monica' Jul 7 '15 at 15:44

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.