2

I have folder with about 1200 files that I need to rename. Theit file extension is .jpg, .gif, and .png for the most part.

Here are some examples:

    Awesome\ FB\ \ (462).jpg   
    Bonus\ FB\ Cover\ Pics\ (80).jpg
    Awesome\ FB\ \ (463).jpg   
    Bonus\ FB\ Cover\ Pics\ (81).jpg
    Awesome\ FB\ \ (464).jpg   
    Bonus\ FB\ Cover\ Pics\ (82).jpg
    Awesome\ FB\ \ (465).jpg   
    Bonus\ FB\ Cover\ Pics\ (83).jpg

I tried:

    rename 's/\.{???} $/-img4sm.{???}/' ./*.{???}

I also tried this but just placing a .jpg or .gif instead of {???}, and replacing the {} with [].

I also tried:

    find . -type f -iname '*.???' -exec rename 's/\.{???} $/-img4sm.{???}/' ./*.{} +

I've managed to use this command without error messages in CloudLinux (CentOS), but no file names changed.

Please help me. Thank you!

2
  • 2
    Could You specify into what are you trying to rename the files ?
    – magor
    Oct 20, 2015 at 7:48
  • Sure. I really don't care too much what their names are. It can be a sequence of numbers or letters, such as 1, 2, 3, 4, etc followed by img4sm.[???]. For example, 1img4sm.jpg, 2img4sm.jpg, 3img4sm.jpg, 1img4sm.gif, 1img4sm.png, etc. Oct 20, 2015 at 16:33

3 Answers 3

2

Unfortunately, rename means different things on different versions of Unix. In particular on Fedora, and therefore CentOS I imagine, rename will only accept simple strings, and not a perl expression like you are using (which would work on debian).

However, it seems all you want to do is add "-img4sm." in front of the .jpg, .png and .gif suffix, so with your version of rename try:

rename -- .jpg -img4sm.jpg *.jpg
rename -- .png -img4sm.png *.png
rename -- .gif -img4sm.gif *.gif

Always copy your directory somewhere as backup in case these commands do not do what you expect.


If you prefer, you can install the more sophisticated perl script rename from cpan by using the cpan command if you already know it, or simply by downloading the link on that page to the download tar. Unpack the tar and "build" the script with

tar xf rename-1.9.tar.gz
cd rename-1.9
perl Makefile.PL 
make
mv bin/rename ~/bin/prename

where the final mv is to some private bin dir in your PATH. Use the name prename to avoid confusion with the rename command you already have. You can then use this to do your action with, eg:

prename -n  's/\.(jpg|png|gif)$/-mg4sm.$1/' -- *.{jpg,gif,png}

where here -n means just show what would happen, without doing it.

4
  • I will keep that in mind for my other VM for CentOS. But, this one is CloudLinux configured by a web hosting company and restricts installations heavily. I cannot install it your way or cpan File::Rename per permissions. Oct 20, 2015 at 16:38
  • You could look at this php solution.
    – meuh
    Oct 20, 2015 at 17:01
  • My only issue with that solution is that it also renames the file extensions, which I am trying to preserve. Basically, I am trying to add some alphanumeric sequence at the beginning of the file name, add -mg4sm to the end of the file name but before the file extension, and remove the original file name. Or, replace the original file name with an alphanumeric sequence in front of the file name and mg4sm in the end of the file name while preserving the extension. Can you help me do this? Oct 20, 2015 at 17:18
  • I provided, in a separate answer, a php script to do what you want.
    – meuh
    Oct 20, 2015 at 17:50
2

With zsh:

autoload zmv # best in ~/.zshrc
zmv -n '(*).(png|jpg|gif)' '$1-mg4sm.$2'

Remove the -n to actually do it.

Otherwise, POSIXly, you can always do:

for file in *.png *.jpg *.gif; do
  echo mv -i -- "$file" "${file%.*}-mg4sm.${file##*.}"
done

(remove echo to actually do it).

2
  • This is on CloudLinux (CentOS) but I am not the admin as it is on a web hosting server. I will try this on my local CentOS or Mac and let you know. Thank you! Oct 20, 2015 at 16:41
  • That was nice. It did part 1/2 of what I need done. It added -mg4sm in front of each jpg, png, and gif file extension. Now, I need to add either a sequence of alphanumeric characters such as 1,2,3,4, etc. in front of each file while removing all of the remaining filename between the 1,2,3,4, and -mg4sm. Oct 20, 2015 at 17:07
0

Here is a solution using php based on this answer. Create a file eg myrename.php with the following contents:

<?php
$test = 1;
if(!chdir('/tmp/mydir')) echo "failed chdir\n";
else if ($handle = opendir('.')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = preg_replace("/\.(jpg|gif|png)$/i","-img4sm.\\1",$fileName);
        if($newName!=$fileName){
            if($test){
                echo "$fileName -> $newName\n";
            }else{
                if(!rename($fileName, $newName))echo "failed $fileName -> $newName\n";
            }
        }
    }
    closedir($handle);
}
?>

replace /tmp/mydir by the path of your directory. Run the script with

php myrename.php

and it will print out the old filename and new filename for each matched file. If this is ok, change $test = 1; to $test = 0; and run it for real. Remember to backup the files somewhere first in case of problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .