ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello all,
new Bash programmer here. I've dealt with other languages in the past, but I honestly think that's not to my advantage right now.
GOAL:
Starting in a directory, search it and all sub directories for any files containing a certain text string, then copy those files to another directory. There's about 1GB worth of files (all email messages) I want to search through.
PROBLEM:
Since I don't know the depth of the folders, my instinct was to create a recursive script. Unfortunately, I have to run this on a server... and I'm guessing it'll completely kill everything.
I'm hoping there's a cleaner solution?
Code:
#!/bin/bash
#navigate to the backups
cd /mnt/home/user/.mailbox/
for f in *; do
if [ -f "$f" ]; then # it was a file
#here is where I'd check for the string
#if it matched, I'd copy it to the directory
else # it was a folder
cd "$f"
/bin/emailSearch.sh # the script calls itself in the new subdirectory
cd ../
fi
done
Something like this will do. If you are expecting to match a large volume of files, you may want to look at piping find into xargs rather than using -exec.
doesn't find -iname just search for the file name? I need to check text inside the file itself, or perhaps I'm reading the man wrong..?
The check is rather complicated, too. If any part of the "to" field matches three or more of a set of 10 users, it's considered a hit. I can use regular expressions to accomplish this, but again I don't think find can do this inside of a file.
You could run the script via the nice command to reduce its impact on the server and if execution time is not important you could add a sleep command in the script too.
You could use the file command to restrict the search to appropriate file types.
You could use find to run the script in each directory but that gains little over recursion.
In essence, what I did was:
1) run a find to get a list of all files in the applicable folders (current problem, see other post) allFiles=$(find $src -print)
2) grep through the files to see if they had the string I wanted, and save the file paths of the matches to a variable files=$(grep -il -E "^To:.*($bod|$admin)" $allFiles)
3) iterate through the matches to modify the files, for f in $files; do
...
4) then copy them over cp "$fSrc" "$dest$fName"
Thanks again! I've learned a ton so far because of this little project!
while IFS="" read -r -d "" file ; do
if grep -iq -E "^To:.*($bod|$admin)" "$file ; then
cp ...
fi
done < <(find "$src" -print0)
As someone pointed out in another thread (CTOP) there is no need for the IFS="" when each line is being read into a single variable, in this case $file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.