Using find, xargs and rename Commands Together
rename is a simple command line utility for renaming several files at once in Linux. You can use it together with find utility to rename all files or subdirectories in a particular directory to lowercase as follows:
$ find Files -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
Explanation of options used in the above command.
-depth
– lists each directory’s contents before the directory itself.-n 1
– instructs xargs to use at most one argument per command line from find output.
Using find and mv Commands in Shell Script
First, create your script (you can name it anything you prefer):
$ cd ~/bin $ vi rename-files.sh
Then add the code below in it.
#!/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
Save and close the file, then make the script executable and run it:
$ chmod +x rename-files.sh
$ rename-files.sh Files #Specify Directory Name