BASH shell script to rename file that has spaces

R  you looking for shell script which rename all files in a directory to replace unnecssery spaces in file names with -. Check following BASH shell script which,

1. Iterate a perticular directory specified in FILES variable.
2. Replaces spaces with - hyphen, and put it in temparary variable new_file. (sed "s/ /-/g"`)
4. Then simple MV command to rename it.

  1. #!/bin/bash
  2. FILES=/path/to/dir/*
  3. for f in $FILES
  4. do
  5. new_file=`echo $f|sed "s/ /-/g"`
  6. if [ "$f" != "$new_file" ]
  7. then
  8. mv "$f" "$new_file"
  9. fi
  10. done
  11. exit 0


Post a Comment

Share this post with your friends