Shell scripting for duplicate files

Shell scripting for duplicate files

Post by Unicorn66 » Sun, 02 Feb 2003 07:44:51



How does one write a shell script that will compare the files in each of
two directories specified on the command line. The script must produce a
list of all files with identical names and indicate which of each pair
is the newest.

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by William Par » Sun, 02 Feb 2003 14:20:28



> How does one write a shell script that will compare the files in each of
> two directories specified on the command line. The script must produce a
> list of all files with identical names and indicate which of each pair
> is the newest.

I assume that you are comparing the file names, not the file content.
    cd dir1
    for i in *; do
        if [ -e dir2/$i ]; then
            if [ $i -nt dir2/$i ]; then
                echo "$i is newer than dir2/$i"
            fi
        fi
    done
--

Linux solution for data management and processing.

 
 
 

Shell scripting for duplicate files

Post by Tim Cargi » Sun, 02 Feb 2003 16:38:41



> How does one write a shell script that will compare the files in each of
> two directories specified on the command line. The script must produce a
> list of all files with identical names and indicate which of each pair
> is the newest.

Which shell?
 
 
 

Shell scripting for duplicate files

Post by Tim Cargi » Sun, 02 Feb 2003 19:52:28




> > How does one write a shell script that will compare the files in each of
> > two directories specified on the command line. The script must produce a
> > list of all files with identical names and indicate which of each pair
> > is the newest.

> I assume that you are comparing the file names, not the file content.
>     cd dir1
>     for i in *; do
>    if [ -e dir2/$i ]; then
>        if [ $i -nt dir2/$i ]; then
>            echo "$i is newer than dir2/$i"
>        fi
>    fi
>     done

Great solution, Mr. Bill!  It has only a few minor flaws and
oversights:

   1) It doesn't take dir1 and dir2 as arguments, as specified.
   2) It does not display/compare 'invisible' files.
   3) It doesn't unconditionally display any identical files, as specified.
   4) It doesn't handle the condition where the times are identical -
      probably a flaw in the 'homework' problem itself.
   5) The interpreter is not specified by a "#!" line.
   6) It assumes a Bourne family shell although the desired
      implementation shell was not specified.
   7) It does not answer the OP's question of 'How does one write a shell
      script ...'.[sic]  Instead, it actually submits an attempt at
      such a script.

But then again, we wouldn't want to do the 'entire' problem correctly
(especially) for someone with an email ID of 'unicorn666' ... now would we?

;-)

Regards,

Tim

 
 
 

Shell scripting for duplicate files

Post by Unicorn66 » Sun, 02 Feb 2003 20:33:01


Originally posted by Tim Cargile


> > How does one write a shell script that will compare the files in
>     each of
> > two directories specified on the command line. The script must
>     produce a
> > list of all files with identical names and indicate which of
>     each pair
> > is the newest.

Which shell?  The shell is bash and dir1 and dir2 are
taken as arguments - thanks

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by Unicorn66 » Sun, 02 Feb 2003 20:57:22


Originally posted by William Park


> > How does one write a shell script that will compare the files in
>     each of
> > two directories specified on the command line. The script must
>     produce a
> > list of all files with identical names and indicate which of
>     each pair
> > is the newest.

> I assume that you are comparing the file names, not the file content.
>     cd dir1
>     for i in *; do
>       if [ -e dir2/$i ]; then
>           if [ $i -nt dir2/$i ]; then
>               echo "$i is newer than dir2/$i"
>           fi
>       fi
>     done
> --
> William Park, Open Geometry Consulting,
> Linux solution for data management and processing.

Yes I'm comparing file names not file content, also I forgot to
say the shell
is bash and the directories are passed in as arguments -thanks

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by Unicorn66 » Sun, 02 Feb 2003 23:49:31



     > >
     > > How does one write a shell script that will compare the files
     > > in each of
     > > two directories specified on the command line. The script must
     > > produce a
     > > list of all files with identical names and indicate which of
     > > each pair
     > > is the newest.
  >
  > I assume that you are comparing the file names, not the file
  > content.
  > cd dir1
  > for i in *; do
  > if [ -e dir2/$i ]; then
  > if [ $i -nt dir2/$i ]; then
  > echo "$i is newer than dir2/$i"
  > fi
  > fi
  > done

Great solution, Mr. Bill! It has only a few minor flaws and
oversights:

1) It doesn't take dir1 and dir2 as arguments, as specified.
2) It does not display/compare 'invisible' files.
3) It doesn't unconditionally display any identical files, as specified.
4) It doesn't handle the condition where the times are identical -
probably a flaw in the 'homework' problem itself.
5) The interpreter is not specified by a "#!" line.
6) It assumes a Bourne family shell although the desired
implementation shell was not specified.
7) It does not answer the OP's question of 'How does one write a shell
script ...'.[sic] Instead, it actually submits an attempt at
such a script.

But then again, we wouldn't want to do the 'entire' problem correctly
(especially) for someone with an email ID of 'unicorn666' ... now would
we?

;-)

Regards,

Tim

I forgot to say it is the bash shell so there will be

#!include /bin/bash

and dir1 and dir2 will be passed as arguments
so does this mean "cd dir1" will be replaced by
"cd $1"  then for the line "if [ -e dir2/$i ]; then", I assume one
would have to change to dir2 first, so would that be   "cd.." followed
by "cd dir$2"
- is there a special way to write the UNIX commands inside the
  shell script,
sorry but this is the first time I've done anything in UNIX
- thanks

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by Tim Cargi » Mon, 03 Feb 2003 09:21:38


[ ... deleted ... ]

Quote:

> I forgot to say it is the bash shell so there will be

> #!include /bin/bash

man include ?!?!

Quote:

> and dir1 and dir2 will be passed as arguments
> so does this mean "cd dir1" will be replaced by
> "cd $1"  then for the line "if [ -e dir2/$i ]; then", I assume one
> would have to change to dir2 first, so would that be   "cd.." followed
> by "cd dir$2"

That's in the ballpark, but out in left field - in baseball terms,
that is.

Quote:> - is there a special way to write the UNIX commands inside the
>   shell script,

Yes.  Using shell syntax and UNIX command and arguments.

Quote:> sorry but this is the first time I've done anything in UNIX

    http://www.gnu.org/manual/bash/html_chapter/bashref_toc.html

Quote:> - thanks

The least I could do.  :-)

Tim

 
 
 

Shell scripting for duplicate files

Post by William Par » Mon, 03 Feb 2003 10:56:34





>> > How does one write a shell script that will compare the files in each of
>> > two directories specified on the command line. The script must produce a
>> > list of all files with identical names and indicate which of each pair
>> > is the newest.

>> I assume that you are comparing the file names, not the file content.
>>     cd dir1
>>     for i in *; do
>>       if [ -e dir2/$i ]; then
>>           if [ $i -nt dir2/$i ]; then
>>               echo "$i is newer than dir2/$i"
>>           fi
>>       fi
>>     done

> Great solution, Mr. Bill!  It has only a few minor flaws and
> oversights:

>   1) It doesn't take dir1 and dir2 as arguments, as specified.
>   2) It does not display/compare 'invisible' files.
>   3) It doesn't unconditionally display any identical files, as specified.
>   4) It doesn't handle the condition where the times are identical -
>      probably a flaw in the 'homework' problem itself.
>   5) The interpreter is not specified by a "#!" line.
>   6) It assumes a Bourne family shell although the desired
>      implementation shell was not specified.
>   7) It does not answer the OP's question of 'How does one write a shell
>      script ...'.[sic]  Instead, it actually submits an attempt at
>      such a script.

> But then again, we wouldn't want to do the 'entire' problem correctly
> (especially) for someone with an email ID of 'unicorn666' ... now would we?

> ;-)

> Regards,

> Tim

We can only show him the way.  Let him do the walking (or homework).

--

Linux solution for data management and processing.

 
 
 

Shell scripting for duplicate files

Post by Tim Cargi » Mon, 03 Feb 2003 11:19:09



> Originally posted by William Park


[ ... deleted ... ]

Quote:

> Yes I'm comparing file names not file content, also I forgot to
> say the shell
> is bash and the directories are passed in as arguments -thanks

File names were implcit.  Do you know why?
 
 
 

Shell scripting for duplicate files

Post by Unicorn66 » Mon, 03 Feb 2003 11:39:49


Hello
I wish to thank everybody for their help. I've got enough info to finish
this on my own. So thanks again and have a good weekend

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by Unicorn66 » Mon, 03 Feb 2003 13:07:26


Originally posted by Tim Cargile


> > Originally posted by William Park

> [ ... deleted ... ]

> > Yes I'm comparing file names not file content, also I forgot
>     to
> > say the shell
> > is bash and the directories are passed in as arguments
>     -thanks

> File names were implcit.  Do you know why?

I guess its because the command
"for i in *; do "  each "i" or should I say "$i" represents the value of
the filename, and the arguments dir1 and dir2 are $1 and $2 (input
parameters) but if I'm wrong tell me. -thanks

--
Posted via http://dbforums.com

 
 
 

Shell scripting for duplicate files

Post by laura fairhe » Mon, 03 Feb 2003 14:36:15




Quote:

>How does one write a shell script that will compare the files in each of
>two directories specified on the command line. The script must produce a
>list of all files with identical names and indicate which of each pair
>is the newest.

Hiya,

One way you can do this is to use a 'for' loop over all the files in
directory1, strip the basename of the file and then check if it exists
and is a file in directory2. If that is true then a newer-than test can
be done using the '-newer' option of the find command ( this could be
replaced by a test command if your shell supports that test however many
do not, check your shells man pages for details... )

The following is a possible implementation that uses that method. It
uses the standard POSIX ${var##word} parameter substitution to find
the basename of the file by string manipulation. This is present in any
POSIX compliant shell, however if your shell does not support this extension
you may replace that with the 'basename' command, or possibly with a 'sed'
command. Another small point is that if the directory tested in the 'find'
command (directory 1) used a hyphen as the first character, it may interfer
with the normal operation of 'find' -- being interpretted as an operator
instead. For that reason the script prefixes a relative directory path
with a './' before using it with 'find'.

#!/bin/sh

# cdir <directory1> <directory2>
# list files that have the same name in directory1 and directory2
# output in 2 space seperated fields with the newest filename first

dir1=$1; dir2=$2

# prefix relative path in $dir1nh=$dir1 with ./
# to stop any initial hyphen from messing up the find command later
# original $dir1 is kept so that we can print out that form

dir1nh=$dir1
case "$dir1nh" in
[!/]*) dir1nh="./$dir1nh";;
esac

for f in "$dir1"/*
do
  if [ -f "$f" ]; then
    fb=${f##*/}
    if [ -f "$dir2/$fb" ]; then
      if [ -n "$(find "$dir1nh/$fb" -prune -newer "$dir2/$fb")" ]; then
         echo "$dir1/$fb $dir2/$fb"
      else
         echo "$dir2/$fb $dir1/$fb"
      fi
    fi
  fi
done

~

bestwishesfrom
laura

--
alt.fan.madonna |news, interviews, discussion, writings
                |chat, exchange merchandise, meet fans....
                |Get into the groove baby you've got to... check us out!

 
 
 

1. Copy files using filenames from text files with shell script or bash script

How can I do this????

I have the file "lista.txt", and this file have the next information:
     one.txt
     two.txt
     five.txt
    six.txt
-------------------
This files exist in the same directory that "list.txt", I would like to
copy this files to other directory

Using Windows the batch file would be like this:  for /f "tokens=*" %i
in (list.txt) do copy "%i" destdir\

How can I do something like that in REDHAT??

Best Regards

2. dhcp default route

3. A shell script to duplicate owner permissions to group

4. Which Pentium for LINUX

5. Perl script log file output has duplicate copies of output lines

6. tar command usage

7. Script to determine Duplicate Files..

8. multi-acces to the same file

9. How to attach file to EMAIL MESSAGE within shell script(Korn shell)

10. Using lock file to prevent duplicate scripts from running?

11. script to find duplicate files

12. NEWBIE-Shell scripting - When to use script variable vs. create tmp file???

13. handling SIGINT in shell scripts when executing another shell script.