compare the contents of two directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare the contents of two directories
# 1  
Old 11-03-2009
compare the contents of two directories

i have been asked to write a bash shell script comparing two directories and sed or awk should not be used in this assignment. compdir 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.

Code:
Usage:    compdir   dir-name1   dir-name2

compdir 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).

Here are examples of compdir error messages (directory d1 exists, d2 and d3 do not exist):
Code:
$ compdir
Usage: compdir dir-name1 dir-name2
$ compdir d1
Usage: compdir dir-name1 dir-name2
$ compdir d1 d2 d3
Usage: compdir dir-name1 dir-name2
$ compdir d1 d2
d2 is not a valid existing directory
$ compdir d2 d1
d2 is not a valid existing directory
$ compdir d2 d3
d2 is not a valid existing directory
$ compdir d3 d2
d3 is not a valid existing directory

anyone has any tips on how i should do this. im a begginer in scripting. thanks.

Last edited by pludi; 11-03-2009 at 05:33 PM.. Reason: code tags, please...
# 2  
Old 11-03-2009
Homework? If so, please post again in the appropriate sub forum.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Clear contents of specified directories, then return exit status

Hello, this is my first post here. I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have: function clear() { USER="$USER" DIR=$1 rm -rfv /Users/"$USER"/library/$DIR/* } clear... (6 Replies)
Discussion started by: YouNicks
6 Replies

2. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

3. Shell Programming and Scripting

Compare contents of two files

Hello, I have two files containing both two columns: $ cat file1 col1 nbr1 col2 nbr2 col3 nbr3 ... $ cat file2 val1 ri1 val2 ri2 val3 ri3 ... I need to compare every nbr (nbr1, nbr2,...) in the second column of file1 with every ri (ri1, ri2,...) in the second column of... (5 Replies)
Discussion started by: newpromo
5 Replies

4. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

5. Shell Programming and Scripting

Compare folder contents over network

I use diff -r dir1 dir2 to get comparison of two folders that are on same machine. Now I need the same thing but one of the folders is on a different machine. Currently I ftp the folder to a temp folder compare using above command and delete the temp folder. Is there any other better options?... (5 Replies)
Discussion started by: ke3kelly
5 Replies

6. UNIX for Dummies Questions & Answers

Help displaying contents of Directories - Urgent

I am new to shell programming and have an assignment question which requires me to list the contents of the present working directory in 4 column format and highlight any subdirectories. It then requires me to develop the shell script to accept a directory name as a positional parameter (if no... (1 Reply)
Discussion started by: cjnd1988
1 Replies

7. Shell Programming and Scripting

How to compare contents of two CSV rows

I have been trying to find the answer to this but can't seem to get it. I have two rows, both are CSV files. I want to iteratively compare each field value to the corresponding value in the second file. Example: File 1.txt 1|hello|3.2|AMB||||B File 2.txt 1|hi|3,2|ABC||||C The... (1 Reply)
Discussion started by: axwack
1 Replies

8. Shell Programming and Scripting

Beset Scripting langauge to delete old directories and contents?

What would the best scripting language be for selecting all the directories in the current directory that are over a week old and deleting there contents? Perl? bash? groovy? ruby? something else? How would you do it? I got a start with perl in the beginners@perl.org and I started to think:... (3 Replies)
Discussion started by: siegfried
3 Replies

9. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

10. UNIX for Dummies Questions & Answers

Delete Directories and Contents

How to delete all subdirectories and contents? oh, and if they are hard linked? (1 Reply)
Discussion started by: t4st33@mac.com
1 Replies
Login or Register to Ask a Question