AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 725064
Accepted
Manuel Jordan
Manuel Jordan
Asked: 2022-11-16 18:44:30 +0800 CST2022-11-16 18:44:30 +0800 CST 2022-11-16 18:44:30 +0800 CST

什么时候强制使用 tar 命令的“--keep-newer-files”选项?

  • 772

关于tar命令

介绍

例如:

source
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

如果是numbers.tar.gz通过命令创建文件,那么tar -czf numbers.tar.gz numbers现在我们有:

source
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

考虑mv numbers.tar.gz target命令是否被执行,因此我们有:

target
 numbers.tar.gz

如果tar -xzf numbers.tar.gz命令被执行,那么我们有

target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

因此,作为一般概述,我们有:

source
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content
target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

超越控制

让我们假设以下简单更新:

target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222222 content <--- updated
  003.txt # with the 333 content

如果目录中的tar -xzf numbers.tar.gz命令target被执行因此002.txt文件被覆盖,所以它的 222222 内容返回到 222。直到这里我没问题。

为了保证新数据的安全或避免不需要的覆盖,可以使用--keep-old-files和--skip-old-files选项,根据tar(1) - Linux 手册页,它表示:

-k, --keep-old-files
don't replace existing files when extracting, treat them as errors
--skip-old-files
don't replace existing files when extracting, silently skip over them

因此执行以下两个命令:

tar --keep-old-files -xzf numbers.tar.gz
tar --skip-old-files -xzf numbers.tar.gz

发生以下情况:

  • 前者始终显示tar: numbers/222.txt: Cannot open: File exists错误消息并且数据保持安全(保留在 222222 中)
  • 后者什么都不显示 - 如果v使用该选项则例外 - 它显示tar: numbers/222.txt: skipping existing file消息;并且数据保持安全(保留在 222222 中)。用于脚本用途。

到这里为止,一切都很好。

经过研究我找到了这个--keep-newer-files选项,并再次根据tar(1) - Linux man page,它表明:

--keep-newer-files
don't replace existing files that are newer than their archive copies

因此执行以下命令:

tar --keep-newer-files -xzf numbers.tar.gz

发生以下情况:

  • 出现tar: Current ‘numbers/222.txt’ is newer or same age消息并且数据保持安全(与 222222 保持一致)

实际上,此--keep-newer-files选项的作用与--skip-old-filesabout 相同,以避免覆盖但显示不同的消息。

问题

  • 什么时候强制使用命令的--keep-newer-files选项而不是和选项?tar--keep-old-files--skip-old-files

我想知道这个选项是强制性的具体情况是什么。

tar
  • 1 1 个回答
  • 26 Views

1 个回答

  • Voted
  1. Best Answer
    Stephen Kitt
    2022-11-16T21:44:03+08:002022-11-16T21:44:03+08:00

    --keep-newer-files如果您想保留上次修改源文件后在目标上所做的更改,并将目标上的任何旧版本替换为源中的较新版本,则该选项很有用。

    为了说明这两个选项之间的区别,您需要另一条信息,即每个文件的时间戳。考虑以下:

    source
      numbers
        001.txt # timestamp t1, contents 1
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    

    这些文件被复制以target保留它们的时间戳。

    source
      numbers
        001.txt # timestamp t1, contents 1
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    target
      numbers
        001.txt # timestamp t1, contents 1
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    

    现在你修复源(观察001.txt部分):t2

    source
      numbers
        001.txt # timestamp t2, contents 111
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    target
      numbers
        001.txt # timestamp t1, contents 1
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    

    目标002.txt上也有人编辑(观察部分):t3

    source
      numbers
        001.txt # timestamp t2, contents 111
        002.txt # timestamp t1, contents 222
        003.txt # timestamp t1, contents 333
    target
      numbers
        001.txt # timestamp t1, contents 1
        002.txt # timestamp t3, contents 22222
        003.txt # timestamp t1, contents 333
    

    您创建一个新存档,并将其解压缩到目标上:

    • 没有任何选项,所有文件都被提取,因此目标最终在源中001.txt和002.txt源中具有相同的内容,丢失对目标所做的更改002.txt;
    • 与--keep-old-files,001.txt和002.txt未被提取,并且目标保留其过时版本的001.txt;
    • with --keep-newer-files,001.txt被提取并覆盖现有的目标文件,因为现有文件比存档中的文件旧,但002.txt没有被提取,因为现有文件比存档中的文件新。
    • 2

相关问题

  • 从 /var/log 提取和归档以 .log 结尾的文件时删除路径名

  • 此 curl/tar 示例中裸破折号的含义

  • tar 目录只发送修改过的文件块

  • 在 Solaris 上;查找、附加到 tar 球并压缩问题

  • bsdtar:如何避免覆盖现有文件信息?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve