SHARE
TWEET

nevermind85

a guest Nov 12th, 2009 183 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # rn: rename multiple files according to several rules
  3. # Based on the original work of Felix Hudson
  4. # http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-12.html
  5. #
  6. # Author Notice:
  7. # Feel free to change and redistribute as long as you share the code
  8. # and you keep these lines. Violators will not be prosecuted, but might
  9. # feel bad about themselves =)
  10. #
  11. # Contact info:
  12. # Eric G. Burgue˝o [nevermind85 (at) gmail (dot) com]
  13. # http://malditaentropia.com.ar
  14.  
  15. #CHANGELOG:
  16. #v.1.0: Implemented add prefix, suffix and replace string features.
  17. #v.1.1: Replaced if switching by case
  18. #v.1.2: Added listing renaming feature
  19. #v.1.3: Implemented color output for better visuals
  20.  
  21. #set colors
  22. y='\E[1;33m'
  23. g='\E[1;32m'
  24. b='\E[m'
  25.  
  26. #Usage message
  27. ayuda(){
  28. echo "Usage:"
  29. echo "  rn -p \"prefix\" file1 [file2] [file3]..."
  30. echo "  rn -s \"sufix\" file1 [file2] [file3]..."
  31. echo "  rn -r \"expression\" \"replacement\" file1 [file2] [file3]..."
  32. echo "  rn -l extension1 extension2"
  33. echo "Of course, you can use wildcards."
  34. echo "Remember to enclose strings within \"\"."
  35. echo "\"expression\" and \"replacement\" patterns need to have special characters escaped like this:"
  36. echo "rn -r \"dvdrip\\.fOv\" \"\" *avi"
  37. echo
  38. exit
  39. }
  40.  
  41. #check for parameters
  42.  
  43. if [ $# -lt 3 ]; then
  44.         ayuda
  45. fi
  46.  
  47. #First check for the various 'modes' that this program has.
  48. #When the first ($1) condition matches, we execute that portion of the
  49. #program and then exit.
  50.  
  51. case $1 in
  52.  
  53. # check for the prefix condition
  54. -p )
  55.         #if [ $1 = "-p" ]; then
  56.         #make sure enought arguments were supplied
  57.         if [ $# -gt 2 ]; then
  58.                
  59.                 #we now get rid of the mode ($1) and prefix ($2)
  60.                 prefix=$2 ; shift; shift
  61.  
  62.                 # this for loop iterates through all of the files that we gave the program
  63.                 # it does one rename per file given
  64.  
  65.                 #note that we use $@ instead of $*, since the latter won't give us escape
  66.                 #chars and quotes passed as a single argument (i.e. filename with spaces)
  67.                 #we also enclose the $@ with "" so that all the quotes in the arguments
  68.                 #do not expand themselves.
  69.                 for file in "$@"
  70.                 do
  71.                         if test -f "$file"
  72.                         then
  73.                                 echo -e "Moved $y$file$b to $g$prefix$file$b"
  74.                                 mv -i "$file" "$prefix""$file"
  75.                         else
  76.                                 echo "File $y$file$b does not exist."
  77.                         fi
  78.                 done
  79.                 #we now exit the program
  80.                 exit
  81.         #if not mandatory arguments were supplied, show the usage
  82.         else
  83.                 ayuda
  84.                 exit
  85.         fi
  86. ;;
  87.  
  88. # check for the suffix condition
  89. # the rest of this part is virtually identical to the previous section
  90. # please see those notes
  91. -s )
  92.         #if [ $1 = "-s" ]; then
  93.         if [ $# -gt 2 ]; then
  94.                 sufix=$2 ; shift; shift
  95.                 for file in "$@"
  96.                 do
  97.                         if test -f "$file"
  98.                         then
  99.                                 echo "Moved $y$file$b to $g$file$sufix$b"
  100.                                 mv -i "$file" "$file$sufix"
  101.                         else
  102.                                 echo "File $y$file$b does not exist."
  103.                         fi
  104.                 done
  105.                 exit
  106.         else
  107.                 ayuda
  108.                 exit
  109.         fi
  110. ;;
  111.  
  112. # check for the replacement condition
  113. -r )
  114.         #if [ $1 = "-r" ]; then
  115.  
  116.         shift
  117.         # remove other information
  118.         expold=$1 ; expnew=$2 ; shift ; shift
  119.         # This for loop iterates through all of the files that we give the program
  120.         # it does one rename per file given using the program 'sed'
  121.         for oldname in "$@"
  122.         do
  123.                 new="`echo "$oldname" | sed s/"$expold"/"$expnew"/g`"
  124.                 if [ "$oldname" = "$new" ]; then
  125.                         echo -e "Nothing done with $y$oldname$b. Probably string $y$expold$b was not found."
  126.                 else
  127.                         echo -e "Moved $y$oldname$b to $g$new$b"
  128.                         mv -i "$oldname" "$new"
  129.                 fi
  130.         done
  131.         exit
  132. ;;
  133.  
  134. #check for the list condition
  135. -l )
  136.         #if [ $1 = "-l" ]; then
  137.         #this mode takes an array of sorted files from the first extension and
  138.         #a second array of sorted files from the second extension, in order to
  139.         #rename every file from the second array with the same name as the first
  140.         #array, but keeping the extension intact.
  141.         #Useful when you have: "Fringe - S01E04.srt" and "fringe.s01e04.xvid.lol"
  142.         #Many nasty things can go on with this so user confirmation is required.
  143.        
  144.         #remove mode argument
  145.         shift
  146.         #save IFS to restore it later
  147.         OLDIFS=$IFS
  148.         IFS=$'\n'
  149.  
  150.         #fill the two arrays
  151.         fileArray1=(`ls *$1`)
  152.         fileArray2=(`ls *$2`)
  153.  
  154.         #simple check for array lenght (they should match)
  155.         if [ ${#fileArray1[@]} != ${#fileArray2[@]} ]; then
  156.                 echo -e "File count for $y$1$b doesn't match file count for $g$2$b. No action taken."
  157.                 echo -e "$y$1$b count: ${#fileArray1[@]}; $g$2$b count: ${#fileArray2[@]}"
  158.                 exit 1
  159.         fi
  160.  
  161.         #get array lenght
  162.         tLen=${#fileArray1[@]}
  163.  
  164.         #get user confirmation
  165.         echo -e "This is a preview of how the renaming will take place:$y FROM$b /$g TO$b:"
  166.         for (( i=0; i<${tLen}; i++ ));
  167.         do
  168.                 oldname=${fileArray2[$i]}
  169.                 newname=`echo "${fileArray1[$i]}"|sed s/\.$1//`
  170.                 echo -e "$y$oldname$b\n$g$newname.$2$b"
  171.         done
  172.         echo -ne "Confirm? [y/n]: "
  173.         read confirm
  174.  
  175.         if [ $confirm = "y" ] || [ $confirm = "Y" ]; then
  176.  
  177.                 #cycle through the arrays and rename
  178.                 for (( i=0; i<${tLen}; i++ ));
  179.                 do
  180.                         oldname=${fileArray2[$i]}
  181.                         newname=`echo "${fileArray1[$i]}"|sed s/\.$1//`
  182.                         if [ "$oldname" = "$newname.$2" ]; then
  183.                                 echo -e "Nothing done with $y$oldname$b. Destination file is the same."
  184.                         else
  185.                                 echo -e "Moved $y$oldname$b to $g$newname.$2$b"
  186.                                 mv -i "$oldname" "$newname.$2"
  187.                         fi
  188.                 done
  189.  
  190.         fi
  191.  
  192.         IFS=$OLDIFS
  193.         exit
  194. ;;
  195. esac
RAW Paste Data
Top