Advertisement
xtherebellion

renameFiles.sh

Feb 9th, 2017
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.14 KB | None | 0 0
  1. #!/bin/bash
  2. #---------------------------------------------------------------------------------------------------------------------------------------------#
  3. #       Purpose: To look through a directory and find all files that contain a string; given as Argument 1, and replace that string with Argument 2
  4. #       Author: Jonathan Laberge
  5. #       This script is free to use and can modify to suit your own needs
  6. #---------------------------------------------------------------------------------------------------------------------------------------------#
  7.  
  8. #---------------------------------------------------------------------------------------------------------------------------------------------#
  9. # Declaring Globals Variables
  10. #---------------------------------------------------------------------------------------------------------------------------------------------#
  11.  
  12. VERSION="Version: 1.0.1"
  13.  
  14. #--------------------------------------------------------------------------------------------------#
  15. # Function usage
  16. # Globals:
  17. #   CURTIME
  18. #   TEMPFILE
  19. # Arguments:
  20. #   FILENAME
  21. # Returns:
  22. #   None
  23. #--------------------------------------------------------------------------------------------------#
  24. function usage(){
  25. local OPTIND=1
  26. USAGE="$(basename "$FUNCNAME"): STRING_TO_FIND REPLACE_WITH_STRING
  27.         or: -d STRING_TO_FIND REPLACE_WITH_STRING PATH_TO_DIR
  28.  
  29. Summary: This script browses $filePath for files that contain a specific string, and then replaces that string
  30.      for each file name that matches.
  31.  
  32. Arguments:
  33.     -d      Enables depth, for searching directories within the filepath
  34.     -l      Lists files in the filePath (excludes any non-file items)
  35.     -a      Auto-mode; find files with yesterday's date and replace with today's
  36.     -h          Print Help (this message) and exit
  37.     -v          Print version information and exit"
  38.  
  39. filePath='/path/to/file'
  40. depth="-maxdepth 1"
  41. files="Files currently in $filePath:"
  42. findString=
  43. replaceString=
  44. while getopts "hvdla" opt; do
  45.   case $opt in
  46.     h)
  47.     echo "$USAGE" >&2
  48.     exit 1
  49.     ;;
  50.     a)
  51.     findString=$(date --date="1 day ago" +'%d-%b')
  52.     replaceString=$(date +'%d-%b')
  53.     ;;
  54.     v)
  55.     echo "$VERSION"
  56.     exit 1
  57.     ;;
  58.     l)
  59.     echo $files
  60.     find $filePath $depth -type f | sed 's|'$filePath'/|./|'
  61.     exit 1
  62.     ;;
  63.     d)
  64.     files="Files currently in $filePath and containing folders:"
  65.     depth="-depth"
  66.     ;;
  67.     \?)
  68.       echo "Invalid option: -$OPTARG" >&2
  69.       exit 1
  70.       ;;
  71.     :)
  72.       echo "Option -$OPTARG requires an argument." >&2
  73.       exit 1
  74.       ;;
  75.   esac
  76. done
  77. shift $(($OPTIND - 1))
  78. # After options are taken into account, declare necessary variables for renameFiles()
  79. # Options should be shifted and therefore allowing $1 to be first argument and not the option.
  80. if [ -z $3 ]; then #If 3rd argument is empty, it defaults to whatever is specified; else $3 will be file path
  81. filePath='/path/to/file'
  82. else
  83. filePath="$3"
  84. fi
  85. if [ -z $findString ] && [ -z $replaceString ]; then
  86. findString="$1"
  87. replaceString="$2"
  88. fi
  89. }
  90.  
  91. #--------------------------------------------------------------------------------------------------#
  92. # Function renameFiles
  93. # Requires 2 Arguments at least, this function searches a directory
  94. # Globals:
  95. #   CURTIME
  96. #   TEMPFILE
  97. # Arguments:
  98. #   FILENAME
  99. # Returns:
  100. #   None
  101. #--------------------------------------------------------------------------------------------------#
  102. function renameFiles(){
  103. fileCount=$(find $filePath $depth -name "*$findString*" | wc -l)
  104.     find $filePath $depth -name "*$findString*" -execdir bash -c 'for f; do mv -iv "$f" "${f//'"$findString"'/'"$replaceString"'}"; done' bash {} +
  105.     echo "$fileCount File(s) have been renamed."
  106. }
  107.  
  108. #---------------------------------------------------------------------------------------------------------------------------------------------#
  109. # Executing functions
  110. #---------------------------------------------------------------------------------------------------------------------------------------------#
  111. if [ $# -ge 2 ] || [ $1 == '-a' ]; then
  112.     usage "$@"
  113.     renameFiles
  114.  
  115.     exit
  116. else
  117.     if [ -z $1 ]; then
  118.     echo "Missing Operands."
  119.     echo "Try '$(basename "$0")' --help for more information."
  120.     else
  121.         usage $1
  122.     fi
  123. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement