unixify 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. #
  3. # unixify.sh
  4. # http://snarfed.org/unixify
  5. # Copyright 2010 Ryan Barrett <unixify@ryanb.org>
  6. #
  7. # Cleans up file names and contents to make them more unix-like. Removes
  8. # punctuation, converts upper case to lower case, optimizes images, and converts
  9. # .doc to .txt. With the dry run flag, -n, just prints what would be done.
  10. #
  11. # test filename: x\ y\ ]z,\[\ \)J\(\ \&\ #\&.jPeG
  12. # should result in: x_y_z_J_and_and.jpg
  13. # parse args
  14. while getopts "n" options; do
  15. case $options in
  16. n ) DRYRUN="-n";;
  17. * ) echo 'Usage: unixify.sh FILES...'
  18. exit 1;;
  19. esac
  20. done
  21. # getops updates OPTIND to point to the arg it stopped at. shift $@ to that point.
  22. shift $((OPTIND-1))
  23. for file in "$@"; do
  24. # clean filename. (careful with the quoting and line break continuations!)
  25. newfile=`rename -v $DRYRUN \
  26. "y/ /_/;
  27. s/[!?':\",\[\]()#]//g; "'
  28. s/&/and/g;
  29. y/A-Z/a-z/;
  30. s/\.\.\./_/g;
  31. s/_+/_/g;
  32. s/_(\.[^.]+$)/$1/;
  33. s/\.jpeg$/.jpg/' \
  34. "$file"`
  35. if [[ $DRYRUN != "" ]]; then
  36. if [[ $newfile != "" ]]; then
  37. echo $newfile
  38. fi
  39. continue
  40. fi
  41. if [[ $newfile =~ ' renamed as ' ]]; then
  42. file=${newfile/* renamed as /}
  43. fi
  44. if [[ $file =~ \.(gif|jpg|png)$ ]]; then
  45. # optimize image and process the EXIF Orientation tag
  46. mogrify -auto-orient $file
  47. elif [[ $file =~ \.doc$ ]]; then
  48. # convert word doc to text
  49. antiword $file > `basename $file .doc`.txt
  50. fi
  51. if [[ `file -b $file` =~ text,.*with\ CRLF ]]; then
  52. # remove any carriage returns
  53. sed --in-place 's/\r//g' $file
  54. fi
  55. done