Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory in which lots of files (around 200) with the name temp_log.$$ are created with several other important files which I need to check.

How can I easily list out all the files and exclude the temp_log.$$ files from getting displayed?

Expected output

$ ls -lrt <exclude-filename-part>
-- Lists files not matching the above given string

I have gone through ls man page but couldn't find anything in this reference. Please let me know if I have missed any vital information here.


share|improve this question
in BASH, ls --hide='temp_log.$$' should work. You could/should also extend this with wildcards, depending which part of the whole term temp_log.$$ is. – erch Jul 23 '13 at 17:21
up vote 12 down vote accepted

With GNU ls (the version on non-embedded Linux and Cygwin, sometimes also found elsewhere), you can exclude some files when listing a directory.

ls -I 'temp_log.*' -lrt

(note the long form of -I is --ignore='temp_log.*')

With zsh, you can let the shell do the filtering. Pass -d to ls so as to avoid listing the contents of matched directories.

setopt extended_glob          # put this in your .zshrc
ls -dltr ^temp_log.*

With ksh, bash or zsh, you can use the ksh filtering syntax. In zsh, run setopt ksh_glob first. In bash, run shopt -s extglob first.

ls -dltr !(temp_log.*)
share|improve this answer

You can use grep with the option -v.

ls -lrt | grep -v <exclude-filename-part>
share|improve this answer
not sure why this was downvoted, it's the correct answer. – Tim Kennedy Oct 16 '12 at 12:21
Doing a grep on ls output is not always correct and is not recommended. It has side effects. Not able to recall exact issues. Side note - down vote was not from me ;) – mtk Oct 16 '12 at 12:28
Here's the link as to why parsing ls is a bad idea. – mtk Oct 18 '12 at 13:28

You can use find for this:

find . \! -name 'temp_log*'

This will just print the names, you can add -ls to make a ls -l style output with timestamp and permissions, or use -exec ls {} + to actually pass to ls with whatever options you want for columns, sorting, etc.

I wrote this assuming this was just files in a directory. If the directory contains other directories, you may want to avoid recursively listing them

find . \! -name 'temp_log*' -maxdepth 1

And if you use ls you'll want to pass the -d option to stop it from listing inside the directories: -exec ls -d {} +

share|improve this answer
Probably want -maxdepth 1 as well to mimic non-recursive ls. – jw013 Oct 16 '12 at 13:27
Nice. Users who have a more minimal POSIX find without -maxdepth should look here. – Wildcard May 12 at 19:52

I think you want to use a feature of the shell, not of ls. I use bash, so I checked for what you want in man bash.. Search for "^EXPANSION", (by first pressing '/'). Excerpt:-


   Expansion is performed on the command line after it has been split into words.  There are seven  kinds  of  expansion
   performed:  brace  expansion,  tilde  expansion,  parameter  and variable expansion, command substitution, arithmetic
   expansion, word splitting, and pathname expansion.

... < snip > ...
Pathname Expansion

   After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [.  If one
   of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted  list
   of  file  names  matching  the  pattern.

I've recently started excluding files based on their first letter, with a command like:

> ls ./[^t]*

This would match anything that doesn't start with the letter t. If you add more characters between the brackets, then you exclude files starting with those letters too.

Looking for a link, and I've a good one! See Pattern Matching. From there, this would work for your case:-

> ls -lrt ./!(temp_log.*)

simples :)

share|improve this answer

Just do

ls -ltr `grep -il patterntosearch  *`
share|improve this answer
grep searches for a pattern within a file, rather than in the file name. – evilsoup Jul 23 '13 at 16:53

Your Answer


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.