Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6004837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:08+00:00 2026-05-23T01:19:08+00:00

I want to write a bash command that grep s all *.txt file with

  • 0

I want to write a bash command that greps all *.txt file with a pattern in current folder to another folder. Should I use find or for loop? I tried using find but it seems to complicate things.

Edit: I want to copy files with a specific pattern to a different folder. For example:

A.txt
B.txt
C.txt

all have the word “foo” in them. I want grep to remove “foo” and send it to a different folder with the same name. I don’t want to change the original file in any way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:19:09+00:00Added an answer on May 23, 2026 at 1:19 am

    Using for would probably be a lot easier for this than find. Something like this:

    otherdir='your_other_directory'
    for file in *.txt; do
        grep -q 'foo' $file && grep -v 'foo' < $file > $otherdir/$file
    done
    

    If your grep doesn’t understand -q then:

    otherdir='your_other_directory'
    for file in *.txt; do
        grep 'foo' $file > /dev/null && grep -v 'foo' < $file > $otherdir/$file
    done
    

    In any case, grep returns a true value to the shell if it finds a match and the X && Y construct executes the Y command if X returns a true value.

    UPDATE: The above solution assumes (as noted by Johnsyweb) that you want to remove any lines that contain “foo”. If you just want to remove “foo” without removing whole lines, then sed is your friend:

    otherdir='your_other_directory'
    for file in *.txt; do
        grep -q 'foo' $file && sed 's/foo//g' < $file > $otherdir/$file
    done
    

    Or:

    otherdir='your_other_directory'
    for file in *.txt; do
        grep 'foo' $file > /dev/null && sed 's/foo//g' < $file > $otherdir/$file
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a bash script that will execute one command in the
I want to list all the file and write the list to a .txt
I want to write a bash script that (recursively) processes all files of a
I want to write a bash function that check if a file has certain
I want to write a sh/bash script that can determine whether a particular directory
I want to write a command that specifies the word under the cursor in
I tried to use the read/write file descriptor in bash so that I could
I want to write a sed filter that changes all periods in its input
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.