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 8165033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:26:48+00:00 2026-06-06T19:26:48+00:00

I am trying to write a bash script that looks at two files with

  • 0

I am trying to write a bash script that looks at two files with the same name, each in a different directory.

I know this can be done with diff -r, however, I would like to take everything that is in the second file that is not in the first file and output it into an new file (also with the same file name)

I have written a (nested) loop with a grep command but it’s not good and gives back a syntax error:

#!/bin/bash
FILES=/Path/dir1/*
FILES2=/Path/dir2/*

for f in $FILES
do
for i in $FILES2
do 
if $f = $i
grep -vf $i $f > /Path/dir3/$i

done
done

Any help much appreciated.

  • 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-06-06T19:26:48+00:00Added an answer on June 6, 2026 at 7:26 pm

    try this

    #!/bin/bash
    cd /Path/dir1/
    for f in *; do
        comm -13 <(sort $f) <(sort /Path/dir2/$f) > /Path/dir3/$f
    done
    

    if syntax in shell is

    if test_command;then commands;fi
    

    commands are executed if test_command exit code is 0

    if [ $f = $i ] ; then  grep ... ; fi
    

    but in your case it will be more efficient to get the file name

    for i in $FILES; do
        f=/Path/dir2/`basename $i`
        grep
    done
    

    finally, maybe this will be more efficient than grep -v

    comm -13 <(sort $f) <(sort $i)
    

    comm -13 will get everything which is in the second and not in first ; comm without arguments generates 3 columns of output : first is only in first, second only in second and third what is common.
    -13 or -1 -3 removes first and third column

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a bash script that looks at a directory full of
I'm trying to write a bash script that will process a list of files
I am trying to write a simple bash script that will search for files
Im trying to write a bash script that executes several commands as different users
I'm trying to write a bash script that takes a file name, and return
I'm trying to write a simple bash script that takes two parameters and sums
I'm trying to write a script that takes two files and a number as
I'm trying to write a bash script that will recursively search a directory, find
I'm trying to write a bash script that wraps whatever the user wants to
I am trying to write a bash or PHP script that will execute a

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.