dotfiles/scripts/sr

80 lines
1.6 KiB
Text
Raw Normal View History

#!/bin/bash
function rename_directories {
find . -type d -not -path '*/\.git/*' -execdir rename "$SEARCH" "$REPLACE" {} \; > /dev/null 2>&1
}
function rename_files {
find . -name '*' -type f -not -path '*/\.git/*' -exec rename "$SEARCH" "$REPLACE" {} \;
}
function rename_inline {
find . -name '*' -type f -not -path '*/\.git/*' -exec sed -i "s/${SEARCH}/${REPLACE}/g" {} \;
}
# das erste Argument der Funktion ist die Anzahl der Argumente des aufrufenden Scripts
# das zweite Argument ist die erwünschte Anzahl an Argumenten
function check_argument_count {
if [ "$1" -lt "$2" ] ; then
echo ">>> Not enough arguments..."
exit 1
elif [ "$1" -gt "$2" ] ; then
echo ">>> Too many arguments..."
exit 2
fi
}
function print_help {
cat <<EOF
Description:
Search and replace recursively.
Usage:
sr [-h|-d|-i|-f] <search> <replace>
Options:
-d Rename only directories.
-f Rename only files.
-i Rename only in files.
-h Print this help.
Author:
michael.grote@posteo.de - git.mgrote.net
EOF
}
SEARCH="$2"
REPLACE="$3"
case "$1" in
-f)
check_argument_count "$#" "3"
rename_files
;;
-i)
check_argument_count "$#" "3"
rename_inline
;;
-d)
check_argument_count "$#" "3"
rename_directories > /dev/null 2>&1
;;
-h)
print_help
exit 0
;;
# ohne Parameter wird alles gemacht
*)
check_argument_count "$#" "2"
SEARCH=$1
REPLACE=$2
echo ">>> Rename files..."
rename_files
echo ">>> Rename directories..."
rename_directories > /dev/null 2>&1
echo ">>> Rename inline..."
rename_inline
;;
esac