Posted under » Linux on 09 November 2017
There are several ways to achieve this, but we’ll explain two of the most efficient and reliable methods. For the purpose of this guide, we have used a directory named Images which has the following structure:
find Images -depth
-depth lists each directory's contents before the directory itself. So lets rename them.
find Images -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
Another alternative way using the find and mv commands in a bash script as explained below. Lets call this script "rename-files.sh".
#!/bin/bash #print usage if [ -z $1 ];then echo "Usage :$(basename $0) parent-directory" exit 1 fi #process all subdirectories and files in parent directory all="$(find $1 -depth)" for name in ${all}; do #set new name in lower case for files and directories new_name="$(dirname "${name}")/$(basename "${name}" | tr '[A-Z]' '[a-z]')" #check if new name already exists if [ "${name}" != "${new_name}" ]; then [ ! -e "${new_name}" ] && mv -T "${name}" "${new_name}"; echo "${name} was renamed to ${new_name}" || echo "${name} wasn't renamed!" fi done echo echo #list directories and file new names in lowercase echo "Directories and files with new names in lowercase letters" find $(echo $1 | tr 'A-Z' 'a-z') -depth exit 0
Then run it like so
rename-files.sh Images
If you want to change your files extension, unfortunately "mv *.xml *.txt" will not work. You have to use some bash and regex for this.
# Rename all *.xml to *.txt for f in *.xml; do mv -- "$f" "${f%.xml}.txt" done