LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-05-2006, 01:02 AM   #1
adds2one
Member
 
Registered: Aug 2005
Location: Vancouver, BC
Distribution: Ubuntu 6.06
Posts: 154

Rep: Reputation: 30
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?
 
Old 09-05-2006, 03:00 AM   #2
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 360

Rep: Reputation: 41
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..

Last edited by rhoekstra; 09-05-2006 at 03:03 AM.
 
Old 09-05-2006, 03:12 AM   #3
derek11
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
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.
 
Old 09-05-2006, 03:18 AM   #4
rahulk
Member
 
Registered: Mar 2006
Posts: 110

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

Last edited by rahulk; 09-05-2006 at 03:24 AM.
 
Old 09-05-2006, 03:27 AM   #5
derek11
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
thanks rahulk, but does it work for current working directory and all its subdirectories?
 
Old 09-05-2006, 03:30 AM   #6
rahulk
Member
 
Registered: Mar 2006
Posts: 110

Rep: Reputation: 16
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
 
Old 09-05-2006, 03:43 AM   #7
derek11
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
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.
 
Old 09-05-2006, 03:58 AM   #8
rahulk
Member
 
Registered: Mar 2006
Posts: 110

Rep: Reputation: 16
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.
 
Old 09-05-2006, 04:05 AM   #9
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 360

Rep: Reputation: 41
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?

Last edited by rhoekstra; 09-05-2006 at 04:08 AM.
 
Old 09-05-2006, 04:31 AM   #10
derek11
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
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
 
Old 09-05-2006, 05:12 AM   #11
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 360

Rep: Reputation: 41
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.
 
Old 09-05-2006, 05:23 AM   #12
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 360

Rep: Reputation: 41
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.
 
Old 09-05-2006, 05:36 AM   #13
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
maybe the package mmv is interesting for you, this app can move multiple files bases on a regex mapping table
 
Old 09-05-2006, 12:16 PM   #14
RedNovember
Member
 
Registered: Oct 2005
Distribution: Debian etch
Posts: 103

Rep: Reputation: 15
If you're using KDE, you can use krename for smaller jobs.
 
Old 09-06-2006, 02:10 AM   #15
derek11
LQ Newbie
 
Registered: Sep 2006
Posts: 6

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


Reply

Tags
file, rename, renamescript


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 04:50 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 07:06 AM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 07:14 AM
Copying files from multiple folders into one aardwolf64 Linux - General 4 01-03-2006 12:53 AM
Multiple file rename jrdioko Linux - Newbie 10 11-07-2004 06:25 PM


All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration