Deleting Files On List Of Folders

I have a list of folders from which I have to delete .dec files. How to delete from them at one time. Any for loop or something has to be used?

Solutions:

for loop or while loop:

cat filename | while read line
do
cd $line
rm -f *.dec
done

Let says, you have folder:

/tmp

and subfolders /tmp/test1 and /tmp/test1

content in your filename looks like this:

/tmp/test1
/tmp/test2

while loop will read line, and cd $line which means cd /tmp/test1 and rm -f *.dec in /tmp/test1, next, read next line

Do it on test env first, or best advise, cp -rp /tmp/test1 /tmp/test1.bkup, just in case you screw up.
 

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 .txtand b has 

Code:
abc.txt
pqr.txt
rmr.txt

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

Solutions:

Check if this heps you get started: 

Code:
for file in /path/tp/b/*
do
    echo check if $file exists in directory a
done

or

Code:

for file in `ls  folderb`
do
find  foldera -type f -name "$file" -exec rm -f {} \;
done

or

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$

or 

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

or

Code:
for i in `ls dir1`;do
if [ -f dir2/$i ]; then
rm dir1/$i
fi
done


 


(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.