Skip to content

Instantly share code, notes, and snippets.

@WSAyan
Created April 25, 2020 20:08
Show Gist options
  • Save WSAyan/99c13aac3ffcb7f0ccf5a3deb252f817 to your computer and use it in GitHub Desktop.
Save WSAyan/99c13aac3ffcb7f0ccf5a3deb252f817 to your computer and use it in GitHub Desktop.
Rename all files with ascending number in a directory
# rename all files with ascending number in a directory
# sh rename.sh -e [extension] -f [first number] -name [optinal name in first]
_ext=""
_first=0
_name=""
while getopts ":e:o:f:n:" opt; do
case $opt in
e)
_ext="$OPTARG"
;;
f)
_first="$OPTARG"
;;
n)
_name="$OPTARG"
;;
\?)
echo "ERROR: Invalid option: -$OPTARG" >&2
exit
;;
esac
done
i=$_first
for file in *.$_ext; do
mv "$file" "$_name$i.$_ext"
i=$((i+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment