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? |
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 |
hi all, actually i'm also trying to solve similar problem
my samba users has created files and folders like these examples: Quote:
i wander if any of you know any script or command to do this i appreciate any help, thank you. |
well you will have to use the find command as posted earlier in this thread and apply it for changing the special characters.
|
thanks rahulk, but does it work for current working directory and all its subdirectories?
|
Quote:
find <directory name> <options> <arguments> for current working directory the option will be "." find . ............. -rahul |
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. |
Quote:
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. |
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 Hope this helps? |
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:
|
Ouch.. you're right...
Replace this line: Code:
if(-d $file) Code:
if(-d "$dir/$file") 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. |
Additionally, to make it more elegant :)..
replace: Code:
system("mv \"$dir/$file\" \"$dir/$newfile\""); Code:
rename "$dir/$file","$dir/$newfile" or warn("Problems renaming $dir/$file --> $dir/$newfile: $!\n"); |
maybe the package mmv is interesting for you, this app can move multiple files bases on a regex mapping table
|
If you're using KDE, you can use krename for smaller jobs.
|
hi rhoekstra, thank you, it works nicely on files in deep directories, but would not replace illegal characters in directory names thou.
|
thanks RedNovember, i'm afraid i did not installed KDE/GNOME on the server, and server is located in remote location.
|
Quote:
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. |
Code:
for i in `find . -name .folder.jpg`;do mv "$i" "${i/.folder.jpg/folder.jpg}";done |
Quote:
Code:
$newfile=~ s/[,& '\(\)]/_/g; #Search for ',', '&', ' ', "'", '(', ')' in filenames and replace them 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... |
Quote:
|
All times are GMT -5. The time now is 12:15 AM. |