Batch rename filenames to snake_case


sudo apt install rename

rename 's/ /_/g' *.{jpeg,jpg,png} | rename 'y/A-Z/a-z/'

ls # get all filenames in the folder
ls *.{jpeg,jpg,png} # get all .png, .jpg or .jpeg files
awk '{ print tolower($0) }' # turn everything lowercase
sed 's/ /_/g' # replace all spaces with underscores
ls *.{jpeg,jpg,png} | awk '{ print tolower($0) }' | sed 's/ /_/g' # get all image filenames in snake_case in Terminal output
'Army vet poor children.jpg'
'British Us Warships.jpg'
'First time loving a soldier Here’s the 101 you need .docx'
'Get in shape military way .docx'
'Monday Motivation Posts_'
'Move over marvel, there’s a real Iron Man in town .docx'
'Prince Harry Green Caps.jpg'
'Prince Harry rewards Royal Marines with Green Berets as training ends  .docx'
'Recruitment taining 2019 things to keep in mind.jpg'
'Recruitment training 2019 things to keep in mind .docx'
'Titans comes together – US, British ships perform joint training.docx'
'Tornado goes out in style.docx'
'Tornado goes out in style.jpg'
'UK Defence Forces top the charts for LGBT empowerment .docx'
'UK defence forces LGBT top charts.jpg'
'Waste no excuse to tell your someone special how much you love them.png'
'Womens day.png'
'Writing the blues away.docx'
'Writing the blues away.jpg'
'amry vet runs for poor children .docx'
'fitness army.jpg'
ls *.{jpeg,jpg,png} | awk '{print tolower($0)}' | sed 's/ /_/g'
ls: cannot access '*.jpeg': No such file or directory

Loop it through and rename all image files..


# Bash script to convert 'An Image File Name.jpg' to 'an_image_file_name.jpg' (snake case)

# Get all image files in the folder
# Turn the filenames lowercase
# Replace spaces with _

for file in ./*.{jpg,jpeg,png}
cp "$file" "$(echo $file | awk '{print tolower($0)}' | sed 's/ /_/g')"
  • the double quotes in cp "" "" are important

Using rename

sudo apt install rename
rename 's/ /_/g' *.{jpeg,jpg,png} | rename 'y/A-Z/a-z/'
  • rename -n 's/ /_/g' *.{jpeg,jpg,png} to replace all spaces with underscores
  • rename -n 'y/A-Z/a-z/' to lowercase everything
  • | will basically take all the filenames generated by the first rename command and pipe them as input to the next rename command
rename -n 's/ /_/g' *.{jpeg,jpg,png} | rename -n 'y/A-Z/a-z/'
rename(rename(Army vet poor children.jpg, Army_vet_poor_children.jpg), rename(army vet poor children.jpg, army_vet_poor_children.jpg))
rename(rename(British Us Warships.jpg, British_Us_Warships.jpg), rename(british us warships.jpg, british_us_warships.jpg))
rename(rename(Prince Harry Green Caps.jpg, Prince_Harry_Green_Caps.jpg), rename(prince harry green caps.jpg, prince_harry_green_caps.jpg))
rename(rename(Recruitment taining 2019 things to keep in mind.jpg, Recruitment_taining_2019_things_to_keep_in_mind.jpg), rename(recruitment taining 2019 things to keep in mind.jpg, recruitment_taining_2019_things_to_keep_in_mind.jpg))
rename(rename(Tornado goes out in style.jpg, Tornado_goes_out_in_style.jpg), rename(tornado goes out in style.jpg, tornado_goes_out_in_style.jpg))
rename(rename(UK defence forces LGBT top charts.jpg, UK_defence_forces_LGBT_top_charts.jpg), rename(uk defence forces lgbt top charts.jpg, uk_defence_forces_lgbt_top_charts.jpg))
rename(rename(Writing the blues away.jpg, Writing_the_blues_away.jpg), rename(writing the blues away.jpg, writing_the_blues_away.jpg))
rename(rename(Waste no excuse to tell your someone special how much you love them.png, Waste_no_excuse_to_tell_your_someone_special_how_much_you_love_them.png), rename(waste no excuse to tell your someone special how much you love them.png, waste_no_excuse_to_tell_your_someone_special_how_much_you_love_them.png))
rename(rename(Womens day.png, Womens_day.png), rename(womens day.png, womens_day.png))
  • -n is for a dry run. to rename all file names in place omit this flag