Place files that contain key phrase in sh script

Posted on 2007-10-19
Last Modified: 2010-04-20

This creates a custom script called that processes files.

      find /home/mydir/ |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" >

Now ALL files are placed in "" however I only want files to be placed in "" that contain the word "Hello World"

Question by:hankknight
    LVL 9

    Accepted Solution

    you can try to get those files that have Hello World in your "find" portion
    eg (not tested)

      find /home/mydir/  -exec grep -i "hello world" {} \; | sed ........

    something like that...
    LVL 16

    Author Comment

    Thanks, ghostdog74.

    I tested it and it does NOT work.  The problem is that your idea inserts unneeded junk. Instead of just adding the filename it adds context-- lines from the file where "Hello World" is contained.
    LVL 9

    Expert Comment

    grep has the -l option to only print out the file name and not matching lines. please check your grep's man page to see if you have such options...
    LVL 16

    Author Comment

    Thanks, this works:

    find /home/mydir -exec grep -il "Hello World" {} \; |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" >
    LVL 48

    Expert Comment

    Here's another way of doing it:

    grep -rli 'hello world' /home/mydir/*|xargs -i echo "./rmcom {} 'Hello World' >tempfile && mv tempfile {}" >

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
    How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now