Rename a sequence of files.
Often a sequence files is written with file names which create issues for other programs, like files written as
file_1.0 file_1.5 file_2.0
.
A simple bash script can be used to rename the files. The bash scripts described here are meant to be saved into a file, like
rename_files.sh
, and executed with bash:
bash rename_files.sh
Padding numbers
If your files are of the format
file1 file2 ... file99 ...
you can pad the numbers with 0:
for old in file*; do
# Extract the number at the end of the file name.
number=${old/file/}
# Create the new number string with the right format, i.e. 0 padding.
num_str=$(printf %04d $number)
# Move the old file to the new file.
cp $old file$num_str
done
The result will be a sequence of files with the names
file0001 file0002 ... file0099 ...
Remove any number padding
If your files are of the format
file0001 file0002 ... file0099 ...
you can remove the padding:
for old in file*; do
# Extract the number at the end of the file name.
number=${old/file/}
# Create the new number string with the right format, i.e. no 0 padding.
num_str=$((10#${number}))
# Move the old file to the new file.
cp $old file$num_str
done
The result will be a sequence of files with the names
file1 file2 ... file99 ...
Change a file sequence with float numbers into a sequence with integers
If your files are of the format
file_1.0 file_1.5 ...
you can change them into a simple sequence:
i=0
for old in file*.*; do
# Extract the number at the end of the file name.
number=${old//file/}
# Create the new number string with the right format, i.e. 0 padding.
num_str=$(printf %04d $i)
# Move the old file to the new file.
cp $old file$num_str
i=$(($i+1))
done
The result will be a sequence of files with the names
file0001 file0002 ... file0099 ...
References
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-12.html