Cuma, Şubat 24, 2012

Birden fazla dosya uzantısını ile arama yapmak ve bunlarla işlem yapmak

How to find multiple file types using linux's "find"
I've always found *nix's "find" and "grep" rather hard to use. Not only are there different flavors of regular expressions to use, but mainly different syntax. For find, the directory you're searching for comes first. For grep, it comes last. To find the negation of something, you'd use "-not" and for grep it's "-v". pain.

Anyway, been trying to learn my tools better, and I found out how to grep and replace expressions across multiple files through emacs. Since rails uses all sorts of file extensions, naturally, I wanted to grep for find different files types. I had thought the -name options took regexs (it doesn't), so I had tried it in regex (no go)...only to find that it's something like this:


find . -name "*.rb" -o -name "*.rhtml"


the -o is the equivalent of a boolean "or". small tip...




------------


I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:

./file.php
./includes/test.php
./classes/class.php

xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit. In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.



Reply
JohnMay 29, 2008 10:17 AM

You can always use the -regextype to make regex play nice. The problem with using -o is that if you want to pass all of your results to a program as a list with -exec you can't because there needs to be a seperate -exec before each -o.

For instance I wanted to search my music directory or videos and pass them all to mplayer to be played in a random order but I had multiple file types in multiple locations, this is what I came up with:

find ./ -regextype posix-awk -regex "(.*.mpg|.*.avi|.*.wmv|.*.mpeg)" -exec mplayer -shuffle -loop 0 {} +

Hiç yorum yok: