leo charre


rename

I want to go over linux rename command very quickly. It’s a simple command but I used to find it tricky and unintuitive.

The man rename output examples of foo and bar are freaking cryptic.
So I want to show some real world examples of rename

Let’s do a directory listing…

[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.JPG
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.JPG
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 Picture 11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 Picture 16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 Picture 21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 Picture 22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 Picture 3.jpg

Great. Now, let’s change those filenames..

[leo@localhost rename_example_dir]$ rename 'Picture ' 'PICTURE_' ./Pictu*
[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.JPG
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.JPG
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 PICTURE_11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 PICTURE_16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 PICTURE_21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 PICTURE_22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 PICTURE_3.jpg

Ok, how about we match up the file extensions too..

[leo@localhost rename_example_dir]$ rename JPG jpg ./*
[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.jpg
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.jpg
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 PICTURE_11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 PICTURE_16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 PICTURE_21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 PICTURE_22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 PICTURE_3.jpg

Now let’s talk about what the heck happened.
The command is..

rename $takeout $putin $towhatfiles

First argument is what you do not want there, second argument is what you want to replace it with, third argument is a list of files, you can use wildcards and all the other freaky unix filematch operators.


Linux User