I have a large number of folders, named as dates like so:
10 Aug 2010 15 Sep 2010 20 Jun 2010 25 Jul 2010 6 Nov 2010
10 Sep 2010 16 Aug 2010 20 Mar 2010 26 Aug 2010 6 Oct 2010
11 Apr 2010 16 Jun 2010 21 Jun 2010 28 Aug 2010 7 Apr 2010
12 Aug 2010 16 Oct 2010 21 Mar 2010 28 Feb 2010 7 Aug 2010
13 May 2010 17 Apr 2010 22 Feb 2010 28 Jun 2010 7 Jun 2010
14 Aug 2010 17 Aug 2010 23 Jul 2010 28 Mar 2010 8 Apr 2010
14 Jun 2010 18 Aug 2010 24 Jul 2010 29 Mar 2010 8 Aug 2010
15 Aug 2010 19 Aug 2010 24 Nov 2010 30 Aug 2010 8 Jul 2010
15 Jul 2010 4 Apr 2010 25 Apr 2010 6 May 2010 8 Sep 2010
I want the date format to change to this:
2010-04-04 2010-05-03
2010-06-06 2010-07-02
2010-07-05 2010-09-01
2010-09-04 2010-09-05
2010-11-01
How can I batch rename a lot of folders from DD MMM YYYY
to YYYY-MM-DD
?
Somewhat to my own surprise, it looks like our old friend
prename
(a.k.arename
on some systems) can do that - with a little wrangling:Alternatively, using GNU
date
Testing with an
echo
(equivalent to prename's-n
flag):Note that parsing of dates can be locale dependent.
The GNU
date
command is very good at converting between different date formats. For example:With that in mind, if the only things in your target directory are the subdirectories you want to rename, you can simply do:
If you want to specify directories only, use:
And to match only directories whose name starts with a number:
Here is a Python script that can do the renaming using the
datetime
module:Let's call the script
rename.py
. To use the script, first make it executable by runningchmod +x rename.py
and then running./rename.py parent_dir
whereparent_dir
is the parent directory containing all the directories whose names you want to change.Here is a Ruby script that does this same thing. Put this into the directory where all those directory resides.
Use
chmod +x rename.rb
and./rename.rb
to rename the directoriesif you want a good GUI solution try pyrenamer easy and very configurable