Say I have a range of files named: "blah-10-blah", "blah-11-blah" etc...up to "blah-30-blah".
I would like to change the names to "blah-20-blah", "blah-21-blah" etc...up to "blah-40-blah".
Is there a way of doing this in the terminal?
Say I have a range of files named: "blah-10-blah", "blah-11-blah" etc...up to "blah-30-blah".
I would like to change the names to "blah-20-blah", "blah-21-blah" etc...up to "blah-40-blah".
Is there a way of doing this in the terminal?
You can do:
#!/bin/bash
files=( blah-??-blah )
for ((i=${#files[@]}-1; i>=0; i--)); do
first="${files[$i]%%-*}"
num="$(grep -o '[0-9]\+' <<<"${files[$i]}")"
last="${files##*-}"
echo mv "$first-$num-$last" "$first-$((num+10))-$last"
done
If you are satisfied with everything, add | bash
at the end to let the mv
operation take place.
We have put the relevant file names into an array files
Then we have iterated over the elements of the array from the end i.e. from last to first
first
will have the first part of file name i.e. part prior to the first -
last
will have the last part of the file name i.e. the part after last -
num
will have the number in between two -
mv "$first-$num-$last" "$first-$((num+10))-$last"
will do the rename operation accordingly
Example:
$ ls -1
blah-10-blah
blah-11-blah
blah-12-blah
blah-13-blah
blah-14-blah
blah-15-blah
blah-16-blah
blah-17-blah
blah-18-blah
blah-19-blah
blah-20-blah
blah-21-blah
blah-22-blah
blah-23-blah
blah-24-blah
blah-25-blah
blah-26-blah
blah-27-blah
blah-28-blah
blah-29-blah
blah-30-blah
blah-foo-1
blah-foo-2
$ for ((i=${#files[@]}-1; i>=0; i--)); do first="${files[$i]%%-*}" \
num="$(grep -o '[0-9]\+' <<<"${files[$i]}")" last="${files##*-}"; \
echo mv "$first-$num-$last" "$first-$((num+10))-$last"; done
mv blah-30-blah blah-40-blah
mv blah-29-blah blah-39-blah
mv blah-28-blah blah-38-blah
mv blah-27-blah blah-37-blah
mv blah-26-blah blah-36-blah
mv blah-25-blah blah-35-blah
mv blah-24-blah blah-34-blah
mv blah-23-blah blah-33-blah
mv blah-22-blah blah-32-blah
mv blah-21-blah blah-31-blah
mv blah-20-blah blah-30-blah
mv blah-19-blah blah-29-blah
mv blah-18-blah blah-28-blah
mv blah-17-blah blah-27-blah
mv blah-16-blah blah-26-blah
mv blah-15-blah blah-25-blah
mv blah-14-blah blah-24-blah
mv blah-13-blah blah-23-blah
mv blah-12-blah blah-22-blah
mv blah-11-blah blah-21-blah
mv blah-10-blah blah-20-blah
$ for ((i=${#files[@]}-1; i>=0; i--)); do first="${files[$i]%%-*}" \
num="$(grep -o '[0-9]\+' <<<"${files[$i]}")" last="${files##*-}";\
echo mv "$first-$num-$last" "$first-$((num+10))-$last"; done | bash
$ ls -1
blah-20-blah
blah-21-blah
blah-22-blah
blah-23-blah
blah-24-blah
blah-25-blah
blah-26-blah
blah-27-blah
blah-28-blah
blah-29-blah
blah-30-blah
blah-31-blah
blah-32-blah
blah-33-blah
blah-34-blah
blah-35-blah
blah-36-blah
blah-37-blah
blah-38-blah
blah-39-blah
blah-40-blah
blah-foo-1
blah-foo-2
It's important to process the files in an inverse numerical order, otherwise the task will fail due to already existing files with the target filename:
find . -maxdepth 1 -type f -name 'blah-??-blah' -print0 | sort -zr | xargs -0 rename 's/-\K([0-9]{2})/$1+10/e'
find . -maxdepth 1 -type f -name 'blah-??-blah' -print0
: prints a NULL-separated list of the files in the current working directory matching the globbing pattern blah-??-blah
;sort -zr
: sorts the list in an inverse numerical order;xargs -0 rename 's/-\K([0-9]{2})/$1+10/e'
: renames the files substituting the first couple of digits after a dash with the correspondent value incremented by 10;% tree
.
├── blah-10-blah
├── blah-11-blah
├── blah-12-blah
├── blah-13-blah
├── blah-14-blah
├── blah-15-blah
├── blah-16-blah
├── blah-17-blah
├── blah-18-blah
├── blah-19-blah
├── blah-20-blah
├── blah-21-blah
├── blah-22-blah
├── blah-23-blah
├── blah-24-blah
├── blah-25-blah
├── blah-26-blah
├── blah-27-blah
├── blah-28-blah
├── blah-29-blah
└── blah-30-blah
0 directories, 21 files
% find . -maxdepth 1 -type f -name 'blah-??-blah' -print0 | sort -zr | xargs -0 rename 's/-\K([0-9]{2})/$1+10/e'
% tree
.
├── blah-20-blah
├── blah-21-blah
├── blah-22-blah
├── blah-23-blah
├── blah-24-blah
├── blah-25-blah
├── blah-26-blah
├── blah-27-blah
├── blah-28-blah
├── blah-29-blah
├── blah-30-blah
├── blah-31-blah
├── blah-32-blah
├── blah-33-blah
├── blah-34-blah
├── blah-35-blah
├── blah-36-blah
├── blah-37-blah
├── blah-38-blah
├── blah-39-blah
└── blah-40-blah
0 directories, 21 files
If using Zsh, the task can be heavily simplified, as Zsh allows to expand filenames in an inverse numerical order:
rename 's/-\K([0-9]{2})/$1+10/e' blah-??-blah(On)
% tree
.
├── blah-10-blah
├── blah-11-blah
├── blah-12-blah
├── blah-13-blah
├── blah-14-blah
├── blah-15-blah
├── blah-16-blah
├── blah-17-blah
├── blah-18-blah
├── blah-19-blah
├── blah-20-blah
├── blah-21-blah
├── blah-22-blah
├── blah-23-blah
├── blah-24-blah
├── blah-25-blah
├── blah-26-blah
├── blah-27-blah
├── blah-28-blah
├── blah-29-blah
└── blah-30-blah
0 directories, 21 files
% rename 's/-\K([0-9]{2})/$1+10/e' blah-??-blah(On)
% tree
.
├── blah-20-blah
├── blah-21-blah
├── blah-22-blah
├── blah-23-blah
├── blah-24-blah
├── blah-25-blah
├── blah-26-blah
├── blah-27-blah
├── blah-28-blah
├── blah-29-blah
├── blah-30-blah
├── blah-31-blah
├── blah-32-blah
├── blah-33-blah
├── blah-34-blah
├── blah-35-blah
├── blah-36-blah
├── blah-37-blah
├── blah-38-blah
├── blah-39-blah
└── blah-40-blah
0 directories, 21 files
Basic idea of this approach is to throw all the files into temporary "basket" directory, and then pick them out one by one, create new name , and move back with new name into the original directory.
The script bellow takes a single argument ( $1 ), which is directory where the files you want to rename are located.
Demo
xieerqi:$ ls testdir
blah-10-blah blah-20-blah blah-30-blah blah-40-blah
xieerqi:$ cat testdir/*
I am file 10
I am file 20
I am file 30
I am file 40
xieerqi:$ ./incrementNames.sh testdir
blah-10-blah ../blah-20-blah
blah-20-blah ../blah-30-blah
blah-30-blah ../blah-40-blah
blah-40-blah ../blah-50-blah
xieerqi:$ ls testdir
blah-20-blah blah-30-blah blah-40-blah blah-50-blah TMP/
xieerqi:$ cat testdir/blah
blah-20-blah blah-30-blah blah-40-blah blah-50-blah
xieerqi:$ cat testdir/blah-20-blah
I am file 10
xieerqi:$ cat testdir/blah-30-blah
I am file 20
Script
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: incrementNames.sh /path/to/dir" && exit
fi
# navigate to target directory
# create temporary directory
cd "$1"
mkdir TMP
# move everything to TMP directory
find . -maxdepth 1 -type f -iname "*-*" -exec mv -t TMP {} \+
# drop down to TMP directory pick filesback into the directory , and rename them as we go
cd TMP
find . -maxdepth 1 -type f -iname "*-*" -printf "%f\n" | sort | while IFS= read FILENAME
do
NEW="$( awk -F '-' '{print $1FS$2+10FS$3 }' <<< "$FILENAME")"
echo "$FILENAME" "../$NEW"
mv "$FILENAME" ../"$NEW"
done
Limitation
This script is specifically for files following pattern text-number-text
, or at least text-number
patter. It wont work for others