vendredi 31 juillet 2015

remove file starting with space in shell scripting

I'm trying to write a shell script to cleanup a directory by deleting files that match particular patterns. My code works with all patterns but if the file name starts with space. Although we can delete a file starting with space by rm \ * however if I pass this pattern to my script it won't delete files starting with space. Here is my code:

for file in *;do
 for pattern in $*; do
    if [[ -f "$file" && "$file" == $pattern ]]; then
      rm "$file"
    fi
  done
done

I also tried this simpler code, but the same problem!

for pattern in $*; do    
  if [[ -f $pattern ]]; then  
    rm $pattern
  fi
done

Could you please help!

Aucun commentaire:

Enregistrer un commentaire