SBaronda.com a little place I like to call home.


Rename multiple files via CLI

Feb. 20, 2016

Moving to a paperless system isn't easy. Getting things automated makes it easier. Here is one tip that will allow you to rename multiple files to append a Date string to a file.

I've been scanning up my old photos to get them to the cloud. My current solution is to scan them using Image Capture, which is an Apple product. This works fine with the exception that the file ends up being Scan 1.jpeg. What I want is 2016-02-20T12:22:11-0500-1.jpeg instead.

Here is how you do it:

$ cd into_dir_where_scanned_files_are_located_at
$ rename -n -v "s/Scan (\d+)/`date +%Y-%m-%dT%H:%M:%S%z` $1/g" *

'Scan 1.jpeg' would be renamed to '2016-02-20T12:21:44-0500-1.jpeg'
'Scan 2.jpeg' would be renamed to '2016-02-20T12:21:44-0500-2.jpeg'
'Scan 3.jpeg' would be renamed to '2016-02-20T12:21:44-0500-3.jpeg'
'Scan 4.jpeg' would be renamed to '2016-02-20T12:21:44-0500-4.jpeg'
'Scan 5.jpeg' would be renamed to '2016-02-20T12:21:44-0500-5.jpeg'
'Scan 6.jpeg' would be renamed to '2016-02-20T12:21:44-0500-6.jpeg'
'Scan 7.jpeg' would be renamed to '2016-02-20T12:21:44-0500-7.jpeg'
'Scan 9.jpeg' would be renamed to '2016-02-20T12:21:44-0500-9.jpeg'

This will do a dry run of the batch process. If you are satisfied then you can remove -n

$ rename -v "s/Scan (\d+)/`date +%Y-%m-%dT%H:%M:%S%z` $1/g" *

'Scan 1.jpeg' renamed to '2016-02-20T12:22:11-0500-1.jpeg'
'Scan 2.jpeg' renamed to '2016-02-20T12:22:11-0500-2.jpeg'
'Scan 3.jpeg' renamed to '2016-02-20T12:22:11-0500-3.jpeg'
'Scan 4.jpeg' renamed to '2016-02-20T12:22:11-0500-4.jpeg'
'Scan 5.jpeg' renamed to '2016-02-20T12:22:11-0500-5.jpeg'
'Scan 6.jpeg' renamed to '2016-02-20T12:22:11-0500-6.jpeg'
'Scan 7.jpeg' renamed to '2016-02-20T12:22:11-0500-7.jpeg'
'Scan 9.jpeg' renamed to '2016-02-20T12:22:11-0500-9.jpeg'


comments powered by Disqus