使用 tar 命令创建带有日期的压缩目录

using tar command to create a compressed directory with date

我需要在 ubuntu 12.04 32 位 运行 这个命令

tar -zcvf $ BACKUP_DIR/backup_`date "+% d-% m-% Y"`.tgz $HOME/tmp

但是我无法获取带有日期的目录。当我执行命令时 shell returns

date +%d -%m -%y: the order is not found
tar: Eliminating the `/ 'initial of the names
/tmp/
/tmp/ssh-IfHQiXoK1342/
tar: /tmp/ssh-IfHQiXoK1342/agent.1342: the socket is not taken into account
/tmp/.ICE-unix/
tar: /tmp/.ICE-unix/1342: the socket is not taken into account
/tmp/pulse-xvCP3trutUHz/
/tmp/pulse-xvCP3trutUHz/pid
tar: /tmp/pulse-xvCP3trutUHz/native: the socket is not taken into account
/tmp/.X11-unix/
tar: /tmp/.X11-unix/X0: the socket is not taken into account
/tmp/keyring-ELkby8/
tar: /tmp/keyring-ELkby8/pkcs11: the socket is not taken into account
tar: /tmp/keyring-ELkby8/ssh: the socket is not taken into account
tar: /tmp/keyring-ELkby8/control: the socket is not taken into account
tar: /tmp/keyring-ELkby8/gpg: the socket is not taken into account
tar: /tmp/geany_socket.0e082422: the socket is not taken into account
/tmp/.X0-lock
/tmp/pulse-PKdhtXMmr18n/
/tmp/unity_support_test.0

结果是一个名为 backup_ 的 .tgz,我尝试将 更改为 ' ' 或 "" 但它也不起作用。我需要对 运行 命令做什么?

您的命令替换的输出正在被分词。你必须引用它。为清楚起见,使用 $() 而不是反引号。

tar -zcvf "$BACKUP_DIR/backup_$(date "+% d-% m-% Y")".tgz "$HOME/tmp"