commit 46bcac61d72188be509ba5b78be19f495870b2e1 Author: cuqmbr Date: Fri Apr 19 20:36:28 2024 +0300 add file renaming script 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