logging recursive grep output
I have a file structure kinda like below:
Main |-Folder1 |--*.htm | |-Folder2 |--*.htm | |-Folder3 |--*.htm I want to grep the htm files and output the results to each folder like: Main |-Folder1 |--*.htm |--results.txt | |-Folder2 |--*.htm |--results.txt I can do something like "grep -r $pattern * > results.txt", but that only puts it under the Main/results.txt, and doesn't break it up. Is thara a way that each folder only has the results for the htm files in the folder? |
for loop, find and grep.
Quote:
Code:
for dir in $(find . -type d) find is a program that is used to traverse file system trees, and test each directory and/or file within that tree. Very powerful tool, little bit of a learning curve {and just a hair of understatement}. the -type argument to find will allow you to specify whether you want to test for directories ( -type d) or files (-type f). So in this case, we're looking for all sub directories under the current directory. The for loop then executes the commands between "do" and "done" for each directory. There are probably a few other ways to do this: 'find' can be used to execute commands directly, without use of the for loop, or, if you're a perlish type of person, there's a module called "File::Find" which does everything that find does, and more. If this works for you and you're interested, I would take a look at the man pages for bash (to learn about for loops), find, and xargs (which is often used in conjunction with find). |
Quote:
I didn't find anything in the help and man pages for find or for to do this. All the experiments I did trying to add double quotes failed. Is there a way to get folders with special characters to be passed? And thanks for the help already. |
Quote:
Code:
pattern="this" Be careful when making changes to this program, and use only backup copies of your data tree for testing. |
Quote:
If it's not too much trouble, I would suggest renaming folders with special characters. Also, as I mentioned in my first post, find has an option which allows you to execute code directly from the find. This can be used as a way to circumvent some issues with special characters, sometimes (I used this once to do a mass rename of a bunch of files which had single quotes in the names, which is otherwise a fairly thorny problem) The '-exec' option to find is used to execute code. your find would look like this: Code:
path="path/to/your/stuff" when you use the -exec option to find, '{}' is a placeholder for the file or directory that find is operating on. Also, the whole expression has to be followed by an escaped semi-colon. The reason that this works so well for processing special characters is that '{}' isn't expanded until find is actually processing that expression, which is well after the shell tokenizes the command. I haven't tested this myself, it may take some fiddling with to get right, and you may very well spend a lot less time renaming some directories. |
All times are GMT -5. The time now is 11:50 AM. |