Skip to content

Instantly share code, notes, and snippets.

@yanshiyason
Created June 25, 2018 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanshiyason/cbbcd76ae86b46d5ea7a2fdb79aa29db to your computer and use it in GitHub Desktop.
Save yanshiyason/cbbcd76ae86b46d5ea7a2fdb79aa29db to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Simple file and folder renaming utility.
#
# Makes use of the [sharkdp/fd](https://github.com/sharkdp/fd) find script
# to rename files and folders
#
# Usage example:
#
# rename from/pattern to_pattern
# rename from_pattern to/pattern
# rename from_pattern to_pattern
#
read -r -d '' script <<'EOF'
ARGV.each_slice(4).each do |from, to, from_path, to_path|
to_path = to_path.sub(from, to)
dir = to_path.split('/')[0..-2].join('/')
`test -d "#{dir}" || mkdir -p "#{dir}"`
`mv "#{from_path}" "#{to_path}"`
puts "#{from_path} ---> #{to_path}"
end
EOF
from=$1
fd -ap "$from" --exec echo "$@ {} {}" | xargs ruby -e "$script"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment