Skip to content

Instantly share code, notes, and snippets.

@mattpetters
Created April 28, 2023 00:10
Show Gist options
  • Save mattpetters/d38adfad522a299685cb4d8da5a7a188 to your computer and use it in GitHub Desktop.
Save mattpetters/d38adfad522a299685cb4d8da5a7a188 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if the input file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <file>"
exit 1
fi
# Store the input file's path
input_file="$1"
# Check if the file exists
if [ ! -f "$input_file" ]; then
echo "Error: File not found."
exit 1
fi
# Get the file's created date in YYYY-MM-DD format
created_date=$(stat -f "%SB" -t "%Y-%m-%d" "$input_file")
# Get the original file name without the extension
original_name_no_ext=$(basename "$input_file" | sed 's/\.[^.]*$//')
# Sanitize the original file name by removing spaces and special characters
sanitized_name_no_ext=$(echo "$original_name_no_ext" | sed 's/[^a-zA-Z0-9._-]/_/g')
# Get the file extension
file_ext=$(basename "$input_file" | sed 's/^.*\././')
# Assemble the new file name
new_file_name="${created_date}_${sanitized_name_no_ext}_v1${file_ext}"
# Rename the file
mv "$input_file" "$(dirname "$input_file")/$new_file_name"
echo "File renamed to: $new_file_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment