您好,我创建了一个 shell 脚本来备份目录中的内容。它创建文件但保留整个路径。
这是我的代码
#!/bin/sh
#creating the destination file
dest="../Demo_Projects/backup/"
#the file name to be created
fname=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
#executing the script
tar -cPf $dest$fname "../Demo_Projects/JsonP/"
这会创建 tar 文件,但整个路径会保留在 tar 文件中。 有没有办法让我们只拥有最里面的目录。
我知道这个问题以前有人回答过 tar file preserves full path. how to stop it?
但是当我按照答案-我修改后的代码如下
#!/bin/sh
#creating the destination file
dest="../Demo_Projects/backup/"
#the file name to be created
fname=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
#executing the script
exec('cd /home/abhishek/Demo_Projects/ && tar -cPf $dest$fname /JsonP/')
但这会导致运行时错误
abhishek@pk:~/sh$ ./backup_demo.sh
./backup_demo.sh: 11: ./backup_demo.sh: Syntax error: word unexpected (expecting ")")
我是 shell 脚本的新手,如有任何帮助,我将不胜感激。 谢谢。
最佳答案
这个
#executing the script
exec('cd /home/abhishek/Demo_Projects/ && tar -cPf $dest$fname /JsonP/')
应该是
#executing the script
cd /home/abhishek/Demo_Projects/ && tar -cPf $dest$fname /JsonP/
关于linux - 在创建 tar 文件时,整个路径被创建为 tar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574879/