Created
May 15, 2014 15:30
-
-
Save allolex/52d3763746bf28c6c9e2 to your computer and use it in GitHub Desktop.
A simple script for renaming files using a sed regular expression pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Usage: | |
| # pattern_rename "SED_PATTERN" "FILE_GLOB" | |
| # | |
| # Example: | |
| # pattern_rename.bash 's/^Youtube - //' "*" | |
| # | |
| # Don't forget to put your file glob in quotes. | |
| PATTERN=$1 | |
| FILE_GLOB=$2 | |
| # Using -n to avoid clobbering your existing files | |
| RENAME_COMMAND="mv -n" | |
| for SOURCE_FILE in ${FILE_GLOB}; do | |
| if [ -e "${SOURCE_FILE}" ]; then | |
| TARGET_FILE=$(echo ${SOURCE_FILE} | sed "${PATTERN}") | |
| ${RENAME_COMMAND} "${SOURCE_FILE}" "${TARGET_FILE}" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment