From 43c5924718bd4da911da8c87b92dc80d5a9bc45b Mon Sep 17 00:00:00 2001 From: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> Date: Sat, 7 Oct 2023 12:59:50 +0530 Subject: [PATCH] Create 4MtWtg.sh Created a shell script that renames multiple files in a directory. Signed-off-by: Ritish Srivastava <121374890+Ritish134@users.noreply.github.com> --- hwu4fY/4MtWtg.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 hwu4fY/4MtWtg.sh diff --git a/hwu4fY/4MtWtg.sh b/hwu4fY/4MtWtg.sh new file mode 100644 index 0000000..d25e5da --- /dev/null +++ b/hwu4fY/4MtWtg.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Check if at least one argument (directory path) is provided +if [ "$#" -eq 0 ]; then + echo "Usage: $0 " + exit 1 +fi + +directory="$1" + +# Check if the provided path is a directory +if [ ! -d "$directory" ]; then + echo "Error: $directory is not a directory." + exit 1 +fi + +# Navigate to the specified directory +cd "$directory" || exit + +# Iterate through each .sh file in the directory +for file in *.sh; do + if [ -f "$file" ]; then + # Extract the timestamp from the filename + timestamp=$(echo "$file" | grep -oE '[0-9]{14}') + + # If a timestamp is found, create a new filename by appending the timestamp + if [ -n "$timestamp" ]; then + new_name="${file%.sh}_$timestamp.sh" + + # Rename the file + mv "$file" "$new_name" + + echo "Renamed: $file -> $new_name" + else + echo "No timestamp found in the filename: $file" + fi + fi +done + +echo "Renaming complete."