Posted By

deanhouseholder on 03/12/14


Tagged

bulk-rename bulk-renamer bulk-renaming renamer bulk-file-rename


Versions (?)

Bulk File Renamer


 / Published in: Bash
 

Description:

This script will loop through all the files in a directory and rename them to match the name contained inside of a text file. This makes it easy to bulk rename files by working iteratively in a single text file, until it contains a list of the file names you want.

Instructions:

1) cd into the directory that contains a list of all the files you want to rename.

2) Type: \ls -1 ../replace.txt

3) cd ..

4) Edit replace.txt but don't change the order of any lines.

5) ./bulk-renamer.sh [directory of files to rename] replace.txt

6) If you don't like the results, just delete replace.txt and start over with step 1

Additional Instructions:

If you are careful, you can also rename just a subset of files in a directory by modifying step 2 to include a wildcard filter to the files you want to rename such as: *.mp3 but you will also need to change the "for loop" below to use the same wildcard filter

Usage: ./bulk-renamer.sh [directory to CD into] [path/to/replace.txt]

  1. #!/bin/bash
  2.  
  3. # Title:
  4. # Bulk Renamer
  5.  
  6. # Author:
  7. # By Dean Householder
  8. # [email protected]
  9. # 3/7/2014
  10.  
  11. # Description:
  12. # This script will loop through all the files in a directory and rename them to match
  13. # the name contained inside of a text file. This makes it easy to bulk rename files
  14. # by working iteratively in a single text file, until it contains a list of the file
  15. # names you want.
  16.  
  17. # Instructions:
  18. # 1) cd into the directory that contains a list of all the files you want to rename.
  19. # 2) Type: \ls -1 ../replace.txt
  20. # 3) cd ..
  21. # 4) Edit replace.txt but don't change the order of any lines.
  22. # 5) ./bulk-renamer.sh [directory of files to rename] replace.txt
  23. # 6) If you don't like the results, just delete replace.txt and start over with step 1
  24.  
  25. # Additional Instructions:
  26. # If you are careful, you can also rename just a subset of files in a directory
  27. # by modifying step 2 to include a wildcard filter to the files you want to rename
  28. # such as: *.mp3
  29. # but you will also need to change the "for loop" below to use the same wildcard filter
  30.  
  31. # Usage: ./bulk-renamer.sh [directory to CD into] [path/to/replace.txt]
  32.  
  33.  
  34. if [ -z "$1" ]; then
  35. echo "Usage: `basename $0` [dirname] [replace.txt]"
  36. echo -e "\nYou can view additional instructions inside the script source.\n"
  37. exit 1
  38. fi
  39.  
  40. cd "$1"
  41.  
  42. count=0
  43. for filename in *; do
  44. count=$(($count+1))
  45. replace=$(sed -n "$count,${count}p" "$2")
  46. mv "$filename" "$replace"
  47. done
  48.  
  49. cd ..

Report this snippet  

You need to login to post a comment.