qwe*_*rty 349 linux tar backup
我试图压缩文件夹(/var/www/)来~/www_backups/$time.tar这里$time是当前日期。
这就是我所拥有的:
cd /var/www && sudo tar -czf ~/www_backups $time"
Run Code Online (Sandbox Code Playgroud)
我完全迷失了,我已经在这里呆了几个小时了。不确定是否-czf正确。我只是想将所有内容复制/var/www到一个$time.tar文件中,并且我想维护所有文件的文件权限。谁能帮我吗?
Mat*_*Mat 487
Totar和gzip一个文件夹,语法是:
tar czf name_of_archive_file.tar.gz name_of_directory_to_tar
Run Code Online (Sandbox Code Playgroud)
该-带czf是可选的。
如果要tar当前目录,用.来指定。
要动态构造文件名,请使用该date实用程序(查看其手册页了解可用的格式选项)。例如:
cd /var/www &&
tar czf ~/www_backups/$(date +%Y%m%d-%H%M%S).tar.gz .
Run Code Online (Sandbox Code Playgroud)
这将创建一个名为20120902-185558.tar.gz.
在 Linux 上,您可能tar还支持使用j而不是z选项的BZip2 压缩。可能还有其他人。检查本地系统上的手册页。
小智 21
最常见压缩算法的示例
这个问题的标题只是“用 tar 压缩文件夹?” 由于这个标题很笼统,但问题和答案更具体,而且由于这个问题吸引了大量的观点,我觉得添加两个最新示例列表会很有好处存档/压缩和提取/解压缩,以及各种常用的压缩算法。
这些已经在 Ubuntu 18.04.4 上进行了测试。它们对于一般使用来说非常简单,但可以使用上面接受的答案和有用的评论中的技术轻松地集成到 OP 的更具体的问题内容中。
对于更一般的受众要注意的一件事是tar不会自动添加必要的扩展(如.tar.gz) - 用户必须明确添加这些,如下面的命令所示:
# 1: tar (create uncompressed archive) all files and directories in the current working directory recursively into an uncompressed tarball
tar cvf filename.tar *
# 2: Untar (extract uncompressed archive) all files and directories in an uncompressed tarball recursively into the current working directory
tar xvf filename.tar
# 3: tar (create gzipped archive) all files and directories in the current working directory recursively into a tarball compressed with gzip
tar cvzf filename.tar.gz *
# 4: Untar (extract gzipped archive) all files and directories in a tarball compressed with gzip recursively into the current working directory
tar xvf filename.tar.gz # Note: same options as 2 above
# 5: tar (create bzip2'ed archive) all files and directories in the current working directory recursively into a tarball compressed with bzip2
tar cvjf filename.tar.bz2 * # Note: little 'j' in options
# 6: Untar (extract bzip2'ed archive) all files and directories in an tarball compressed with bzip2 recursively into the current working directory
tar xvf filename.tar.bz2 # Note: same options as 2 and 4 above
# 7: tar (create xz'ed archive) all files and directories in the current working directory recursively into a tarball compressed with xz
tar cvJf filename.tar.xz * # Note: capital 'J' in options
# 8: Untar (extract xz'ed archive) all files and directories in an tarball compressed with xz recursively into the current working directory
tar xvf filename.tar.xz # Note: same options as 2, 4, and 6 above
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅 tar手册页(最好man tar在您的特定机器上使用)。下面我直接从手册页总结了上面使用的选项:
-c, --create
创建一个新的存档-x, --extract, --get
从存档中提取文件-v, --verbose
详细列出已处理的文件-z, --gzip
通过 gzip 过滤存档-j, --bzip2
通过 bzip2 过滤存档-J, --xz
通过 xz 过滤存档-f, --file=ARCHIVE
使用归档文件或设备 ARCHIVE
无需-在组合选项前添加 ,也无需=在f选项和文件名之间添加符号。
我从我最近的文章中得到了所有这些,随着我有时间工作,它将进一步扩展为更全面的文章。