Created
April 4, 2025 06:11
-
-
Save soltrinox/bfe12809bc5ac059d4e9134fe491aaae to your computer and use it in GitHub Desktop.
RENAME TRUNCATE FILE NAME INDEX
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 | |
# This script recursively renames files in all child directories. | |
# For any file that begins with a numeric prefix followed by a hyphen, | |
# the script will remove that prefix. For example: | |
# 001-finename.extension.pdf => finename.extension.pdf | |
# Find all files recursively, sort them alphabetically | |
find . -type f | sort | while IFS= read -r file; do | |
dir=$(dirname "$file") | |
base=$(basename "$file") | |
# Check if the file name starts with one or more digits followed by a hyphen | |
if [[ "$base" =~ ^[0-9]+- ]]; then | |
# Remove the numeric prefix and the hyphen | |
newbase=${base#[0-9]*-} | |
newfile="$dir/$newbase" | |
echo "Renaming '$file' to '$newfile'" | |
# Avoid overwriting an existing file | |
if [ -e "$newfile" ]; then | |
echo "Warning: '$newfile' already exists. Skipping rename." | |
else | |
mv "$file" "$newfile" | |
fi | |
else | |
echo "Skipping '$file' (no numeric prefix found)" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment