Hello Keith,
m@m-xubuntu:~$ cd scripts
m@m-xubuntu:~/scripts$ ls -l name-reformat
-rwxrwxr-x 1 m m 343 May 1 04:20 name-reformat
m@m-xubuntu:~/scripts$
M
Hello Keith,
m@m-xubuntu:~$ cd scripts
m@m-xubuntu:~/scripts$ ls -l name-reformat
-rwxrwxr-x 1 m m 343 May 1 04:20 name-reformat
m@m-xubuntu:~/scripts$
M
HI Mike.
Perfect! Before launching the script on your files, you might like to check first that it works - for confidence. So we will create two files to test it on. The first is a dummy mp3 file in the format of your files. The second has the same format but is not an mp3 file.
In you scripts directory enter (please use the Code[Select] option to copy the code):
touch dd-mm-yyyy.mp3
touch DD-MM-YYYY.txt
You can check the files by using the ls command.
Now enter the name of your script:
name-reformat
The output should be something like:
keith@E5570:~/scripts$ name-reformat
Old file: yyyy-mm-dd.mp3 New file: dd-mm-yyyy.mp3
keith@E5570:
Note that the txt file was ignored, so any non-mp3 files will remain unchanged.
If your output is different or you have an error, please post the full output.
Keith
Keith,
Bingo.
m@m-xubuntu:~/scripts$ touch dd-mm-yyyy.mp3
m@m-xubuntu:~/scripts$ touch DD-MM-YYYY.txt
m@m-xubuntu:~/scripts$ ls
dd-mm-yyyy.mp3 DD-MM-YYYY.txt name-reformat
m@m-xubuntu:~/scripts$ name-reformat
Old file: dd-mm-yyyy.mp3 new file: yyyy-mm-dd.mp3
m@m-xubuntu:~/scripts$
I think your example output reversed the new and old filenames, but your script go it spot on.
That’s great!
Unfortunately I 've found a bug in my script when dealing with multple mp3 fies. I’ll fix that before moving on…
[EDIT] Found the error and fixed it.
If you are sure that you have backed up the directory of mp3 files (say by copying the directory to another name) then please do the following:
ls -l name*
Check that the “w” permissions are still there.
You can check its operation again by entering:
name-reformat
name-reformat
Your file names should now be reformatted. Check with ls and report the result.
Note that filenames that don’t conform to the format that you gave me (with two hyphens) will produce an error message.
Keith
Thanks Keith.
I’m very grateful for all your help…
Mike
Keith,
I can’t find an attached script…
Mike,
In my reply #24: Right at the bottom immediately under my name you will see a paperclip symbol with the file-name name-reformat next to it.
Click (or double-click) on the name to download it. It’s not at all obvious - sorry.
Keith
Do you mean #23?
No sign of a paperclip Keith…
I’m using dark mode which sometimes makes things hard to see, but I’ve checked thouroughly.
I’m going to log out, which switches the forum back to light mode, and have another look. I’ll be back.
Edit: This is odd. I can’t log out. When I try to switch to my profile nothing happens…
Mike,
Definitely #24. The attachment to this post shows where to look.
Keith
My login timed out by default (I set it to 1 hour).
Still no luck though. No sign of a paperclip.
And at this end, #24 is one of my replies to you… the one which says I’m grateful for all your help.
Likewise there’s no paperclip in your post saying “definitely #24”
Bizarre.
Try refreshing the page as I may have added the attachment after submitting the post.
But the numbering error is a worry I’ll take up with the Management.
Not sure how to proceed now. The system has your mail address - may I email the flle to you?
Keith
Of course. Or maybe just cut and paste it into a new post?
PS. Refreshing the page didn’t help.
Mike,
I’ve emailed the two files and show the script here also (ought to have done that at the start!).
#!/bin/bash
#name-reformat.
#Check mp3 files exist. If none then exit:
if [[ -z $(ls *.mp3) ]]; then
echo "No mp3 files."
exit
fi
#For each file extract the various parts of the file name.
for file in $(ls -1 *.mp3); do
day=$(echo $file | cut -d "-" -f 1)
month=$(echo $file | cut -d "-" -f 2)
year=$(echo $file | cut -d "-" -f 3 | cut -d "." -f 1)
suffix=$(echo $file | cut -d "." -f 2)
#Create a reformatted name:
new_file=$year"-"$month"-"$day"."$suffix
echo "Old file: $file New file: $new_file"
#Now change the file name:
mv $file $new_file
done
Keith
Hello Keith,
I copied the new script, pasted it into name-reformat using mousepad, and tried following your instructions with the following result.
m@m-xubuntu:~/scripts$ ls -l name*
-rw-rw-r-- 1 m m 567 May 5 20:28 name-reformat
m@m-xubuntu:~/scripts$ name-reformat
bash: /home/m/scripts/name-reformat: Permission denied
m@m-xubuntu:~/scripts$
…?
Mike
Hi Mike.
No problem. It’s just that you need to make the file executable. In the scripts directory where you are now, enter:
chmod +x name-reformat
You can check again with
ls -l name*
and you will see the "X"s have reappeared in the permissions list.
You could create several false mp3 file names of the right format to make a better test - e.g. 12-02-1923.mp3 etc.
Then enter :
name-reformat
This will reverse the name of your test mp3 file(s).
If that’s worked OK then we will operate on your real mp3 files - if you have backed them up to another directory.
Keith
Bingo!
Yes. I’ve backed up the directory Keith…
Jolly good.
The code only works on files in the directory from which it is called. So to use the code on your real files, change to the directory where those files are and just type the script name like you did just now:
name-reformat
Let me know how it goes.
Keith
Keith,
Sample of output below…
Old file: 25_04_2022.mp3 New file: 25_04_2022-25_04_2022.mp3-25_04_2022.mp3.mp3
Old file: 25_05_2022.mp3 New file: 25_05_2022-25_05_2022.mp3-25_05_2022.mp3.mp3
Old file: 26_01_2022.mp3 New file: 26_01_2022-26_01_2022.mp3-26_01_2022.mp3.mp3
Old file: 26_04_2022.mp3 New file: 26_04_2022-26_04_2022.mp3-26_04_2022.mp3.mp3
Old file: 26_05_2022.mp3 New file: 26_05_2022-26_05_2022.mp3-26_05_2022.mp3.mp3
Old file: 27_01_2022.mp3 New file: 27_01_2022-27_01_2022.mp3-27_01_2022.mp3.mp3
Old file: 27_02_2022.mp3 New file: 27_02_2022-27_02_2022.mp3-27_02_2022.mp3.mp3
Old file: 27_03_2022.mp3 New file: 27_03_2022-27_03_2022.mp3-27_03_2022.mp3.mp3
M
Ah. I have no idea at all what’s going on, in fact I would struggle to write a script to do that!
It works on my system:
keith@E5570:~/scripts$ ls -1
11-222-333.mp3
DD-MM-YYYY.mp3
DD-MM-YYYY.txt
name-reformat
tttt-cc-zz.mp3
yyyy-mm-dd.mp3
keith@E5570:~/scripts$ name-reformat
Old file: 11-222-333.mp3 New file: 333-222-11.mp3
Old file: DD-MM-YYYY.mp3 New file: YYYY-MM-DD.mp3
Old file: tttt-cc-zz.mp3 New file: zz-cc-tttt.mp3
Old file: yyyy-mm-dd.mp3 New file: dd-mm-yyyy.mp3
keith@E5570:~/scripts$
I’ll have a think and PM you.
Would you please email the script back to me so I can check if it’s been corrupted?
Keith