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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:25:30+00:00 2026-05-20T13:25:30+00:00

Okay this is about my fifth thread on the topic because I’m balls-out lost

  • 0

Okay this is about my fifth thread on the topic because I’m balls-out lost in every aspect of this horrendous assignment. I’ve used other solutions that worked perfectly but cannot be employed such as using the awk command, sed, and comm. Instead, gotta do this the hard way.

I am so lost, at this point I’m not even doing it to hand it in because it’s a zero since it’s past the due date, I’ve been tearing my hair out for the past 18 hours trying to get this done just for the sake of getting it done. Much appreciated if you can save me from putting a bullet into my head.

Point of the assignment: You are to write a Bash shell script which will help compare the contents of two directories. Write a utility that satisfies the following requirements:

  1. Your script will compare filenames
    in two directories, and list
    information about filenames that are
    in one directory but not the other.
    The information listed will be a
    long listing of each file, similar
    to the “ls -l” command. The
    directory names must be specified,
    including any required absolute or
    relative paths.
  2. Your script will work for all kinds
    of files, including directories that
    are contained in the specified
    directories. The script will print
    an appropriate error message if the
    number of arguments passed to it is
    something other than 2, or if the
    directory names specified are not
    names of valid existing directories.
    If an error message is issued, then
    the script should end with an exit
    status of 1 (one). Otherwise, it
    should end with an exit status of 0
    (zero).

Specifications:

  1. Files in the directory that this
    script is supposed to work on don’t
    have execute permissions, only read
    which if I’m not mistaken can only
    display file names, so we can’t see
    file contents. I tried, I get
    permission denied.
  2. All information regarding this
    assignment can be found here:
    https://cs.senecac.on.ca/~lczegel/BTO120/assign1/assign1.html

What I have done:

#!/bin/bash

if [ ! -d $1 ]
   then
      echo $1 is not a valid existing directory >&2
      exit 1
   elif [ ! -d $2 ]
      then
      echo $2 is not a valid existing directory >&2
         exit 1
   elif [ $# = 0 ]
      then
         echo Usage: compdir dir-name1 dir-name2 >&2
         exit 1
   elif [ $# = 1 ]
      then
         echo Usage: compdir dir-name1 dir-name2 >&2
         exit 1
   elif [ $# = 2 ]
      then

         ls -a $1 > temp1
         ls -a $2 > temp2

         cat temp1 |
         while read input
         do
            grep -Fvf temp1 temp2 > temp1_diff
         done

         cat ~temp2 |
         while read input
         do
            grep -Fvf temp2 temp1 > temp2_diff
         done

         #Files that are in $1 but not in $2
         cat temp1_diff |
         while read input
         do
            Files that are in $1 but not in $2
            cd $2
            ls -la `cat ../temp1_diff`
         done

         cd ..
         echo -e

         #Files that are in $2 but not in $1
         cat temp2_diff |
         while read input
         do
            Files that are in $2 but not in $1
            cd $1
            ls -la `cat ../temp2_diff`
         done

   elif [ $# = 3 ]
      then
         echo Usage: compdir dir-name1 dir-name2 >&2
         exit 1
   else
         echo Usage: compdir dir-name1 dir-name2 >&2
         exit 1
fi

The problems I am encountering:

  1. It will not write any files that
    have any characters like dots or
    spaces or anything in them to
    temp1_diff and temp2_diff.
  2. I cannot get the echo to echo once
    even if it’s before or in the loop.
  3. I get constant errors that it cannot
    navigate to $1 or $2 etc.
  • 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-20T13:25:31+00:00Added an answer on May 20, 2026 at 1:25 pm

    Once upon a time, there was a program called dircmp that did quite a lot of what is requested – the main difference being that it did not do ls -l format listings for files found in but not the other directory. That will require some hacking. Some time ago it became clear that dircmp was going the way of dinosaurs, but there’s enough of the palæontologist in to create a version that works for me. It will likely give you some issues if you have to deal with filenames containing blanks, tabs or newlines, but otherwise is likely to usable.

    Here is the not complete answer to your assignment, but at least a workable start point.

    #!/bin/sh
    #
    #       @(#)$Id: dircmp.sh,v 1.6 2003/03/12 08:29:13 jleffler Exp jleffler $
    #
    #       Simulation of the much-loved dircmp(1) script, with extensions.
    
    arg0=$(basename $0 .sh)
    
    error(){
            echo "$arg0: $*" 1>&2
            exit 1
    }
    
    dflag=0 # Files that are different
    mflag=0 # Files that are missing in one or the other
    sflag=0 # Files that are the same in both (or directories, or otherwise special)
    while getopts dms flag
    do
            case "$flag" in
            (d) dflag=1;;
            (m) mflag=1;;
            (s) sflag=1;;
            (*) echo "Usage: $arg0 [-dms] dir1 dir2" 1>&2; exit 1;;
            esac
    done
    shift $(expr $OPTIND - 1)
    
    # If user set no flags, set them all (traditional behaviour of dircmp).
    if [ $sflag = 0 ] && [ $dflag = 0 ] && [ $mflag = 0 ]
    then dflag=1; mflag=1; sflag=1
    fi
    
    if [ $# != 2 ]
    then echo "Usage: $arg0 [-dms] dir1 dir2" 1>&2; exit 1 
    elif [ ! -d "$1" ]
    then error "$1 is not a directory"
    elif [ ! -d "$2" ]
    then error "$2 is not a directory"
    fi
    
    tmp="${TMPDIR:-/tmp}/dc.$$"
    trap "rm -f \"$tmp\".?; exit 1" 0 1 2 3 13 15
    
    (cd "$1" 1>&2 && find . -print | sort) > "$tmp".1
    (cd "$2" 1>&2 && find . -print | sort) > "$tmp".2
    
    {
    if [ $mflag = 1 ]
    then
            comm -23 "$tmp".1 "$tmp".2 > "$tmp".3
            comm -13 "$tmp".1 "$tmp".2 > "$tmp".4 
            if [ -s "$tmp".3 ] || [ -s "$tmp".4 ]
            then
                    long=$(awk '{if(length($0) > len) { len = length($0); }}
                                    END { print 2 * len + 6; }' "$tmp".3 "$tmp".4)
                    echo "Files in $1 only and in $2 only"
                    echo
                    pr -w$long -l1 -t -m "$tmp".3 "$tmp".4
                    echo
            fi
            rm -f "$tmp".3 "$tmp".4
    fi
    
    if [ $sflag = 1 ] || [ $dflag = 1 ]
    then
            comm -12 "$tmp".1 "$tmp".2 > "$tmp".5
            if [ -s "$tmp".5 ]
            then
                    case $sflag$dflag in
                    (11) echo "Comparison of files in $1 and $2";;
                    (01) echo "Files which differ in $1 and $2";;
                    (10) echo "Files which are the same in $1 and $2";;
                    esac
                    echo
                    cat "$tmp".5 |
                    while read file
                    do
                            if [ -f "$1/$file" ] && [ -f "$2/$file" ]
                            then
                                    if cmp -s "$1/$file" "$2/$file"
                                    then [ $sflag = 1 ] && echo "same                $file"
                                    else [ $dflag = 1 ] && echo "different           $file"
                                    fi
                            elif [ $sflag = 0 ]
                            then continue
                            elif [ -d "$1/$file" ] && [ -d "$2/$file" ]
                            then echo "directory           $file"
                            elif [ -b "$1/$file" ] && [ -b "$2/$file" ]
                            then echo "block special       $file"
                            elif [ -c "$1/$file" ] && [ -c "$2/$file" ]
                            then echo "character special   $file"
                            elif [ -p "$1/$file" ] && [ -p "$2/$file" ]
                            then echo "named pipe          $file"
                            else echo "***dubious***       $file"
                            fi
                    done 
                    echo
            fi
    fi
    } |
    uniq
    
    rm -f $tmp.?
    trap 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I know I asked about this before, and the answer was basically cache
Okay, I guess this is entirely subjective and whatnot, but I was thinking about
Okay so this is another question about a previous question I asked: Rookie SQL
Okay this is a fairly broad question. This is my first App and I'm
Okay this is a real headscratcher. I have an application which calls a web
Okay this is not a question of how to get all uniques or How
Okay I have this RewriteRule which is supposed to redirect any request for the
Okay so im working on this php image upload system but for some reason
Okay, this is just a crazy idea I have. Stack Overflow looks very structured
Okay, this may be a dumb question, but I've not been able to find

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.