Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do I list files that don't match a pattern?

Dave, I have a straightforward Linux question that's got me stumped. I have a directory with lots of files that include an underscore, but I want to produce a listing of files that don't have the underscore, not those that do have it. I can get it with a combination of ls, grep -v and such, but it's painful and convoluted. Is there a simpler solution?


Dave's Answer:

In an interesting bit of sychronicity, a similar question arose on a mailing list I'm involved with, where the writer wanted to know how to list the opposite of a given pattern.

It turns out that there's a fairly straightforward solution if you're willing to delve into the inner workings of the all-powerful find command. If you've used find, you probably use it to look for files that are greater than a certain size, have a specific filename pattern, or have a specific permission setting. And yet, find can do so much more...

I won't get too sidetracked showing some of the great capabilities of find herein, so let's focus on your specific problem.

In the jargon of the find command, statements are comprised of primaries and operands. Want to match a specific filename pattern? Use -name "pattern". Here's the magic, though: want to negate the primary? Just preface it with -not.

Put these together and here's the easy way to find all files that don't have an underscore:

find -not -name "*_*" -print

This is a bit too broad, actually, because I presume that you don't want to match directories if you can help it. This is done by adding the primary -type f. If you'd like to further constrain your search results to just those files in the current directory (remember, find likes to traverse entire file trees, not just the current directory) you can add -maxdepth 1. Change the "1" to a "2" and you'll get only the current directory and matches one level deeper.

Put it all together and even toss in a second filename conditional to screen out files with dashes in their names and here's what you end up with:

find . -not -name "*_*" -and -not -name "*-*" \
  -maxdepth 1 -type f -print

Experiment with this command and some of the further constraints you can put on it by using various find primaries, and I think you'll be suitably impressed!

And for those of you that are Mac OS X folk, I'll just share the tip that the command line interface to Tiger's Spotlight feature offers even more power and capabilities for identifying individual files across the file system.


Related Unix and Linux Help articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Unix and Linux Help   (Article 3861, Written by )
Tagged:
Previous: Google, Yahoo and MSN support "nofollow"? What's that?
Next: What's podcasting?




Reader Comments To Date: 5

marek said, on October 1, 2007 3:24 AM:

Hi,

thanks for this tip, it's exactly what I was looking for! (using 'find' to list only files that don't match certain permissions) Great.

John Dondapati said, on April 1, 2009 1:57 PM:

Awesome!

Dude, I've been googling for over an hour to get this right. YOU ARE THE MAN!

Thank you so much!

Ramya said, on July 29, 2010 12:22 PM:

Thank you so much. The "-not" really helped me out.

andy said, on October 12, 2010 11:09 AM:

Hi!, Does anybody knows how to list files that match a particular pattern withing the files itself, not in the name of file?

Dave Taylor said, on October 12, 2010 11:22 AM:

Andy, that's what "grep" is for. As an example, say you wanted to list all files that contained the word "tattoo":

grep -l tattoo *

(that's a lower-case L, not an I)

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.