Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Last active December 16, 2015 03:49
Show Gist options
  • Save pmbuko/5372666 to your computer and use it in GitHub Desktop.
Save pmbuko/5372666 to your computer and use it in GitHub Desktop.
This script will, in the specified directory, find files ending with a space and a 2 -- ' 2' -- and strip that ending off matching files. Read inline comments for more info.
#!/bin/bash
# Explicitly set the directory you want to operate on here
workingPath="/your/path/here"
# Set a custom internal field separator to ease handling
# of filenames that contain spaces
IFS=$'\n'
# Identify files that end in ' 2' and tack on our custom
# field separator
endIn2=$(find "${workingPath}" -regex '.* 2$' | awk '{print $0"\n"}')
# Go through the list of files and rename them
for oldName in $endIn2; do
newName=$(echo "${oldName}" | awk -F' 2' '{print $1}')
# show what this script will do
echo "\"${oldName}\" <-- will become --> \"${newName}\""
# uncomment the next line after verifying output
#mv "${oldName}" "${newName}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment