Deletes those files in a directory ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deletes those files in a directory ?
# 1  
Old 03-10-2010
Deletes those files in a directory ?

I need a shell script that accepts 2 directories names bar1 and bar2
as arguments , and deletes those files in bar2 which are are identical to their name
spaces in bar1.

I tried with this code but it gives error.
if [ $# -eq 1 ]
then
echo " syntax <comnd dir1 dir2 > "
exit
else

#echo $1 $2
for i in `ls $1`
do
for j in `ls $2`
do
if [ $i=$j ]
then
unlink $j
fi
done

done
fi

o/p :-
bash-3.1$ sh 17.sh
unlink: cannot unlink `bar1': Is a directory
unlink: cannot unlink `bar2': Is a directory
unlink: cannot unlink `bar1': Is a directory
unlink: cannot unlink `bar2': Is a directory
unlink: cannot unlink `bar1': Is a directory
unlink: cannot unlink `bar2': Is a directory
bash-3.1$

Thnak you
Krish
SmilieSmilieSmilie

# 2  
Old 03-10-2010
Code:
#!/bin/ksh
ls $1 > one
ls $2 > two
awk 'FILENAME=="one" {arr1[$0]++}
       FILENAME=="two" && $0 in arr1 {print $0} ' one two > dupfiles
# this PART IS DANGEROUS
while read fname
do
   rm $2/$fname
done < dupfiles

This creates a file that has the names of the duplicates found in dir==$2 then deletes them from directory $2. Be careful this script could clobber something you really need. It also DOES NOT consider links, which could be important. All in all this script is not a great idea - but I think it does what you specifed
# 3  
Old 03-10-2010
Try:

Code:
for each `comm -12 <(ls -1 dir1 | sort) <(ls -1 dir2 | sort)`
do
rm dir2/$each
done

# 4  
Old 03-11-2010
Still I'm getting errors...
# 5  
Old 03-11-2010
I think following is the best script to do your task.Before you remove the particular file in bar2 directory,you need to check whether it is a file or not.

Code:
j=0
for i in `ls dir1 `
do
arr[$j]=$i
let j+=1
done
for i in `ls dir2`
do
if [[ -f dir2/$i ]];then
for((k=0;k<${#arr[@]};k++))
do
if [[ ${arr[$k]} == $i ]]
then
rm dir2/$i
fi
done
fi
done

# 6  
Old 03-11-2010
Try this ,

Code:
if [ $# -ne 2 ]
then
        echo "Usage : command dir1 dir2"
        exit
else
for i in `ls $1`
do
        for j in `ls $2`
        do
                if [ $i = $j ]
                then
                        rm $2/$j;
                fi
         done
 done
fi

# 7  
Old 03-11-2010
Thank you mr. karthigayan'

Now the script works fine.....Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. UNIX for Dummies Questions & Answers

Command rm deletes filename but not the blocks

Hi It happens when I try to delete a file of 250MB with the command rm -r on our old Intergraph CLIX that the filename disappears while the blocks remain on the machine. Only when I reboot the system the blocks really disappear. Then rm works again for sometime but after some time it happens... (15 Replies)
Discussion started by: hausi2012
15 Replies

3. Solaris

rm -rf not able deletes file but not the directory

i am trying to remove a directory using rm -rf command but its not getting removed.. it doesnt throw any error also.. i am logging as the owner of the dir and removing it but still no luck.. i am able remove a file but not a directory. i am using solaris 10 (12 Replies)
Discussion started by: chidori
12 Replies

4. Slackware

Thunderbird 3.1.9 deletes from drafts

I am using Tbird as it came with Slackware 13.37 and everytime I send something I get a message 1 or 2 drafts deleted. Should it be doing that? If not has this been reported ? How can I find out if it was reported? I have no drafts to delete. (2 Replies)
Discussion started by: slak0
2 Replies

5. UNIX for Advanced & Expert Users

MAKE-like build that deletes orphans?

I have a complex multi-stage (media conversion and formatting) build process that leaves orphan target files needing deleting automatically (i.e. by reference only to the build rules themselves, not any parallel config) upon each regular build. Since MAKE cannot do this, what can? Thanks. (4 Replies)
Discussion started by: chrisjj
4 Replies

6. Shell Programming and Scripting

A script that deletes files.

I want to write a script that deletes files inside the dir. However, the script should also allow the user to confirm by pressing (d) key before deleting files.. #!/bin/bash for file in $1/* do size='ls -l $file | cut -f 5 -d " "' name='ls -l $file | cut -f 9 -d " "' ... (1 Reply)
Discussion started by: herberwz
1 Replies

7. UNIX for Advanced & Expert Users

rsync deletes original directory

Hi I have a strange problem. Sometimes when I execute the below command something wierd happens. rsync -avz -e ssh travegre@travegre.net: ../travegre.net/ the folder named "hm" that is held in travegre.net and is coppied along with all the other folders and data at travegre.net, gets deleted... (4 Replies)
Discussion started by: travegre
4 Replies

8. UNIX for Advanced & Expert Users

Why rsync deletes source directory?

Hi I have a strange problem. Sometimes when I execute the below command something wierd happens. rsync -avz -e ssh travegre@travegre.net: ../travegre.net/ the folder named "hm" that is held in travegre.net and is coppied along with all the other folders and data at travegre.net, gets... (1 Reply)
Discussion started by: travegre
1 Replies

9. Shell Programming and Scripting

Shell Program which deletes all lines ...

Shell Program which deletes all lines containing the word "UNIX" in the files supplied as argument..please help me to do this task :) (4 Replies)
Discussion started by: abhiseknitd
4 Replies

10. UNIX for Dummies Questions & Answers

Multi line deletes

I would like to be able to delete the first n lines of a file from inside of a ksh script. I am not sure how to achieve this or if it is possible. Could someone please help. Thanks in advance. (5 Replies)
Discussion started by: rehoboth
5 Replies
Login or Register to Ask a Question