0

I have the following directory structure:

/media/storage/sqlbackup/CUSTOMER1
/media/storage/sqlbackup/CUSTOMER2
...
/media/storage/sqlbackup/CUSTOMER*

Each CUSTOMER* directory may contain subdirectories named daily, weekly, and monthly. If a CUSTOMER* directory does not contain daily OR weekly OR monthly, I want it to be created, if it does, then I want it to remain.

Before: CUSTOMER1/daily

After: CUSTOMER1/{daily,weekly,monthly}

I was trying to do this with clever use of find, but trying to return all that don't match.

  • 1
    What do you want created if there are no daily, weekly, monthly directories for a given customer? All of them? Just one? Which one? – roaima Nov 22 '19 at 18:09
  • 1
    So you want to create all missing subdirectories? – roaima Nov 22 '19 at 18:10
  • Yes, @roaima. Correct. – Kahn Nov 22 '19 at 18:10
6

You can create the directories while hiding any error related to the directory already existing:

for custDir in /media/storage/sqlbackup/CUSTOMER*
do
    mkdir -p "$custDir"/{daily,weekly,monthly}
done

You cannot use /media/storage/sqlbackup/CUSTOMER*/{daily,weekly,monthly} because the {...} sequence is expanded before the wildcard, and a wildcard pattern will only match files/directories that exist.

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.