Wednesday, September 12, 2012

Unix: List Files Which Do Not Have A Particular Pattern In Filename

Many a times we want to list files which do not have a particular pattern in its file name.
Here are a few things you can do (Remember not to use ls -l here):

ls |grep -v bz2 -- file name has bz2 anywhere in the file name
ls |grep -v bz2$ -- file name ends with bz2
ls |grep -v ^bz2 -- file name starts with bz2
ls |grep -v .bz2. -- file name has bz2 in the middle
ls |grep ..bz2 -- file name has atleast 2 chars before bz2
ls |grep '\.bz2$' -- file name ends with .bz2 (\ to escape)
ls |grep "\.bz2$" -- file name ends with .bz2 ("or ' might matter with the shell being used)
ls |grep b.2 -- file name can have bz2 or bx2
ls |grep '\.b.2' -- file name can have .bz2 or .bx2
ls |grep "\.b.2" -- file name can have .bz2 or .bx2

0 comments:

Post a Comment