shell script to rename files in a directory code example

Example 1: bash how to change all filenames in a directory

# Basic syntax using rename:
rename filename_search_expression replacement_text file_search
# For every file returned with file_search, if filename_search_expression
# is present in the file name, it will be changed to replacement_text

# Example usage:
# Given two files named:
this_is_an_example_filename.txt
example_filename_this_is.txt # Yoda phrasing

rename example_filename other_filename *.txt # Changes filenames to:

this_is_an_other_filename.txt
other_filename_this_is.txt

Example 2: rename multiple files linux

rename 's/old-name/new-name/' files

Example 3: bash rename multiple files

# Basic syntax using rename:
rename search_string replacement_string files

# Example usage:
# Say you have a directory containing the following files:
badly_name_file_1.txt
badly_named_file_1.txt
badly_named_file_2.txt
badly_named_file_3.png

rename txt png *txt # This would change all extensions to .png, e.g.:
badly_name_file_1.png
badly_named_file_1.png
badly_named_file_2.png
badly_named_file_3.png

# *txt returns all files that end in txt and then if txt is found in the
#	file name, it gets replaced with png

rename named speled badly* # This would change named to speled in all
#	files that begin with badly, e.g.:
badly_name_file_1.txt # This one is skipped because it doesn't have named
badly_speled_file_1.txt
badly_speled_file_2.txt
badly_speled_file_3.png