Linux Blog

Bash Scripting Techniques

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:20 pm on Sunday, May 18, 2008

Here are some techniques that you can use in your bash scripts for finding and searching through files. Combined with other shell scripting techniques these can be very powerful.

Find all files in the current directory and print them:

find . -iname ".jpg"

Find all files that you have access to read with matching patern:

find / -iname "pattern"

Normally with grep text is matched and is case sensitive. Heres how to do a case insensitive search with grep:

cat [filename] | grep -i [match]

Finding and replacing text is easily done in bash with sed. This find and replace puts the contents into a new file:

 cat [filename] | sed 's/FIND/REPLACE/' > [new filename]

Finding the line number that a particular line of text is on is sometimes useful. Here is how to do it:

 cat [filename] | grep -n [match]

Looping over a file in bash and echoing the output is sometimes useful for the processing of text files. Heres how to do it:

cat [filename] | while read i; do echo $i; done

Thats about all the bash scripting techniques that I can currently think of for finding in files. I know there are a ton more that I use but its hard to write them all down at once. As I come up with them or solve a problem I’ll add them here. If you have any of your own, please leave them in the comments.





Random Man Pages:
euler2d
glhanoi
gpm-types
nsswitch

3 Comments »

681

Comment by Heikki Naski

May 19, 2008 @ 2:26 am

Some people recommend that instead of this:
cat | grep -i match

you should use this:
grep -i match filename

and instead of this:
cat | sed ’s/FIND/REPLACE/’ > newfilename

use this:
sed ’s/FIND/REPLACE/’ > newfilename

It’s less typing and no need for another pipe.
I admit that I too often make the “mistake” of catting something to grep because I think that I want to put something in the stream and fish out something from there so it fits the mental pattern better.

683

Comment by Andrew

May 19, 2008 @ 10:28 am

Something like:
grep “string” file
works too, or more to my liking…to find all the files in all the subdirectories containing the string:

grep -r “string” *

684

Comment by Owen

May 19, 2008 @ 6:36 pm

Yes, those techniques also work too.

@Heikki I always get confused the other way & I’m so used to typing that its no big deal.

I forgot to add another bash scripting technique. For looping over a file list. The best way I have found to do it is:
find . -iname “*” | while read $i; do echo $i; done;

It works for looping over the current directory of files and works with spaces.

Doing a for i in $(ls); do echo $i; done; does not provide the same results.

Any one else have any scripting techniques that they like to use?

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>