I wish to rename multiple (many) files, according to their modification datetime. That is, I wish to provide a date (say, 2014-31-03
), and for that date, replace the substring a
to the substring bc
in any of the files whose modification date was that date.
For example, suppose the output of ls -latr --time-style=long-iso
is as follows:
total 8
drwxrwxr-x 3 bach bach 4096 2014-04-01 10:02 ..
-rw-rw-r-- 1 bach bach 0 2014-03-31 10:02 a.1
-rw-rw-r-- 1 bach bach 0 2014-03-31 10:02 a.2
-rw-rw-r-- 1 bach bach 0 2014-03-31 10:02 bc.1
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 a.3
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 bc.2
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 bc.3
drwxrwxr-x 2 bach bach 4096 2014-04-01 10:08 .
then the output after the modification should be as follows:
total 8
drwxrwxr-x 3 bach bach 4096 2014-04-01 10:02 ..
-rw-rw-r-- 1 bach bach 0 2014-03-31 10:02 bc.1
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 a.3
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 bc.2
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:02 bc.3
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:12 bc.1
-rw-rw-r-- 1 bach bach 0 2014-04-01 10:12 bc.2
drwxrwxr-x 2 bach bach 4096 2014-04-01 10:12 .
I do not care about the case for which there is more than one appearance of the substring a
in the name of a file. It's ok if it will replace only the first occurrence. If it makes it much easier, I'm also ok with assuming the substring appears at the beginning of the filename, but I can't assume it is of fixed length.
This find (GNU) should work:
find . -type f -newermt 2014-03-30 ! -newermt 2014-04-01 -exec bash -c 'f="$1"; n="bc.${f##*.}"; mv "$f" "$n"' - '{}' \;