Y-T
October 17, 2010, 10:07pm
1
Hello,
i have the following problem:
there are two folders with a lot of files.
Example:
FolderA contains AAA, BBB, CCC
FolderB contains DDD, EEE, AAA
How can i via script identify AAA as duplicate in Folder B and delete it there? So that only DDD and EEE remain, in Folder B?
Thank you in advance for looking into this.
cola
October 17, 2010, 10:10pm
2
y-t:
Hello,
i have the following problem:
there are two folders with a lot of files.
Example:
FolderA contains AAA, BBB, CCC
FolderB contains DDD, EEE, AAA
How can i via script identify AAA as duplicate in Folder B and delete it there? So that only DDD and EEE remain, in Folder B?
Thank you in advance for looking into this.
No two files with same name can be in the same folder.
Y-T
October 17, 2010, 10:13pm
3
that is why they are in Folder A and Folder B, as i wrote
rdcwayx
October 17, 2010, 10:19pm
4
which env? Is the diff
command in your system support to diff folder?
Y-T
October 17, 2010, 10:22pm
5
when i enter "diff" at the command line, it is recognized as command. I am using ubuntu, if this is any useful information
The aim is to keep the files each in their folders, but just delete in B those which already exist in A
cola
October 17, 2010, 10:26pm
6
#!/bin/sh
vr=$(ls /path/to/FolderA)
for fl1 in ${vr};do
echo "${fl1}"
done
vr2=$(ls /path/to/FolderB)
for fl2 in ${vr2};do
echo "${fl2}"
done
Now make two arrays to save the result and compare those two arrays.
If there is any match then remove those files.
1 Like
rdcwayx
October 17, 2010, 10:27pm
7
in ubuntu, you should be able to diff the folder directly.
diff /path/to/FolderA /path/to/FolderB
1 Like
Y-T
October 17, 2010, 10:29pm
8
ok, thank you, i will try the Diff.. the array option would mean still i have to compare manually if i read it right, which is futile because it is >10K files
kurumi
October 17, 2010, 10:37pm
9
#!/usr/bin/env ruby -w
require 'fileutils'
Dir["./A/*"].each do |d|
name=File.basename(d) if File.directory?(d)
if File.exists?("B/"+name)
print "#{name} exists in #{"B/"+name}\n"
print "Deleting %s\n" % name
FileUtils.rm_r(d, :force => true)
end
end
1 Like
Y-T
October 17, 2010, 10:39pm
10
Ah, that works too, kurumi, thank you
for i in FolderA;do
ls ${i}/* | while read file;do
tfile=`basename $file`
if [ -f FolderB/${tfile} ];then
rm FolderB/${tfile}
fi
done
done
1 Like
frans
October 18, 2010, 3:45am
12
Simple
while read file
do rm -f FolderB/$file
done < <(ls FolderA)
1 Like
Note: that is bash code, not shell code.
Neo
October 18, 2010, 4:09am
14
According to Wikipedia
Bash is a free software Unix shell written for the GNU Project. Its name is an acronym which stands for Bourne-again shell.[3] The name is a pun on the name of the Bourne shell (sh), an early and important Unix shell written by Stephen Bourne and distributed with Version 7 Unix circa 1978,[4] and the common Christian concept of born again. Bash was created in 1987 by Brian Fox. In 1990 Chet Ramey became the primary maintainer.[5]
Bash is a POSIX shell with a number of extensions. It is the shell for the GNU operating system from the GNU Project. It can be run on most Unix-like operating systems. It is the default shell on most systems built on top of the Linux kernel as well as on Mac OS X and Darwin. It has also been ported to Microsoft Windows using Subsystem for UNIX-based Applications (SUA), or POSIX emulation provided by Cygwin and MSYS. It has been ported to MS-DOS by the DJGPP project and to Novell NetWare.
I think any (command line style) scripting code that is designed to work in shell is "shell code" isn't it?
OK, maybe I should have said: That is bash shell code, not POSIX compliant shell code, so it will only work in bash or maybe ksh93.
---------- Post updated at 10:22 ---------- Previous update was at 10:12 ----------
for i in FolderA/*
do
rm -f "FolderB${i#FolderA}" 2>/dev/null
done
ls FolderA |
while read file
do
rm -f "FolderB/$file" 2>/dev/null
done
2 Likes
kurumi
October 18, 2010, 4:29am
16
There's a also slight difference in the meaning of "shell code" in computer security. In computer security, a shellcode is a small piece of code used as the payload in the exploitation of a software vulnerability (as defined by Wikipedia). Its not to be confused with "shell scripting code".
1 Like
Y-T
October 18, 2010, 7:34am
17
you all have been of great help. Thank you so very much!