From 46bcac61d72188be509ba5b78be19f495870b2e1 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Fri, 19 Apr 2024 20:36:28 +0300 Subject: [PATCH] add file renaming script --- recursive-rename.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 recursive-rename.sh diff --git a/recursive-rename.sh b/recursive-rename.sh new file mode 100755 index 0000000..1df4dcb --- /dev/null +++ b/recursive-rename.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env sh + +PATH_TO_ROOT_DIRECTORY="${1}" +NAME_TO_BE_REPLACED="${2}" +NAME_TO_REPLACE_TO="${3}" + +files=$(find "${PATH_TO_ROOT_DIRECTORY}" -name "*${NAME_TO_BE_REPLACED}*") + +for relative_file_path in ${files}; do + + number_of_fields=$(printf '%s\n' "${relative_file_path}" | awk --field-separator '/' '{ print NF - 1}') + relative_folder_path=$(printf '%s\n' "${relative_file_path}" | cut -d '/' -f "-${number_of_fields}") + file_name=$(printf '%s\n' "${relative_file_path}" | awk --field-separator '/' '{ print $NF }') + new_name=$(printf '%s\n' "${file_name}" | sed "s/${NAME_TO_BE_REPLACED}/${NAME_TO_REPLACE_TO}/") + + new_file_path="${relative_folder_path}/${new_name}" + mv "${relative_file_path}" "${new_file_path}" + + printf "Moved\n '%s'\nto\n '%s'\n\n" "${relative_file_path}" "${new_file_path}" +done