0

I hava problem with bash srcipt that removes all files ( with name given
in $1 ) in current dir and subdirectories .
The problem is that the script don't see all subdirectories and when
I try to run it in my home dir the srpit display strange message .

Code:

#!/bin/sh
# this sript remove file from current dir and his subdir ..

remove_file()
{
  #########
  #echo "$1"        # $1- current dir , $2 - filename
  #echo "$2"
  #########
  
  if [ "$#" -eq 2 ] ; then  # $1 and $2
   for file in *
   do
     
     if [ $file = "$2" ] && [ -f $file ] ; then
        echo
        echo "Remove : ${1}/${file} "
        #rm -f ${1}/${file}           # removing ... 
     elif [ -d $file ] ; then
       local directory=`pwd`  # current dir
       echo
       echo "IN : ${directory}/${file} "
       ( cd $directory/${file} ; remove_file $directory/${file} $2 ) # recursve call
     else
       return 0
     fi
   done
  else
   return 0
  fi
}

remove_file `pwd` $1     # $1 is the filename that we want to remove
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Infarction
0

what about something like this:

#!/bin/bash

for file in `find ./ -iname $1`
do
  rm $file
done

Not tested, but I think it'll work. You'll have to give different flags to rm if you want it to remove directories as well...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.