I have 100 folders inside every folder I have one or two files named as the following:XXX_001_014_max.jpgXXX_001_024_max.jpgI saved the folders names in a file list.txtI ran the following code to rename the files inside the folders at once:#!/bin/bashfor i in $(cat list.txt); domv ${i}/XXX_001_014_max.jpg ${i}/image.jpgdoneI aim to rename only the file XXX_001_014_max.jpg to image.jpgand if this file does not exist(i.e. I have only one file inside the folder), then I want the code to rename the second file XXX_001_024_max.jpg to image.jpg I know how to rename the files using the command mv but I am not familiar with the exact usage of if statement in this case.
Rename only one file in each directory, based on condition
bash;files;rename;test
Avoid the for i in $(cat squeeze) construct - it will fail if any of your directory names contains IFS characters...I would read the lines into an array and then simply rename the first file in each directory. Since you always have one or two files in each directory, that means renaming the first one in each directory should do the job - e.g. with bash:mapfile -t mylist <list.txtfor i in ${mylist[@]}doset -- $i/*echo mv -- $1 $i/image.jpgdoneset --Remove the echo if you're happy with the result.