Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Perl one-liner for renaming files

Tags:

perl

I was testing/trying out this Perl one-liner, and I'm trying to figure out what happened to the files. I don't see the files anymore. Did I delete them or what went wrong?

Example of file names listed (original):

IMG_0178.JPG
IMG_0182.JPG
IMG_0183.JPG
IMG_0184.JPG
IMG_0186.JPG

I wanted to simply change the file extension to lowercase (.jpg):

perl -e'while(<*.JPG>) { s/JPG$/jpg/; rename <*.jpg>, $_ }'
like image 598
cjd143SD Avatar asked Dec 13 '22 00:12

cjd143SD


1 Answers

Don't use rename with a glob. Use scalars. Try to assign the file name to a new variable before the substitution and rename the old name to the modified one, like this:

perl -e'while(<*.JPG>) { ($new = $_) =~ s/JPG$/jpg/; rename $_, $new }'

Check output with ls -1:

IMG_0178.jpg                                                                                                     
IMG_0182.jpg                                                                                                
IMG_0183.jpg                                                                                              
IMG_0184.jpg                                                                                               
IMG_0186.jpg
like image 67
Birei Avatar answered Dec 14 '22 13:12

Birei