Score:0

How to sort files into directories and subdirectories by name

jp flag

I am new in the world of Linux and I need some advice.

I am continuously saving files in one folder in the following format:

20221021-164822-778711241-604184411.mp3
20221021-164911-778711241-607925196.mp3
20221021-165005-778711241-775371830.mp3
20221021-165152-778711241-777604545.mp3
20221021-165328-778711241-739031020.mp3
20221021-165410-778711241-723150081.mp3
20221021-165517-778711241-604144169.mp3
20221021-165612-778711241-737180820.mp3
20221021-165702-778711241-604292686.mp3
20221021-165748-778711241-777603529.mp3

the format is

YYYYMMDD-somenumbers.mp3

I would need cron to put these files into these tree folders every day

/path/YYYY/MM/DD

Do you have any advice, please?

Thank you very much in advance.

in flag
Silly question, but can the application that is making the MP3 files not do that for you? If not, you’ll want to look into shell scripting and write something that will find and move the files, creating directories as required. That script can then be called from a cron job
Score:0
ng flag

This works here:

#!/usr/bin/env bash

# "for" loop to iterate over all *mp3* files
for i in *.mp3; do
    # Use substring to get the name of the directory for each file    
    dr="${i:0:4}/${i:3:2}/${i:6:2}"
    # find out if the directory exists; if it doesn't, create it
    [ -d "$dr" ] || mkdir -p "$dr"
    # Move the file
    mv "$i" "$dr"
done

If you have a lot of files to move every day, you might save yourself a few CPU cycles by creating the entire directory structure in advance:

# Does not allow for leap years
for i in {1..365}; do mkdir -p $(date +'%Y/%m/%d' -d "2021-12-31 +$i days"); done

You could then remove this line:

    [ -d "$dr" ] || mkdir -p "$dr"

You may end up with a lot of empty directories, but they don't take up much space.

Score:0
jp flag

I finally wrote this script and it seems to work:

mkdir /destination-folder/$(date +"%Y")
mkdir /destination-folder/$(date +"%Y")/$(date +"%m")
mkdir /destination-folder/$(date +"%Y")/$(date +"%m")/$(date +"%d")
mv /source-folder/* /destination-folder/$(date +"%Y")/$(date +"%m")/$(date +"%d")

and cron will always run it at 23:50

ng flag
It wasn't clear from your question that all the files you download on a particular day will have that day's date as the first eight characters of the name. That simplifies things quite a bit. If you make the entire directory structure at once, you don't need the calls to `mkdir` in your script at all.
ng flag
A caveat: if, for whatever reason, your script fails to run (for example) today, you'll get today's files in tomorrow's folder tomorrow night.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.