[SOLVED] How to remove duplicate files from Two Folders?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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
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.
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.