LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-19-2010, 01:26 AM   #1
guessity
Member
 
Registered: Dec 2009
Posts: 41

Rep: Reputation: 15
How to remove duplicate files from Two Folders?


I have two folders -

Folder abc and Folder xyz which contains 1000's of files with few of them having the same file names.

How can I remove the duplicates from Folder abc?

plz help me out on this.
 
Old 08-19-2010, 01:53 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Are there other folders in these folders?
Assuming not you could try a simple for loop:
Code:
for file in xyz/*
do
    [[ -e "abc/$file" ]] && echo "abc/$file"
done
Change echo to rm if the data looks correct. If there are spaces in files names may need to choose an alternate while loop combo.
 
Old 08-19-2010, 02:04 AM   #3
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Hi Grail,
thx. I did the following

Code:
#!/bin/bash
for file in /home/tst/xyz/*
do
    [[ -e "/home/tst/abc/$file" ]] && rm -rf "/home/tst/abc/$file"
done
However I can see nothing coming. I have a file in the both the folders called 1.txt and that is not getting removed for some reason.

Any idea where I have gone wrong?
 
Old 08-19-2010, 02:22 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Try fdupes.
 
1 members found this post helpful.
Old 08-19-2010, 02:26 AM   #5
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Actually I have two directories and I dont have permission to install on this linux system.
 
Old 08-19-2010, 02:28 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by guessity View Post
Hi Grail,
thx. I did the following

Code:
#!/bin/bash
for file in /home/tst/xyz/*
do
    [[ -e "/home/tst/abc/$file" ]] && rm -rf "/home/tst/abc/$file"
done
However I can see nothing coming. I have a file in the both the folders called 1.txt and that is not getting removed for some reason.

Any idea where I have gone wrong?
Hello,

You need to look at your paths and variables. You have set full path so when calling your variable $file you get the full path, so nothing found because:
Code:
$file will be /home/tst/xyz/filename
and in your test:
Code:
[[ -e "/home/tst/abc//home/tst/xyz/filename" ]]
Change your script like this and run it from /home/tst (or put that in another variable)
Code:
#!/bin/bash
for file in xyz/*
do
    filename=`basename $file`
    [[ -e "abc/$filename" ]] && rm -rf "abc/$filename"
done
Kind regards,

Eric
 
Old 08-19-2010, 02:31 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Also, you can 'dry run' your script by putting this line:
Code:
set -x
right after #!/bin/bash. That way the script doesn't get executed but you'll get output on your screen of what would be done so you can see where it goes wrong. Works great when troubleshooting.

Kind regards,

Eric
 
Old 08-19-2010, 02:36 AM   #8
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Thx EricTRA. It works well!!
 
Old 08-19-2010, 02:43 AM   #9
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

Glad it works.

Kind regards,

Eric
 
Old 08-19-2010, 02:43 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo rm -f $(sort <(ls -1 abc) <(ls -1 xyz)| sed 's/^\.*\///' | uniq -d | sed -e ":a" -e 'N;s/\n/ xyz\//;ba')
 
Old 08-19-2010, 02:50 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
You might want to double check that the files are exact copies first, though, otherwise you might lose something important.

Code:
#!/bin/bash
for file in xyz/*
do
    filename=`basename $file`
    if [[ -e "abc/$filename" ]]; then
        if diff -q "$file" "abc/$filename" > /dev/null; then
            rm -rf "abc/$filename"
        else
            echo "$file and abc/$filename are different, review manually."
        fi
    fi
done
 
1 members found this post helpful.
Old 08-19-2010, 05:05 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Sorry my response didn't work ... wasn't paying attention. In my defence however, I did suggest using echo before using rm to check data was correct.
This would have shown you the path issue straight away
 
1 members found this post helpful.
Old 08-19-2010, 05:26 AM   #13
Leslie007
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 2
You can use this perl script also,

use strict;
use warnings;
use File::Basename;
my @dir = </home/Bash/abc/*>;


foreach my $file (@dir) {
unlink($file) if(-e "/home/test/xyz/".basename($file)) ;
}
 
Old 08-19-2010, 05:28 AM   #14
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi grail,

Your response does work if you assume executing the script from the 'base' location. Since OP didn't use that structure but put in the full path it didn't work as expected.

Kind regards,

Eric
 
Old 08-19-2010, 06:57 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I was wondering... abc/* will form abc/file1, abc/file2 .. etc. So how did your scripts work?
Shouldn't it be just like this?
Code:
cd abc/
for A in *; do
    [[ -e ../xyz/$A ]] && rm "$A"
done
I commonly do this pattern when I'm deleting duplicate files in a directory.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to AUTO remove duplicate files drudge Linux - Newbie 9 03-13-2013 03:19 PM
Script to remove duplicate jpg files ski_phreak Programming 1 05-26-2010 09:22 AM
How to use find and remove folders and files Drigo Linux - Newbie 1 06-01-2009 12:53 AM
remove ~ from all files and folders BoraX Linux - General 1 10-25-2006 08:58 PM
need app to remove duplicate files from the filesystem Ynot Irucrem Linux - Software 2 05-19-2005 01:35 PM


All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration