Linux shell脚本:创建一个具有当前日期名称的文件夹



我正在尝试制作一个简单的备份脚本,但我在创建一个带有名称当前日期的文件夹时遇到问题

我的脚本就是这样,基本上问题出在最后一行

drivers=$(ls /media/)
declare -i c=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)

输出:

(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
在/

media/0362-BA96/之前存在的路径经过三重检查

已解决: 做了贾尼斯所说的最终剧本的样子

drivers=$(ls /media/)
declare -i c=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"

尝试将其更改为:

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

: 在 FAT 上无效(它用于指定磁盘)。一些 M$ 无效字符在 GNU/Linux 系统上有效,但避免它们更安全(只需替换为 . )。使用以下日期格式

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

它应该适用于大多数文件系统,但对于 MS DOS FAT 来说可能太长了。更多信息,你可以在这里找到。

相关内容

  • 没有找到相关文章

最新更新