Linux shell script: create folder with current date name

I am trying to create a simple backup script, and I had a problem creating a folder with a completed date name

My script is that and basically the problem is in the last line

drivers=$(ls /media/) declare -ic=0 for word in $drivers do echo "($c)$word" c=c+1 done read -n 1 drive echo c=0 for word in $drivers do if [ $c -eq $drive ] then backuppath="/media/$word/backup" fi c=c+1 done echo "doing back up to $backuppath" cp -r /home/stefanos/Programming $backuppath/$(date +%Y-%m-%d-%T) 

Ouput:

 (0)0362-BA96 (1)Data (2)Windows 0 doing back up to /media/0362-BA96/backup cp: cannot create directory `/media/0362-BA96/backup/2012-12-05-21:58:37': Invalid argument 

The path is checked three times, which exists before / media / 0362-BA96 /

solvable : What janisz said, the last script looks like

 drivers=$(ls /media/) declare -ic=0 for word in $drivers do echo "($c)$word" c=c+1 done read -n 1 drive echo c=0 for word in $drivers do if [ $c -eq $drive ] then backuppath="/media/$word/backup" fi c=c+1 done echo "doing back up to $backuppath" backup(){ time_stamp=$(date +%Y_%m_%d_%H_%M_%S) mkdir -p "${backuppath}/${time_stamp}$1" cp -r "${1}" "${backuppath}/${time_stamp}$1" echo "backup complete in $1" } #####################The paths to backup#################### backup "/home/stefanos/Programming" backup "/home/stefanos/Android/Projects" backup "/home/stefanos/Dropbox" 
+4
source share
2 answers

: invalid in FAT (used to indicate a drive). Some of the invalid M $ characters work on GNU / Linux systems, but it is safer to avoid them (just replace with . ). Use the following date format

 date +%Y_%m_%d_%H_%M_%S 

It should work on most file systems, but it may be too long for MS DOS FAT. You will find more information here .

+10
source

Trying to change it to:

 time_stamp=$(date +%Y-%m-%d-%T) mkdir -p "${backuppath}/${time_stamp}" cp -r /home/stefanos/Programming "${backuppath}/${time_stamp}" 
+14
source

All Articles