使用 tar 用 while 循环中的文件更新存档?

使用 tar 用 while 循环中的文件更新存档?

我这里列出了问题:2019 年 10 月升级:pip 升级失败 · 问题 #5844 · msys2/MINGW-packages:基本上,pipMSYS2 拒绝更新,因为它看到一些文件。pacman -Syu基本上转储这个:

...
:: Proceed with installation? [Y/n] y
:: Retrieving packages...
 mingw-w64-x86_64-gl...     4.6 MiB  4.97M/s 00:01 [#####################] 100%
 mingw-w64-x86_64-gn...  1942.9 KiB  1045K/s 00:02 [#####################] 100%
(18/18) checking keys in keyring                   [#####################] 100%
(18/18) checking package integrity                 [#####################] 100%
(18/18) loading package files                      [#####################] 100%
(18/18) checking for file conflicts                [#####################] 100%
error: failed to commit transaction (conflicting files)
python2-pip: /c/msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.py exists in filesystem
python2-pip: /c/msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc exists in filesystem
...

我不想卸载 pip,而且似乎(pacman“文件系统上存在”错误)我可以“只是”将文件移动到其他地方,然后运行更新。当然,我想保留要移动的文件的原始文件位置。

所以,我想,也许我可以将tar这些文件放在存档中(使用--remove-files原始文件也被删除,这样它们就不会再更新),然后在更新后恢复它们 - 如果我需要任何其他软件包,已经安装了它们。

不幸的是,我真的找不到正确的调用,以便tar使用来自while循环的文件更新存档(也就是说,我没有“单行”、以空格分隔的文件列表)。

这会导致存档中只有一个文件:

$ pacman -Syu | grep exists | awk '{print $2}' | while read f; do tar -czf /tmp/bckp.tar "$f"; done
:: Proceed with installation? [Y/n] y
error: failed to commit transaction (conflicting files)
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
...

$ tar tzvf /tmp/bckp.tar
-rw-r--r-- user/None       13854 2019-09-02 09:07 c/msys64/usr/lib/python3.7/site-packages/pip/_vendor/urllib3/packages/rfc3986/validators.py

这甚至不会运行:

$ pacman -Syu | grep exists | awk '{print $2}' | while read f; do tar -czvf --strip-components=1 --append /tmp/bckp.tar.gz "$f"; done
:: Proceed with installation? [Y/n] y
error: failed to commit transaction (conflicting files)
tar: You may not specify more than one '-Acdtrux', '--delete' or  '--test-label' option
Try 'tar --help' or 'tar --usage' for more information.
tar: You may not specify more than one '-Acdtrux', '--delete' or  '--test-label' option
Try 'tar --help' or 'tar --usage' for more information.
...

这也不会:

$ pacman -Syu | grep exists | awk '{print $2}' | while read f; do tar -zvf --strip-components=1 --append /tmp/bckp.tar.gz "$f"; done
:: Proceed with installation? [Y/n] y
error: failed to commit transaction (conflicting files)
tar: Cannot update compressed archives
Try 'tar --help' or 'tar --usage' for more information.
tar: Cannot update compressed archives
Try 'tar --help' or 'tar --usage' for more information.
...

这最终会抱怨现有文件“没有这样的文件或目录”:

$ pacman -Syu | grep exists | awk '{print $2}' | while read f; do tar -vf --strip-components=1 --append /tmp/bckp.tar.gz "$f"; done
:: Proceed with installation? [Y/n] y
error: failed to commit transaction (conflicting files)
tar: Removing leading `/' from member names
tar: /tmp/bckp.tar.gz: Cannot stat: No such file or directory
/c/msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.py
tar: Removing leading `/' from hard link targets
tar: Exiting with failure status due to previous errors
tar: Removing leading `/' from member names
tar: /tmp/bckp.tar.gz: Cannot stat: No such file or directory
/c/msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc
...

/c/msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc因此,假设我有一个循环中的文件列表(绝对文件路径),形式为while- 我怎样才能将它们全部添加到存档中.tar,这样它们最终会在其中删除绝对路径的第一部分(即提到的文件最终会tar以 ) 形式存档msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc

答案1

好的,我想我终于找到了正确的调用 - 请注意,sed添加是因为--strip-components适用于提取仅有的;因此,在这里我们/c/从文件路径字符串中手动删除,然后指示tar更改目录以/c/使用-C开关:

$ pacman -Syu | grep exists | awk '{print $2}' | sed 's_/c/__' | xargs tar -cvf /tmp/bckp.tar -C /c/
:: Proceed with installation? [Y/n] y
error: failed to commit transaction (conflicting files)
msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.py
msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc
msys64/usr/lib/python2.7/site-packages/pip/_internal/distributions/__init__.py
msys64/usr/lib/python2.7/site-packages/pip/_internal/distributions/__init__.pyc
...

...并仔细检查:

$ tar tvf /tmp/bckp.tar
-rw-r--r-- user/None        3360 2019-09-02 09:08 msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.py
-rw-r--r-- user/None        4154 2019-09-02 09:08 msys64/usr/lib/python2.7/site-packages/pip/_internal/commands/debug.pyc
-rw-r--r-- user/None         861 2019-09-02 09:08 msys64/usr/lib/python2.7/site-packages/pip/_internal/distributions/__init__.py
-rw-r--r-- user/None         986 2019-09-02 09:08 msys64/usr/lib/python2.7/site-packages/pip/_internal/distributions/__init__.pyc
...

相关内容