search for files or directories, then show a sorted list of just the unique directories where the matches occur

for i in $(locate your_search_phrase); do dirname $i; done | sort | uniq
Ever use 'locate' to find a common phrase in a filename or directory name? Often you'll get a huge list of matches, many of which are redundant, and typically the results are not sorted. This command will 'locate' your search phrase, then show you a sorted list of just the relevant directories, with no duplications. So, for example, maybe you have installed several versions of the java jre and you want to track down every directory where files matching "java" might exist. Well, a 'locate java' is likely to return a huge list with many repeated directories since many files in one directory could contain the phrase "java". This command will whittle down the results to a minimal list of unique directory names where your search phrase finds a match.

2009-02-05 14:03:20

1 Alternatives + Submit Alt

What Others Think

You should have used "sort -u" instead of "sort | uniq".
dardo1982 · 366 weeks and 5 days ago
How can i delete a comment and change my profile picture?
dardo1982 · 366 weeks and 5 days ago
private investigator tucson I need to know more
yeoyeo · 79 weeks ago
thanks alot for this, great site! how to find a life coach
looneyhans · 65 weeks and 2 days ago
You are helping me a lot with codes. I really love to browse this site. Because all the answers to my coding doubts are available here. Please do not stop updating such useful information. I would love to check it. video conferencing installation
charlly · 51 weeks and 4 days ago
seofox · 26 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: