Created
February 1, 2024 07:55
-
-
Save FabianBeiner/1e998f6f4621bd2f465f72e83c08e795 to your computer and use it in GitHub Desktop.
Scalable Capital - PDF Renamer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Scalable Capital - PDF Renamer | |
# This script is designed to rename PDF files obtained from Scalable Capital, | |
# where the filenames start with a 22-character random string followed by a date and a description. | |
# It removes the random string, formats the date as 'YYYY-MM-DD', and adds a hyphen | |
# to create a more human-readable and consistent file name. | |
# Example: | |
# Original File Name: '2fFabUoFC14sfGZCZ7FJpa 20231004 Monatlicher Kontoauszug Broker Baader Bank.pdf' | |
# Renamed File Name: '2023-10-04 - Monatlicher Kontoauszug Broker Baader Bank.pdf' | |
# Iterate through all files in the directory | |
for file in *; do | |
# Check if it's a file | |
if [ -f "$file" ]; then | |
# Extract the random 22-character string from the beginning of the filename | |
random_string=$(echo "$file" | grep -oE '^[[:alnum:]]{22}') | |
# Extract the date from the remaining part of the filename | |
date=$(echo "$file" | grep -oE '[0-9]{8}') | |
# Check if both random string and date are found in the filename | |
if [ -n "$random_string" ] && [ -n "$date" ]; then | |
# Extract the rest of the filename without the random string and the date | |
rest_of_filename=$(echo "$file" | sed "s/^$random_string //;s/ $date //" | sed 's/^[[:alnum:]]* //') | |
# Rename the file by formatting the date and adding a hyphen between the date and the rest of the filename | |
new_name="$(date -d "$date" '+%Y-%m-%d') - $rest_of_filename" | |
mv "$file" "$new_name" | |
echo "Renamed: $file -> $new_name" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment