Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Find anything and open it with one command UNIX
I have found this find command useful to find anything in a selected directory and open the matching items with the application set to open those file types. It will look at file names and the contents of the file in order to find matches:

sudo find /path/to/dir_to_search -type f -exec grep -i -q stuff_to_find "{}" \; -exec open "{}" \;

Replace /path/to/dir_to_search with the full path to the directory in which you'd like to search, and replace stuff_to_find with the string you wish to match.

[robg adds: This works, but be careful with what you're searching for; if it appears in a lot of files of different types, your machine could be busy for quite a while, launching apps and opening files! You don't necessarily need sudo, if you're searching within a directory you have full rights to use. Also note that if what you're searching for contains a space, you'll have to do some work with quoting and backslashes to capture it properly (examples in comments are welcomed!). If you want to simply run the command at or below the current directory, replace the /path/to/dir_to_search bit with a single period (.); this will search at and below the current directory.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[4,893 views]  

Find anything and open it with one command | 18 comments | Create New Account
Click here to return to the 'Find anything and open it with one command' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Find anything and open it with one command
Authored by: mankoff on Apr 14, '05 10:34:51AM

If you know what your are looking for is uniquely named, you can shorten the above command to:

open `locate unique_foo`


---
http://spacebit.dyndns.org

[ Reply to This | # ]

Find anything and open it with one command
Authored by: lazybone on Apr 14, '05 07:50:42PM

First, 'locate' will not look at file contents.

Secondly it depends on the GNU find database being up to date. The update gets run nightly (typically) and might not have run on a powerbook that is usually asleep or turned of at night.



[ Reply to This | # ]
for this problem...
Authored by: wallybear on Apr 18, '05 05:36:46AM
...you can use PseudoAnacron, a free utility that checks at startup which periodic tasks you have skipped and launches them.

Periodic tasks are really important to keep your system clean and synchronized. You can also install Anacron, but PseudoAnacron (a Startup item) is not so invasive and will suffice.


[ Reply to This | # ]
shorter version
Authored by: stephendv on Apr 14, '05 10:40:09AM
You could also use a shortened version which requires less keyboard gymnastics. For example to open all .java files in a specific folider:
open `find /my/java/src/files -name *.java`
or to search the current folder:
open `find . -name *.java`


[ Reply to This | # ]
That doesn't work with certain characters
Authored by: lullabud on Apr 14, '05 01:46:57PM

That only works with really simple filenames, and does not include spaces and other special characters. The original find command gracefully handles the special characters.



[ Reply to This | # ]
Find anything and open it with one command
Authored by: luhmann on Apr 14, '05 11:34:43AM

Another option:

Wait for Tiger and then use Spotlight!



[ Reply to This | # ]
Find anything and open it with one command
Authored by: bmaxwell on Apr 14, '05 12:33:37PM

The reply tips will do for searching file names. However the command in the tip will search the file contents as well as the file names.



[ Reply to This | # ]
Concerning spaces
Authored by: lullabud on Apr 14, '05 01:36:00PM
You can use double-quotes around your search-string if you have to use spaces or other strange characters like single quotes, etc. The syntax used here used throughout a wide array of *nix applications. If you become familiar with it you will be much more at home in the terminal. You can als- put a back-slash before a space. The following commands do the same thing.

find "/some/path name/with spaces/" -type f -exec grep -i -q "spaced string within file" "{}" \; -exec open "{}" \;

find /some/path\ name/with\ spaces/ -type f -exec grep -i -q spaced\ string\ within\ file "{}" \; -exec open "{}" \;


The first exmple can be demonstrated by typing open " and then using tab-completion to finish up a filename that has spaces. The second can be demonstrated by dragging a filename with spaces onto the terminal window.

[ Reply to This | # ]
Syntax Mistake in example
Authored by: lullabud on Apr 14, '05 02:04:22PM
Gah, I don't know what happened but those semi-colons need to be back-slashed in the -exec command.

find /some/path/ -name '*.html' -exec open "{}" \;

[ Reply to This | # ]
Find anything and open it with one command
Authored by: oink on Apr 14, '05 02:45:37PM

Since there are "find" experts here. A while back, I was trying to do a find and move the found files up a level (the parent's directory) but couldn't do it. -exec mv {} .. \; didn't work. Any advice?

---
blurred visionary



[ Reply to This | # ]
Find anything and open it with one command
Authored by: fracai on Apr 14, '05 03:40:44PM
the following will find all files in the current directory to the directory above the current.
files in directories below the current will not be affected and will print errors.
find ./ -type f -exec mv {} ../{} \;

---
i am jack's amusing sig file

[ Reply to This | # ]

Find anything and open it with one command
Authored by: oink on Apr 14, '05 11:09:37PM

Thank you for trying, but that doesn't solve the problem. That command will move the found files to the parent of the current working directory (./) not the parents of the found files. Example move /dir1/dir2/dir3/file to /dir1/dir2/file

---
blurred visionary



[ Reply to This | # ]
Find anything and open it with one command
Authored by: kps on Apr 15, '05 07:06:37PM
find . -type f -execdir mv {} .. \;

[ Reply to This | # ]
Find anything and open it with one command
Authored by: oink on Apr 16, '05 08:37:00AM

THANK YOU. I learned about execdir from the man page, but it didn't work for me as expected, your example works perfectly. Gotta remember that.

---
blurred visionary



[ Reply to This | # ]
try this
Authored by: SOX on Apr 14, '05 03:33:04PM

Hey this crashes my computer:

open `find / -name *`

(Dont try it!)



[ Reply to This | # ]
Find is unnecessary, use grep -r
Authored by: fungus on Apr 14, '05 03:58:15PM
You don't need to use find at all in this example. grep -r can recursively search folders all by itself.
grep -r -i -l -q "stuff to find" /path/to/dir_to_search | xargs open
-r recursively search all directories
-i case insensitive searching
-l just list the filenames that match
-q stop searching the file after the first match (faster)

Note: be careful with your search pattern stuff to find as open could launch many many files.
You can also shorten it to:
grep -rilq "stuff to find" /path/to/dir | xargs open

[ Reply to This | # ]
Find is unnecessary, use grep -r
Authored by: lazybone on Apr 14, '05 07:47:46PM

Two reasons why I like the original command better:

1.) Not really valid here since we're talking about OS X here: "grep -r" is not guaranteed to be available on other flavors of unix. So I don't like to rely on it's availability

2.) This one is a major one for me: When filenames contain characters that do funky stuff (international) or need to be escaped (like blanks), xargs will miserably fail, while the original command will still work.



[ Reply to This | # ]
Find is unnecessary, use grep -r
Authored by: kps on Apr 15, '05 07:32:11PM
Tip: instead of find -print | xargs, use find -print0 | xargs -0; then blanks and special characters are not a problem.

[ Reply to This | # ]