I have the folder "Backup yzT Jan-01-2013". I've made also a script for do a backup everyday.

The problem I'm facing is how to rename the folder day by day according to date? Get the new name is easy, it's just

"/home/yzT/Backup yzT `date '+%b-%d-%Y'`"

However how can I select the folder "Backup yzT Jan-01-2013" exactly? Because every day it will be different.

I've tried

mv "/home/yzT/Backup yzT *" "/home/yzT/Backup yzT `date '+%b-%d-%Y'`

hoping that the mv path works like a regular expression, but sadly, it doesn't.

So how can I rename the folder?

  • What is the name of the file you're trying to rename? That's really not clear in your description. (You're also not doing yourself a favor at all by having spaces in those paths, and by using a localized date format.) – Mat Jan 1 '13 at 14:06
  • The directory today is /home/yzT/Backup yzT Jan-01-2013. Tomorrow it will be /home/yzT/Backup yzT Jan-02-2013, and so on. I want to rename the directory itself not the files it contains. – yzT Jan 1 '13 at 14:10
  • Then I don't understand what you're trying to do at all. Why do you want to rename yesterday's backup folder? – Mat Jan 1 '13 at 14:10
  • There is only one directory. Tomorrow the script will first rename the current folder ("Backup yzT Jan-01-2013" to "Backup yzT Jan-02-2013") and then do the tasks it has to do using rsync. The renaming part is what I don't know to do. For daily use is pointless be renaming according to date, but I'm planning to do a weekly backup also. – yzT Jan 1 '13 at 14:16
  • 1
    Well that's exactly my question. Why not just call that folder "Backup yzT"? What point is there in having a date there at all if you're keeping only one? If you have a daily and weekly backup, have "Daily_Backup" and "Weekly_Backup" directories. – Mat Jan 1 '13 at 14:18

Your mv command isn't working because you're quoting the *, so filename globbing isn't kicking in.

You could try something like:

mv "/home/yzT/Backup yzT "* "/home/yzT/Backup yzT `date '+%b-%d-%Y'`

(Notice that the * was moved out of the quotes. And this is not a regular expression match, it is globbing.)

This will match all filenames that start with Backup yzT in that directory, so it doesn't work for your scheme with a potential Backup yzT weekly directory.

That being said, you're really making things hard for yourself with those directory names. If you chose a scheme like:

yzT.daily.timestamp yzT.weekly.timestamp etc...

you wouldn't have to worry about the spaces making all your scripts harder to write correctly. This scheme also allows you to easily glob for daily backups independently of weekly ones, or backups for two users independently.

Finally, using a date format such as YYYYMMDD.HHMMSS is simpler to deal with in scripts than localized timestamps - they sort ok by default for instance, and are easy to parse (no need to worry about language, mapping of month names, etc.).

  • ok done, the * was the problem – yzT Jan 1 '13 at 14:55

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.