Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #!/usr/bin/env zsh
  2.  
  3. # If all of the following conditions are met, treats destination as file
  4. # (i.e. mkdir the dirname, and move & rename to basename).
  5. #
  6. # - there is only one source
  7. # - that source is a file
  8. # - basename of the destination has an extension
  9. #
  10. # To suppress this behavior, use the -n option.
  11. # To force treating destination as a file, use the -f option.
  12.  
  13. local -a src
  14. local dst_dir
  15. local dst_file
  16. local force_rename=false
  17. local guess=true
  18.  
  19. while getopts 'hfn' flag; do
  20. case "$flag" in
  21. f)
  22. force_rename=true
  23. ;;
  24. n)
  25. guess=false
  26. ;;
  27. h)
  28. echo "Usage: $0 [-h] [-f] [-n] <src [src...]> <dst>"
  29. exit 0
  30. esac
  31.  
  32. done
  33. shift $(( $OPTIND - 1 ))
  34.  
  35. src=( ${(@)argv[1,-2]} )
  36. dst_dir="${argv[-1]}"
  37. dst_file=""
  38.  
  39. if $force_rename || \
  40. (( $#src == 1 )) && [[ -f $src[1] ]] && \
  41. [[ -n $dst_dir:e ]] && $guess; then
  42. dst_file="${dst_dir:t}"
  43. dst_dir="${dst_dir:h}"
  44. fi
  45.  
  46. echo "mkdir -p \"${dst_dir}\""
  47. echo "mv ${(@)src} \"${dst_dir}/${dst_file}\""
  48. return
  49.  
  50. mkdir -p "${dst_dir}" && mv ${(@)src} "${dst_dir}/${dst_file}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement