• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need Linux/Unix command line help

AmigaMan

Diamond Member
I've been using Linux and OS X heavily for the past couple of years now but never bothered to learn how to search files via the command line. So how do you do the following:

1) search a directory for files matching a wildcard or regex pattern?

2) search a directory for files which contain a certain string?

Are there any other command line tips some of you other developers might know?
 
Originally posted by: AmigaMan
I've been using Linux and OS X heavily for the past couple of years now but never bothered to learn how to search files via the command line. So how do you do the following:

1) search a directory for files matching a wildcard or regex pattern?

find.

2) search a directory for files which contain a certain string?

grep.
 
I was hoping for something better in this forum than the typical cynical, 12-year old response of ATOT, guess I was wrong. Fine, I'll go RTFM.
 
Originally posted by: AmigaMan
I was hoping for something better in this forum than the typical cynical, 12-year old response of ATOT, guess I was wrong. Fine, I'll go RTFM.

I've posted info on both of these commands in the past. Including examples.

What do you need me to type that isn't covered in the man pages or you don't understand having read the man pages? What kinds of examples can I give you? 🙂
 
Just do a 'find --help' and 'grep --help' and you should get the basic syntax. For find I think it's 'find path wordorfolderyouaresearchingfor whatyouwantfindtodo' So it could be 'find /home MyDocs -print' Default is the current folder and default output is -print (display on the screen). Hope that makes sense.
 
Originally posted by: n0cmonkey
Originally posted by: AmigaMan
I was hoping for something better in this forum than the typical cynical, 12-year old response of ATOT, guess I was wrong. Fine, I'll go RTFM.

I've posted info on both of these commands in the past. Including examples.

What do you need me to type that isn't covered in the man pages or you don't understand having read the man pages? What kinds of examples can I give you? 🙂

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.
Basically I wanted to know common uses of find and grep. I'll search the forums though, should have thought of that first....
 
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.
 
Originally posted by: n0cmonkey
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.
As complex as you want if you go with egrep or grep -e.
Originally posted by: n0cmonkey
Originally posted by: AmigaMan
I've been using Linux and OS X heavily for the past couple of years now but never bothered to learn how to search files via the command line. So how do you do the following:

1) search a directory for files matching a wildcard or regex pattern?

find.
I've always been too lazy to learn find properly so ls | grep pattern works fine for simple stuff (ls -R should give recursion, although I don't know if that prints path names too, no box to test on atm).
 
Originally posted by: kamper
I've always been too lazy to learn find properly so ls | grep pattern works fine for simple stuff (ls -R should give recursion, although I don't know if that prints path names too, no box to test on atm).

That doesn't seem to work properly on all platforms.
 
Hmm, well it's in both the OpenBSD and os x manpages. I can't imagine that a gnu tool would actually have less functionality. Different flag maybe? What platforms are you thinking of?

Anyway: don't be lazy. Learn find. 🙂 :sigh;
 
Originally posted by: kamper
Hmm, well it's in both the OpenBSD and os x manpages. I can't imagine that a gnu tool would actually have less functionality. Different flag maybe? What platforms are you thinking of?

My mistake, it works now that I'm typing the right command in. No path though.
 
Back
Top