Originally posted by: AmigaMan
sorry just having a frustrating day with OSX. Netbeans, Spotlight and Finder are conspiring against me. I thought searching from the command line would help, but I don't know how to do that using find or locate.
locate should work in OS X. I don't use netbeans or spotlight on my OS X systems, so I can't be of much assistance with those in particular.
😉
Basically I wanted to know common uses of find and grep. I'll search the forums though, should have thought of that first....
Anandtech search sucks.
😛
Find:
find . -name file
The "." should be the root of the filesystem you want to check, in this case everything in the current working directory down. If you want to check the entire drive it should be:
find / -name file and if you want to search your home directory:
find ~/ -name file
The -print option can be used, but it's the default behaviour for all recent implimentations of find that I am aware of (including OS X's).
To use wildcards for the name of the file I've had to encapsulate it in double quotes:
find . -name "file*". I don't know if that's necessary on all implimentations, but it doesn't break anything. I don't know how complex your regex-ing can be either, I do pretty simple searches.
The -exec option is nice, but can be slow and some people think it is difficult to understand. I think it's pretty simple though:
find . -name file -exec rm {} \; will rm any file named file found by the find command. The {}'s just mean the file that was found with the find command.
xargs is apparently something that can be handy when using find to find a large number of files, but I don't have much experience with it.
There are plenty of other options that I don't generally use. You can find some file types (directories, block devices (I think), etc.), and find based on dates. You'll have to check the man pages or google for something on that though.
Grep:
grep string file
The string will just be the string you are looking for. You can use double or single quotes if the string includes spaces (I'm not sure if escaping spaces with a back slash will work in this instance, I haven't tried). I'm not sure how complex the string can be as far as regexs are concerned, I try to keep things simple.
You can pipe input to grep also:
cat filename.txt | grep string
You can eliminate all matches to string and print everything else:
cat filename.ext | grep -v string_you_don't_want_to_see
Let me know if I didn't explain something well or if you need something I didn't get into.