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

Report this snippet  

You need to login to post a comment.