LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Rename multiple files in folders??? (http://www.linuxquestions.org/questions/linux-software-2/rename-multiple-files-in-folders-480473/)

adds2one 09-05-2006 01:02 AM

Rename multiple files in folders???
 
Hello,

I am fairly new to Linux and still coming to know the shell.

I have a Music folder that has album folders in it. Inside of each folder are the songs and a .jpg that is sometimes named folder.jpg and sometimes named .folder.jpg.

I would like to use the shell to issue a command to go through all the folders in the Music folder and rename anything named .folder.jpg to folder.jpg.

I am sure I must be able to do this with the shell.

Could anyone tell me how?

rhoekstra 09-05-2006 03:00 AM

There are numerous ways to achieve this...

You can write C / perl / python programs to do this... this is what you don't want, right?... :)

You could try it like this:

Code:

for i in `find . -name .folder.jpg`;do mv $i ${i/.folder.jpg/folder.jpg};done
Hope this helps..

derek11 09-05-2006 03:12 AM

hi all, actually i'm also trying to solve similar problem

my samba users has created files and folders like these examples:

Quote:

my presentations, documents & proposals
Client's suggestions (last project)
a am looking ways to replace all these illegal charactes (from foldernames/filenames): ',()&/ inside hundreds of subdirectories

i wander if any of you know any script or command to do this

i appreciate any help, thank you.

rahulk 09-05-2006 03:18 AM

well you will have to use the find command as posted earlier in this thread and apply it for changing the special characters.

derek11 09-05-2006 03:27 AM

thanks rahulk, but does it work for current working directory and all its subdirectories?

rahulk 09-05-2006 03:30 AM

Quote:

Originally Posted by derek11
thanks rahulk, but does it work for current working directory and all its subdirectories?

yup it will, find command takes the first argument as the directory name and searches the complete sub directory tree.

find <directory name> <options> <arguments>

for current working directory the option will be "."

find . .............

-rahul

derek11 09-05-2006 03:43 AM

it would be great help if you could give me complete command for clearing/removing , (commas) ' (single quotes) and () (braces) from filenames & foldernames in current folder and all its sub folders.

thanks a lot.

rahulk 09-05-2006 03:58 AM

Quote:

Originally Posted by derek11
it would be great help if you could give me complete command for clearing/removing , (commas) ' (single quotes) and () (braces) from filenames & foldernames in current folder and all its sub folders.

thanks a lot.

e.g.

for i in `find . -name "*,*"`; do mv $i ${i/,/}; done

you can replace the two commas with the special character.

just like the one posted by "rhoekstra" earlier.

rhoekstra 09-05-2006 04:05 AM

The following perl script descents all folders and renames the files that contain illegal characters accordingly.

enter the following in 'rename.pl', make it executable 'chmod u+x rename.pl' and execute it

Code:

#!/usr/bin/perl

use strict;

sub processDir
{
    my $dir=shift;
    opendir(DIR, $dir);
    my @files=grep {! /^\.\.?$/} readdir(DIR);
    closedir(DIR);
    foreach my $file(@files)
    {
        if(-d $file)
        {
            processDir("$dir/$file");
        }
        my $newfile=$file;
        $newfile=~ s/[,& '\(\)]/_/g; #Search for ',', '&', ' ', "'", '(', ')' in filenames and replace them with underscores.
        if( $newfile ne $file )
        {
            print "Renaming \"$dir/$file\" to \"$dir/$newfile\"\n";
            system("mv \"$dir/$file\" \"$dir/$newfile\"");
        }
    }
}

my $dir=shift;
if(!defined($dir))
{
    $dir=".";
}
processDir($dir);

next call it with './rename.pl <directory>' and it should run through..

Hope this helps?

derek11 09-05-2006 04:31 AM

hi rahulk, i guess i need to run your command as many times as there are directories? ~ and there are many hundreds.

thanks rhoekstra, your perl script worked nicely, it would only go 2 levels down in directory structure, but my deep directories, sometimes as much as 10 levels deep.

and it give me this error if there are too many files in a directory:

Quote:

-bash: ./renamer.pl: /usr/bin/perl: bad interpreter: Argument list too long

rhoekstra 09-05-2006 05:12 AM

Ouch.. you're right...
Replace this line:
Code:

if(-d $file)
with:
Code:

if(-d "$dir/$file")
To make it go deeper.
The last error I don't understand quite well..

And to call the program, don't enter '*' on the command line. Just call renamer.pl with the directory to process. It'll find all the files within.
If it needs to process all the files in the current directory, pass along '.' as parameter, or nothing at all (it'll default to process the current directory.

rhoekstra 09-05-2006 05:23 AM

Additionally, to make it more elegant :)..

replace:
Code:

            system("mv \"$dir/$file\" \"$dir/$newfile\"");
with:
Code:

            rename "$dir/$file","$dir/$newfile" or warn("Problems renaming $dir/$file --> $dir/$newfile: $!\n");
Hope this all will help.

doc.nice 09-05-2006 05:36 AM

maybe the package mmv is interesting for you, this app can move multiple files bases on a regex mapping table

RedNovember 09-05-2006 12:16 PM

If you're using KDE, you can use krename for smaller jobs.

derek11 09-06-2006 02:10 AM

hi rhoekstra, thank you, it works nicely on files in deep directories, but would not replace illegal characters in directory names thou.


All times are GMT -5. The time now is 02:20 PM.