Skip to content

Instantly share code, notes, and snippets.

@yanshiyason yanshiyason/rename
Created Jun 25, 2018

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.