LinuxQuestions.org

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

adds2one 09-05-2006 12: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 02: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 02: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 02: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 02:27 AM

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

rahulk 09-05-2006 02: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 02: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 02: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 03: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 03: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 04: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 04: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 04: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 11:16 AM

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

derek11 09-06-2006 01:10 AM

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

derek11 09-06-2006 01:15 AM

thanks RedNovember, i'm afraid i did not installed KDE/GNOME on the server, and server is located in remote location.

adds2one 10-04-2006 12:56 AM

Quote:

Originally Posted by rhoekstra
You could try it like this:

Code:

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

This works great when the directory names are one word but doesn't work for most of my directories which are album titles such as:

To Bring You My Love which Linux sees as:

To\ Bring\ You\ My\ Love/

Your script seems to try to go into folder To\ then Bring\ then You\ etc....

Here is an example of the message I get:

mv: cannot stat `./Murray': No such file or directory
mv: cannot stat `Street/.folder.jpg': No such file or directory
mv: cannot stat `./Daydream': No such file or directory
mv: cannot stat `Nation/.folder.jpg': No such file or directory

Thanks so much for your help.

doc.nice 10-04-2006 04:08 AM

Code:

for i in `find . -name .folder.jpg`;do mv "$i" "${i/.folder.jpg/folder.jpg}";done
some litte dashes should help ;)

rhoekstra 10-04-2006 02:19 PM

Quote:

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

It should rename directories as well, but this line:
Code:

$newfile=~ s/[,& '\(\)]/_/g; #Search for ',', '&', ' ', "'", '(', ')' in filenames and replace them with underscores.
takes care of identifying which characters to change with underscores...
between the '[' and ']' add additional 'illegal' characters in order to get them replaced with underscores.

A bit more advanced is to determine if the name consists of 'any' character that is not between a-z,A-Z,0-9,_,-, etc... That could be done instead of the above line perhaps... I'm figuring out how to do so.. in this case you can tell what characters ARE valid, rendering all remaining characters invalid without naming them specifically...

adds2one 10-05-2006 12:22 AM

Quote:

Originally Posted by doc.nice
Code:

for i in `find . -name .folder.jpg`;do mv "$i" "${i/.folder.jpg/folder.jpg}";done
some litte dashes should help ;)

thanks for the suggestion but this results in the exact same problem.


All times are GMT -5. The time now is 12:15 AM.