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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.