Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env zsh
- # If all of the following conditions are met, treats destination as file
- # (i.e. mkdir the dirname, and move & rename to basename).
- #
- # - there is only one source
- # - that source is a file
- # - basename of the destination has an extension
- #
- # To suppress this behavior, use the -n option.
- # To force treating destination as a file, use the -f option.
- local -a src
- local dst_dir
- local dst_file
- local force_rename=false
- local guess=true
- while getopts 'hfn' flag; do
- case "$flag" in
- f)
- force_rename=true
- ;;
- n)
- guess=false
- ;;
- h)
- echo "Usage: $0 [-h] [-f] [-n] <src [src...]> <dst>"
- exit 0
- esac
- done
- shift $(( $OPTIND - 1 ))
- src=( ${(@)argv[1,-2]} )
- dst_dir="${argv[-1]}"
- dst_file=""
- if $force_rename || \
- (( $#src == 1 )) && [[ -f $src[1] ]] && \
- [[ -n $dst_dir:e ]] && $guess; then
- dst_file="${dst_dir:t}"
- dst_dir="${dst_dir:h}"
- fi
- echo "mkdir -p \"${dst_dir}\""
- echo "mv ${(@)src} \"${dst_dir}/${dst_file}\""
- return
- mkdir -p "${dst_dir}" && mv ${(@)src} "${dst_dir}/${dst_file}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement