简体   繁体   中英

using tar command to create a compressed directory with date

I need to run this command in ubuntu 12.04 32bit

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

but I can't obtain the directory with date. When I execute the command the 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

And the result is a .tgz called backup_ , I tried to change for ' ' or "" but also it doesn't work. What I need to do to run the command?

The output of your command substitution is being wordsplit. You must quote it. For clarity, use $() instead of backticks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM