Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Comparing 2 folders removing the files that are not there twice....

  1. #1
    Join Date
    Aug 2007
    Location
    Denmark
    Beans
    86
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Comparing 2 folders removing the files that are not there twice....

    Hi

    I have 2 folders containing files for a special timelapse video.
    They have been shot with two cameras and named the same way.

    All the pictures are amed like: 2013-01-11-07:58:17.jpg

    My problem is that one folder has slightly more pictures than the other. (camera error)
    So I want to compare folder 1 to folder 2, but only at file name level.
    And remove the pictures that aren't represented in both folders (copying them to a new folder)

    Can this be done and how?

    J;-)
    Last edited by thusgaard; January 18th, 2013 at 09:51 AM. Reason: spelling

  2. #2
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,918
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Comparing 2 folders removing the files that are not there twice....

    the diff command should do it:
    Code:
    ls ./folder1 > file1
    ls ./folder2 > file2
    diff -s file1 file2
    edited: incomplete command
    Last edited by fdrake; January 18th, 2013 at 11:50 AM.

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  3. #3
    Join Date
    Aug 2007
    Location
    Denmark
    Beans
    86
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Comparing 2 folders removing the files that are not there twice....

    Quote Originally Posted by fdrake View Post
    the diff command should do it:
    Code:
    ls ./folder1
    ls ./folder2
    diff -s file1 file2
    Will that remove anything or just show the difference?

    Dosn't this only work on one file. I have 2300+ files in both directories.
    Last edited by thusgaard; January 18th, 2013 at 10:00 AM.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Comparing 2 folders removing the files that are not there twice....

    are these directories flat or have subdirs in them?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Aug 2007
    Location
    Denmark
    Beans
    86
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Comparing 2 folders removing the files that are not there twice....

    Quote Originally Posted by Vaphell View Post
    are these directories flat or have subdirs in them?
    No subdirs, they are completely flat

  6. #6
    Join Date
    Aug 2007
    Location
    Denmark
    Beans
    86
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Comparing 2 folders removing the files that are not there twice....

    Quote Originally Posted by fdrake View Post
    the diff command should do it:
    Code:
    ls ./folder1 > file1
    ls ./folder2 > file2
    diff -s file1 file2
    edited: incomplete command
    Is there a way to

    move the files in the diff list.
    Say is I do like this:

    Code:
    diff -s file1 file2 > difffile

  7. #7
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,918
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Comparing 2 folders removing the files that are not there twice....

    i am sure someone will come up with a more elegant code , but this is what I could get at the moment as fast as I could.

    Code:
    diff -qr ~/a ~/b | while read line ;
    do  folder="$( echo $line | cut -d ":" -f 1| cut -d " " -f 3)"; 
    file="$( echo $line | cut -d " " -f 4)"; 
    path="$folder/$file"; 
    cp $path ~/c ; 
    done
    copy and paste into the termianl one line at the time:

    "~/a" and "~/b" are the folders to be compared
    "~/c" ius the folder where you want to copy the file not in common


    Note :
    1-you have to change the path and the name of the folders based on your folders;
    2- I used the copy command not the move "mv". Just to make sure you don't loose any data. Try it first then change "cp $path ~/c ; " with "mv $path ~/c ; " (or even better an IF condition that deletes the file only if the copy commands is succesfull)_
    BE CAREFULL! "mv" COMMAND DOES NOT FORGIVE!!!
    Last edited by fdrake; January 20th, 2013 at 04:48 AM.

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  8. #8
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Comparing 2 folders removing the files that are not there twice....

    couldn't you just do something like

    Code:
    $ for file in dir2/*; do [ -f "dir1/${file##*/}" ] || cp "$file" newdir/; done
    Code:
    $ ls dir1
    file1  file2  file3  file4  file5
    $ ls dir2
    file1  file2  file3  file4  file5  file6
    $ 
    $ for file in dir2/*; do [ -f "dir1/${file##*/}" ] || echo cp "$file" newdir/; done
    cp dir2/file6 newdir/

  9. #9
    Join Date
    May 2006
    Location
    Boston
    Beans
    1,918
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Comparing 2 folders removing the files that are not there twice....

    Quote Originally Posted by steeldriver View Post
    couldn't you just do something like

    Code:
    $ for file in dir2/*; do [ -f "dir1/${file##*/}" ] || cp "$file" newdir/; done
    Code:
    $ ls dir1
    file1  file2  file3  file4  file5
    $ ls dir2
    file1  file2  file3  file4  file5  file6
    $ 
    $ for file in dir2/*; do [ -f "dir1/${file##*/}" ] || echo cp "$file" newdir/; done
    cp dir2/file6 newdir/
    I do not think the code copies the files present in dir1 AND not in dir2 . You copy only the files present in dir2 and AND in dir1.


    can you posyt the content of new dir?
    Last edited by fdrake; January 20th, 2013 at 04:43 AM.

    blog
    Donations BTC : 12FwoB7uAM5FnweykpR1AEEDVFaTLTYFkS
    DOUBLEPLUSGOOD!!

  10. #10
    Join Date
    Aug 2007
    Location
    Denmark
    Beans
    86
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Comparing 2 folders removing the files that are not there twice....

    This worked for me:

    Code:
    $ cd folder1/
    $ for file in `ls -1` ; do [ -f ../folder2/${file} ] || mv  $file /somefolder/ ; done
    
    $ cd ../folder2/
    $ for file in `ls -1` ; do [ -f ../folder1/${file} ] || mv  $file /somefolder/ ; done

    Thanks for all your help.

    J;-)

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •