Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
I have been beating my head against the wall trying to figure this out. Its probably very simple, so here goes.
I have a directory full of audio files and I used grip to get them off of the cd. The problem I have is
most of the file names are wrong and I want to rename them with a one line bash script (or command).
Example:
Acts-Chpt-01.mp3
Acts-Chpt-02.mp3
Acts-Chpt-03.mp3
....
....
Acts-Chpt-15.mp3
Acts-Chpt-16.mp3
When the files were written, the chapter numbers that were written are wrong. Chapter number 1
should actually be 12, 2 should be 13, 3 should be 14, etc. BTW, files 1-9 starts with '0'....
I have a ton of files that I need to rename and I am looking for a shortcut. I created a new directory to practice in and created a bunch of files with the 'touch' command. (I'm not going to practice on the real thing). touch file-01.mp3 file-02.mp3 etc. I read the man and info pages on 'tr' but I am getting nowhere. Can someone offer a suggestion for batch renaming files with numbers in the filename and increasing or decreasing the actual number in the filename without changing anything else?
I'll mess around with that script. It definitely needs a little tweaking but it got me started in the right direction. I ran it and I'm sharing the output with you...
I named the script 'rename'.
Part of the filenames obviously need to be removed. I'll work on the script and if you think of a fix then please post it. I appreciate your input!! Thanks for your help!
If you're willing to consider an alternate suggestion...
There's a really nice (free) program I used to use for this back when I used Windows. MP3Tag. It's a file renamer, tag editor, etc. Really nice for organizing a music collection.
Its heading in the right direction. I just need to add something to the script that recognizes if I use the number '1' as the starting number....
Let me try it this way: For the numbers '1-9', I need the script to recognize that and add a '0' before the digits '1-9'. I was looking through 'rute' ( the tutorial) and found a simple script in there and he uses
something along the lines of:
while test "$N" -le "10"
For some reason the -le command isn't being recognized by my bash.
Am I making any sense?
What I'm looking for is a line I can add to the script that utilizes 'if $input <= 10 then add a zero before the digits '1-9' so I get 01, 02, 03, 04...and when it gets to 10 just print 10, 11, 12 and so on?
Any ideas?
This post is a bit scattered as I pressed for time. Sorry for any confusion.
I'm a bit confused, if you're adding 11 to each number ("1 should actually be 12, 2 should be 13, 3 should be 14, etc.") then how does "01" come into play? There should be no 01 after you add 11 to all your numbers.
Maybe you can give two concrete examples (one single digit number and one double) of what the files ARE now and what they SHOULD be after.
If I have a bunch of files and the beginning number is '1' and the last one is '15' and I want them to be renamed 12.mp3, 13.mp3, 14.mp3, etc, then thats one scenario.
Some of the files are named 16.mp3, 17.mp3, 18.mp3, etc and I may want them to be 06,.mp3, 07.mp3, 08.mp3, etc. The reason I'm looking for a '0' before the digits '1-9' is so the .mp3 files play in the proper order... if you have files named 1.mp3, 14.mp3, 10.mp3, 11.mp3, 13.mp3 etc the file and 'ls', then the following is how it is listed:
07:53 PM /home/agg : ls
10.mp3 11.mp3 13.mp3 14.mp3 1.mp3
10 is listed before 1.
I need that '0'!!!!
I think I understand now. These may not be the most elegant solutions but they should work. This is based on your naming structure given above (Acts-Chpt-01.mp3).
To add 11:
Code:
for i in *;
do base=${i%-*}; extension=${i##*.};
num=$(echo ${i%.*} |cut -d '-' -f3 | cut -d '.' -f1);
newnum=$num+11; mv "$i" "$base-$newnum.$extension";
done;
To add the 0:
Code:
for i in *;
do base=${i%-*}; extension=${i##*.};
num=$(echo ${i%.*} |cut -d '-' -f3 | cut -d '.' -f1);
let newnum=$(printf %02d $num);
mv "$i" "$base-$newnum.$extension";
done;
Like I said, not very elegant but it works. If you want something more universal I would tread into the realm of perl myself.
The script is making sense to me and I'm understanding it well except for one thing.
It does work up to a point now ( the script you just put on here this morning)
Here is the directory listing before and after the script and the error message included:
Quote:
11:25 AM /home/ab : c;ls
Acts-Chpt-11.wav Acts-Chpt-14.wav Acts-Chpt-17.wav Acts-Chpt-20.wav
Acts-Chpt-12.wav Acts-Chpt-15.wav Acts-Chpt-18.wav Acts-Chpt-21.wav
Acts-Chpt-13.wav Acts-Chpt-16.wav Acts-Chpt-19.wav Acts-Chpt-22.wav
11:25 AM /home/ab : lt9
Enter name: Acts-Chapter-
Enter starting number: 1
hello
hello
hello
hello
hello
hello
hello
hello
/bin/lt9: line 17: 08: value too great for base (error token is "08")
Files Renamed
11:25 AM /home/ab : ls
Acts-Chapter-01.mp3 Acts-Chapter-04.mp3 Acts-Chapter-07.mp3 Acts-Chpt-20.wav
Acts-Chapter-02.mp3 Acts-Chapter-05.mp3 Acts-Chapter-08.mp3 Acts-Chpt-21.wav
Acts-Chapter-03.mp3 Acts-Chapter-06.mp3 Acts-Chpt-19.wav Acts-Chpt-22.wav
((Don't pay attention to the change in the extension as that is minor, because I'm going to implement 'lame' into the script to convert the files from .wav>.mp3.))
The error from line 17 is throwing me for a loop. Other than that, its doing exactly what I was looking for. Thank you so much!! Any ideas for a fix on this? I don't understand 'value to great for base'.
I'm assuming that bash is treating "-lt 10" as digits '0-8' as 0 thru 8 is actually nine digits.
Thanks for your patience and help!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.