-
-
Save bergercookie/5ad8b6ea3ba2044283bba3e52f31dfba to your computer and use it in GitHub Desktop.
mv-in-place a file, keep it under the same directory - change just the basename
This file contains 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 | |
# Change the name of a file without having to specify its full path from | |
# scratch. Instead specify only the modified filename, and keep it under the | |
# same parent directory. | |
name=$(basename $0) | |
args=$@ | |
function usage() { | |
echo -e "Usage:\n\n$name <path-to-file> <new-filename-in-same-dir>" | |
echo "$name <path-to-file> -p <new prefix>" | |
echo "$name <path-to-file> -s <new suffix>" | |
} | |
function bad() { | |
usage | |
exit 1 | |
} | |
if [[ $# != 2 && $# != 3 ]]; then | |
bad | |
fi | |
filename=$1 | |
shift | |
while test $# -gt 0; do | |
case "$1" in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-p) | |
shift | |
if [[ $# -eq 1 ]]; then | |
prefix="$1" | |
mv $filename $(dirname $filename)/$prefix$(basename $filename) | |
exit 0 | |
else | |
bad | |
fi | |
exit 0 | |
;; | |
-s) | |
shift | |
if [[ $# -eq 1 ]]; then | |
suffix="$1" | |
mv $filename $filename$suffix | |
exit 0 | |
else | |
bad | |
fi | |
exit 0 | |
;; | |
*) | |
new_name=$1 | |
mv $filename $(dirname $filename)/$new_name | |
break | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment