Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created April 4, 2025 06:10
Show Gist options
  • Save soltrinox/112f1bc45c6ea79dbdbe2c504b73794d to your computer and use it in GitHub Desktop.
Save soltrinox/112f1bc45c6ea79dbdbe2c504b73794d to your computer and use it in GitHub Desktop.
APPEND PREFIX INDEX INTEGET LEFT PAD ZERO
#!/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