Advertisement
freddy87

Batch renaming bash script!!

Jun 12th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Script moulded by freddy87
  4.  
  5.  
  6. read -p "Enter the filetype you want to rename: ." extension #option to select filename extension
  7. file_num=$(ls -1 *.$extension| grep -v '/$' | wc -l) #counts files in folder of type $extension
  8. echo "Number of files to rename: "$file_num #output for peace of mind
  9. padding=${#file_num} #gets length of file_num for padding to give each filename
  10. echo "Length of padding in filename: "$padding #output for peace of mind
  11.  
  12.  
  13.  
  14. a=1
  15. for i in *.$extension; do
  16. new=$(printf "%0"$padding"d."$extension "$a") #$padding appropriate to number of files
  17. if [ "$new" = "$i" ]; then
  18. echo "Error: Filenames matching already"
  19. elif [ "$new" != "$i" ]; then
  20. mv -- "$i" "$new" #renames files
  21. let a=a+1
  22. fi
  23. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement