ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ihave a list of files that i need to run script on. i need a simple script (preferably .bat or any other) to help me accomplish two things:
1, run a script over the folder that is containing these files to re-name these files
2, as some are audio files, i need a script to also re-name these file's 'Title'
Wash your mouth out with soap!!!---that is a DOS term (file extension meaning batch file)
You don't really describe two separate things---please tell us the criteria to be used for renaming all the files.....or give some before/after examples.
The general form would be:
Code:
for file in `ls <dirname>` do
.
<Insert code to do the necessary tests>
.
mv $file <newname>
done
Depending on your criteria, there may be short cuts.
If you are using Debian or a Debian-based distro, you have a program already installed name rename that can do wonders. Check man rename. (There's a program with the same name on other distros, but it doesn't seem nearly as powerful.) There are also lots and lots of programs, both gui and command-line, that can do renaming. Two I can think of quickly are GPRename (a gui, Perl-based renamer) and mmv (a command line renamer).
In my experience, renaming is one of those things that feels like it should be very easy, but there are lots of subtle gotchas. (For example, do your song titles have lots of spaces in their names?) So, even for a "simple" job, it's often better to get a utility that's robust and fully tested.
Beyond that, it's hard to give concrete advice without more details about what the files look like now and what you want them to look like. For something like this, some sample data (both before and after) would help a lot.
Last edited by Telemachos; 09-17-2008 at 07:03 AM.
reason is that i want them to remain sequentialy organized when moved into iTunes - for iPod. otherwise it mix up in the entire iTune.
Any sample script to do this please? in the real sense i intend to do this in xp machine, so if DOS solution is available it will be much appreciated. if not, i also have linux (CentOS) box.
Any sample script to do this please? in the real sense i intend to do this in xp machine, so if DOS solution is available it will be much appreciated. if not, i also have linux (CentOS) box.
for a ready script, you can try the Python script here on your Linux box.
example usage:
Code:
# ./script.py -D "/path/to/files" -p "file" -e "_file" -l "*.mp3" # -l options lists files only
To be renamed or deleted): [ /path/to/files/112file04.mp3 ] to [ /path/to/files/112_file04.mp3 ]
To be renamed or deleted): [ /path/to/files/112file03.mp3 ] to [ /path/to/files/112_file03.mp3 ]
To be renamed or deleted): [ /path/to/files/112file01.mp3 ] to [ /path/to/files/112_file01.mp3 ]
To be renamed or deleted): [ /path/to/files/112file05.mp3 ] to [ /path/to/files/112_file05.mp3 ]
To be renamed or deleted): [ /path/to/files/112file02.mp3 ] to [ /path/to/files/112_file02.mp3 ]
# ./script.py -D "/path/to/files" -p "file" -e "_file" "*.mp3" #remove -l to actual rename
# ./script.py -D "/path/to/files" -i "00" -l "*.mp3"
To be renamed or deleted): [ /path/to/files/112_file04.mp3 ] to [ /path/to/files/00112_file04.mp3 ]
To be renamed or deleted): [ /path/to/files/112_file05.mp3 ] to [ /path/to/files/00112_file05.mp3 ]
To be renamed or deleted): [ /path/to/files/112_file01.mp3 ] to [ /path/to/files/00112_file01.mp3 ]
To be renamed or deleted): [ /path/to/files/112_file02.mp3 ] to [ /path/to/files/00112_file02.mp3 ]
To be renamed or deleted): [ /path/to/files/112_file03.mp3 ] to [ /path/to/files/00112_file03.mp3 ]
# ./script.py -D "/path/to/files" -i "00" "*.mp3"
# ./script.py -D "/path/to/files" -s "[0-9][0-9][.]mp3" -e " 001:100.mp3" -l "*.mp3"
To be renamed or deleted): [ /path/to/files/00112_file01.mp3 ] to [ /path/to/files/00112_file 001.mp3 ]
To be renamed or deleted): [ /path/to/files/00112_file02.mp3 ] to [ /path/to/files/00112_file 002.mp3 ]
To be renamed or deleted): [ /path/to/files/00112_file05.mp3 ] to [ /path/to/files/00112_file 003.mp3 ]
To be renamed or deleted): [ /path/to/files/00112_file04.mp3 ] to [ /path/to/files/00112_file 004.mp3 ]
To be renamed or deleted): [ /path/to/files/00112_file03.mp3 ] to [ /path/to/files/00112_file 005.mp3 ]
# ./script.py -D "/path/to/files" -s "[0-9][0-9][.]mp3" -e " 001:100.mp3" "*.mp3" #actual rename
if you do not have Python, you can use the shell versions
Otherwise, you can just write a simple script using shell tools such as sed, rename, or even bash.
thanks so much GhostDog, but i have two more questions to ask:
1,
As these .mp3 files inside each folder are just too many (i listed just 5 above though - but its just many), in all i have over 100 folders. Is there a way to write this script without specifically describing each .mp3 file name. in other words to write the script in a way that it can loop from one .mp3 file to another inside a particular folder?
2,
In addition to re-naming the files name, i also want to re-name these files 'Titles', so how will the script to do this look like?
His script does that if you read carefully. He says that if you remove the -l flag, it'll actually do the rename, instead of just showing you what it would have done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.