Changing date format in filenames in Linux [Solved]

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


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:


The output should be something like:
keith@E5570:~/scripts$ name-reformat
Old file: yyyy-mm-dd.mp3 New file: dd-mm-yyyy.mp3

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.




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

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:

  1. download the attached script to your scripts directory to replace the old file.
  2. navigate to the scripts directory
  3. check that the new file is OK by
ls -l name*

Check that the “w” permissions are still there.
You can check its operation again by entering:

  1. Now navigate to your mp3 directory then enter:

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.


Thanks Keith.

I’m very grateful for all your help…



I can’t find an attached script…

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.


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…


Definitely #24. The attachment to this post shows where to look.


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

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?


Of course. Or maybe just cut and paste it into a new post?

PS. Refreshing the page didn’t help.

I’ve emailed the two files and show the script here also (ought to have done that at the start!).


#Check mp3 files exist. If none then exit:
if [[ -z $(ls *.mp3) ]]; then 
  echo "No mp3 files."

#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:
 echo "Old file: $file    New file: $new_file"
#Now change the file name:
 mv $file $new_file


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



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 :


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.



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:


Let me know how it goes.



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


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
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

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?