delete all the files in folder a which exist in folder b


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete all the files in folder a which exist in folder b
# 1  
Old 07-28-2009
delete all the files in folder a which exist in folder b

Hi ,
I need a script which basically deltes all files in folder a which are alreasy present in folder b

say folder a has files
Code:
abc.txt
pqr .txt

and b has
Code:
abc.txt
pqr.txt
rmr.txt

then file abc.txt and pqr.txt from a should be deleted

Last edited by Yogesh Sawant; 07-28-2009 at 03:32 AM.. Reason: added code tags
# 2  
Old 07-28-2009
check if this heps you get started:
Code:
for file in /path/tp/b/*
do
    echo check if $file exists in directory a
done

# 3  
Old 07-28-2009
what have you tried so far?
# 4  
Old 07-28-2009
Code:
 
for file in `ls  folderb`
do
find  foldera -type f -name "$file" -exec rm -f {} \;
done

# 5  
Old 07-28-2009
you can also try diff command to get listing.
Code:
-bash-3.2$ diff fol1 fol2 | grep fol1
Only in fol1: b.txt
Only in fol1: c.txt
-bash-3.2$

# 6  
Old 07-28-2009
try it

Code:
for i in $(ls -1 /home/pritish/a);do
        for j in $(ls -1 /home/pritish/b);do
                if [ $i ==  $j ]; then
                        $(rm -i /home/pritish/a/$i)
                fi
        done
done


Last edited by Yogesh Sawant; 07-28-2009 at 11:48 AM.. Reason: added code tags
# 7  
Old 07-28-2009
Code:
for i in `ls dir1`;do
if [ -f dir2/$i ]; then
rm dir1/$i
fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. UNIX for Dummies Questions & Answers

Script to delete files in different folder

Hi Experts, i need a little help. i have different folder that contain files that need to be deleted. but those folder contains huge amoung of same with 3 different extention. what i used to do is to delete them using the rm commande rm *.ext *.ext1 *.ext3 what i want to do is to have... (1 Reply)
Discussion started by: yprudent
1 Replies

5. Shell Programming and Scripting

need to navigate into specified folder and delete the files

Hi all, I need to write a script to naviagate into the list of specified folder and delete the files in it. I have mentioned the list of folders in a external file so it can be reusable. The issue is am facing now is that, i am not understating on how to navigate into the folder locations... (6 Replies)
Discussion started by: Nithz
6 Replies

6. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

8. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

9. UNIX for Dummies Questions & Answers

How can i delete files in folder by date?

Hi, I have some files on a folder and i want to delete all the files that were created on July. Thanks, Kobi. (9 Replies)
Discussion started by: kobibn
9 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question