1

I have a lot of files in subsubfolders. They all look like in.*.radiate, where * is just a number.

Is there a way to rename all of them with a single command to just in.radiate?

Either in linux or MATLAB?

  • Please clarify: Loads of subdirectories with one file in each? – Jarmund Apr 23 '15 at 21:47
  • They are actually all in subsubdirectories and yes, with one file each called in.*.radiate – Jackson Hart Apr 23 '15 at 21:48
1

If there's an easier way, I do not know, but here's a simple perl script that'll do the job for you:

#!/usr/bin/perl
use warnings;
use strict;

my $oldname = '*.radiate';   # Pattern of old filename. Edit if desired.
my $newname = 'in.radiate';  # New filename. Edit if desired.

foreach my $folder1 (glob("*"))
{
    if (-d $folder1)
    {
        foreach my $folder2 (glob("$folder1/*"))
        {
            if (-d "$folder2")
            {
                print "Processing $folder2\n";
                system("mv $folder2/$oldname $folder2/$newname");
            }
        }
    }
}

Stick that into a file named, for example, rename.pl in the directory where all of the subdirectories are, and run it with perl rename.pl.

It has a bug/feature where it'll let you know if the folder being processesed doesn't contain a .radiate file.

There are probably many shorter ways to do it, but this should be readable enough for "anyone" to edit if needed.

EDIT: Updated to traverse subdirectories of subdirectories, as I misread the original question. Tested it on my machine, and it works as intended on Linux Mint 17 with perl 5.18.2

  • This does not work, becuase they are in subsubolders, not subfolders – Jackson Hart Apr 23 '15 at 22:01
  • Could it be extended to subsubfolders? – Jackson Hart Apr 23 '15 at 22:04
  • @JacksonHart I didn't notice that you said "subsub", but yes it can, and I just did. I also added the ability to change the filename pattern to look for – Jarmund Apr 23 '15 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.