gpt4 book ai didi

linux - tar 目录中的文件并将存档放在另一个目录中

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:07 28 4
gpt4 key购买 nike

我正在尝试将目录 (echo 1) 中的所有文件压缩到存档目录中名为 [directory name].tar 的 tar 存档(第二个 echo)。这是正确的吗?

#!/bin/bash

#Author
#Josh
++++++++++++++++++++++++++
echo "Please enter the name of a folder to archive"
++++++++++++++++++++++++++
echo "Please enter a foldername to store archives in"
++++++++++++++++++++++++++
touch fname
+++++++++++++++++++++++++
tar fname2.tar

最佳答案

dir_to_be_tarred=
while [ ! -d "$dir_to_be_tarred" ]; do
echo -n "Enter path to directory to be archived: " # -n makes "echo" not output an ending NEWLINE
read dir_to_be_tarred
if [ ! -d "$dir_to_be_tarred" ]; then # make sure the directory exists
echo "Directory \"$dir_to_be_tarred\" does not exist" # use quotes around the directory name in case user enter an empty line
fi
done

storage_dir=
while [ ! -d "$storage_dir" ]; do
echo -n "Enter path to directory where to store the archive: "
read storage_dir
if [ ! -d "$storage_dir" ]; then
echo "Directory \"$storage_dir\" does not exist"
fi
done

tarfile="$storage_dir"/`date '+%Y%m%d_%H%M%S'`.tar.gz # the tar archive is named "YYYYMMDD_HHMMSS.tar.gz"
tar cfz "$tarfile" "$dir_to_be_tarred"
echo 'Your archive is in:
'`ls -l "$tarfile"`

关于linux - tar 目录中的文件并将存档放在另一个目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780956/

28 4 0