List files not matching given string in filename

filterls

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.

Thanks

Best Answer

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.*)
Related Question