
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? 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.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Unix and Linux Help
(Article 3861)
Tagged: Previous: Google, Yahoo and MSN support "nofollow"? What's that? Next: What's podcasting? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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. Posted by: marek at October 1, 2007 3:24 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|