Is it possible to automatically rename (mp3) files, to there date in YY/MM/DD format, using files on raspbian?

I have a audio recorder (A external Device) set up to automatically record mp3 files to a specific folder and upload them to the cloud. I want to name them their date beforehand so the folder (via. Grive2) has files like 19-09-05.mp3, not XXXX125Y2G.mp3.

I am not sure how to automate file renaming, since I am a linux noob, and have no prior scripting knowledge

  • / is not a valid character for a filename since that's the directory separator. – jordanm Sep 6 '19 at 0:20
  • oh, didn't think of that oops. – FredTheDoggy Sep 6 '19 at 0:23

To rename a single file:

mv test.mp3 "rec-$(date -r test.mp3 '+%Y-%m-%d.mp3')"

This uses the file's timestamp to rename it to rec-YYYY-MM-DD.mp3. The rec- can be removed, but IMO is worthwhile because it provides more info about what the file is than just a date. Or use another prefix if you prefer.

To rename all .mp3 files in a directory that haven't already been renamed:

cd /path/to/directory
for f in *.mp3; do
  if [[ ! "$f" =~ ^rec-[0-9]{4}-[0-9]{2}-[0-9]{2}\.mp3$ ]] ; then
    mv "$f" "rec-$(date -r "$f" '+%Y-%m-%d.mp3')"

BTW, you should probably configure your recording software to use a specific and distinctive filename pattern (e.g. Test-nnnn.mp3), to make it easier to avoid renaming other .mp3 files. i.e. for f in Test-*.mp3 is a lot more specific than for f in *.mp3. Or just get it to store the recordings in a directory not used for anything else.

| improve this answer | |
  • But how do I configure this to happen every hour or something? – FredTheDoggy Sep 6 '19 at 17:09
  • you save it in a script, make it executable with chmod +x, and set up a cron job to run it hourly. – cas Sep 6 '19 at 23:37

We could use some information about what software you use to record, and then how they are placed in a directory.

Hence upload seems to be automated there also runs a deamon somewhere that watches that directory and then uploads new files seen in there.

If all of that is 3rd party you will likely only have chance to use options of the software that generates the mp3 files in the first place. In that case you want to look into that softwares documentation if it can use system variables e.g. date/time/hour/something or alike integrated by its developers in the options that allow you to determine the output file.

Otherwise you might want to see if the software that uploads all registered files in that directory can be modified to only recognize files that follow a specific naming pattern.

following that you could use inotify-tools to watch the directory and build a script to rename incoming new files reading the mp3 files metadata tags using ffmpeg.

I hope this helps.

| improve this answer | |
  • I use a external recorder, that just adds the files to a directory, and I am uploading using Grive2 – FredTheDoggy Sep 6 '19 at 0:22
  • 1
    I'd recommend to check if the external recorder can be told to use a specific naming pattern for the output files. Thats probably going to be the easiest way over anything else. – Seemax Sep 6 '19 at 0:24
  • Yeah, I tried that but it can only name it like "Test-1.mp3" and not dates @Seemax – FredTheDoggy Sep 6 '19 at 0:26
  • Thats rather sad then. In that case you probably want to export to a seperate directory first, run inotify on it and use inotify to execute a script utilizing ffmpeg that reads the mp3 files date/time metadata tags, renames the file according to the extracted information and then moves the file into the upload dir of grive. I am not a scripting hero either, just look around stackexchange and search engine for scripting examples for inotify and ffmpeg and manufacture something from that. – Seemax Sep 6 '19 at 0:31
  • I've been doing that, but the scripting is really complicated and Im not 100% sure how to do it, so that was part of the reason I asked – FredTheDoggy Sep 6 '19 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.