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
    • 最新
    • 标签
主页 / computer / 问题

问题[tar](computer)

Martin Hope
WesternGun
Asked: 2022-02-12 00:41:26 +0800 CST

tar 使用管道删除文件而不提取文件:正确的语法是什么?

  • 7

我正在使用 GNU tar 来处理 tar(docker 映像的层)来修改其中的一些 jar。我在做:

  • 将图像作为 tar 保存到磁盘
  • 提取它,所以我将每一层都放在一个目录中
  • 进入每一层,我有一个layer.tar,一个json和一个VERSION
  • 迭代所有*/*.jar文件layer.tar,试图找到一些类文件
  • 如果我找到它们,提取具有文件树结构的 jar,从中删除类文件,然后将其放回layer.tar,覆盖原始 jar
  • 将每一层打包回一个新的 tar,使用 docker 加载并稍后推送(尚未完成)

我为此创建了一个脚本,它几乎可以完成工作,但有 2 个罐子一个接一个,一个带有要删除的类,另一个没有它。

#!/bin/bash

# tar needs find to package without ".". u for update, c for create
function pack_all_without_period() {
    find $1 -printf "%P\n" -type f -o -type l -o -type d | sudo tar -$3vf $2 --no-recursion -C $1 -T -
}

if [ -z $1 ]; then
    printf "Save the image as tar, extract, and enter each layer to remove the vulnerable classes(JMSAppender/SocketServer/SimpleSocketServer)\nPlease provide the image name. \n"
    exit 1
fi
dir="log4j-1.x-fix"
image_tar=amq-image-to-fix.tar
if [ ! -d $dir ]; then 
    mkdir $dir
fi
# save image to tar
docker save $1 -o $image_tar
# extract tar
tar xf $image_tar -C $dir
# each layer is extracted to a folder, each folder has a "layer.tar". 
# Go into each folder, extract `layer.tar`, and use `jar` to remove the classes
# and package them back to `layer.tar` (-a to append), and delete the extracted folders.
# at last, package all layers + manifest.json and so back into another tar, WITHOUT COMPRESSION
cd $dir
# enter layer and exit
for layer in */; do
    echo Processing layer $layer
    cd $layer
    # tar does not support overwrite, as tape cannot be overwritten; so I wanted to remove the original jar from tar, 
    # then append it back with tar -u/-A/-r; but then I found tar --delete is extremely slow(by design)
    # so at last I have to extract all files and package them back
    mkdir temp
    sudo tar --extract --directory=temp --file layer.tar --wildcards "*.jar"   # file tree is preserved, so package them back is easy
    if [[ $? -eq 0 ]]; then 
        for f in $(find . -mindepth 2 -name "*.jar" -not -type l -printf "%P\n"); do # exclude jolokia.jar(link)
            sudo jar -tvf $f | grep -E "(*JMSAppender*.class|*SocketServer.class|*log4j*.class)"
            if [[ $? -eq 0 ]]; then
                echo Found classes in $f
                read -p "Do you want to remove these classes? (Y/N) " option
                if [[ $option == 'Y' || $option == 'y' ]]; then
                    echo Removing class file from $f
                    sudo zip -d $f "*JMSAppender.class" "*SocketServer.class" "*SimpleSocketServer.class"
                    ######### here I need to delete the original jar with the classes I just deleted, but I don't know how ############
                else continue
                fi
            else
                continue
            fi

        done
        # append folders to tar, without leading "."
        echo Appending modified folders to layer.tar anew
        pack_all_without_period temp layer.tar r
    fi
    sudo rm -r $(find . -maxdepth 1 -mindepth 1 -type d -print)
    cd .. # back to $dir
done
cd ..

# tar will always include a folder "." as root. This function get rid of it, so the archive
# only contains the content of the folder
# compress will preserve ownership and group by default; and to extract while preserving the same info,
# we use '--same-owner', which is used by default when using sudo. 
# again, append all layers and files to new tar, without leading "."
echo after processing all layers, we are at $(pwd)
pack_all_without_period $dir amq-image-fixed.tar c
sudo rm -Irv $dir $image_tar




但我发现:

  1. tar只能追加,不会覆盖。所以我改变了它,所以我会先删除原来的 jarlayer.tar然后追加
  2. 然后我发现它tar --delete some/path/foo.tar不适用于tar --file xxx --delete path-to-jar. GNU tar 文档声称--delete可以在标准输入和标准输出的管道中工作(https://www.gnu.org/software/tar/manual/html_node/delete.html)但是正确的语法是什么?我尝试了这些但没有工作:
    sudo tar tf layer.tar $f | sudo tar --delete #not deleting
    sudo tar xf layer.tar --exclude $f | sudo tar cf layer.tar -T -  # create tar of size 0

还有一些注意事项:

  • 我不想提取所有文件,因为每一层都包含/usr或/boot我不想处理。我的罐子基本上都在下面/opt(不是 100% 确定)
  • 我需要保留所有权/时间戳等。这就是我使用的原因sudo(但不确定这是否能达到我的目的)

我使用这样的脚本:

./remove-log4j-1.x-classes.sh registry.access.redhat.com/jboss-amq-6/amq63-openshift:1.4-44.1638430186

请帮忙,谢谢!

编辑:我现在尝试:

tar tf layer.tar -O | tar f - --delete $f > layer-new.tar

或者

zcat -f layer.tar | tar f - --delete $f > layer-new.tar

但我因错误而失败:

tar: opt/amq/lib/optional/log4j-1.2.17.redhat-1.jar: Not found in archive
tar: Exiting with failure status due to previous errors
linux tar
  • 1 个回答
  • 148 Views
Martin Hope
usert4jju7
Asked: 2021-11-18 01:40:51 +0800 CST

tar 命令不起作用

  • 5

我试图弄清楚为什么以下 tar 命令不起作用 -

我已经尝试了以下 2 个版本,但都不起作用 -

版本 1

tar -c --use-compress-program=pigz -f /home/jhonst/data_lake/1m/UX.tar -C '/home/jhonst/data_lake/1m/*.UX.csv'

我看到的错误是

tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.

版本 2

tar -c --use-compress-program=pigz -f /home/jhonst/data_lake/1m/UX.tar -C '/home/jhonst/data_lake/1m/*.UX.csv' .

我看到的错误是

    tar: 
/home/jhonst/data_lake/1m/*.UX.csv: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now

请有人指导我做错了什么

bash tar
  • 1 个回答
  • 539 Views
Martin Hope
Hot JAMS
Asked: 2021-07-23 23:40:24 +0800 CST

用于操作大量 tar.gz 档案的 bash 工作流程

  • 5

我正在使用由 4 个子目录组成的目录

ls -t
pnmrnp40_to_69  pnmrnp9028_to_9100  pnmrnp00_to_39  pnmrnp70_to_9028

在每个 prmnp* 子目录中,有许多填充属于 *.tar.gz 存档或 *.md5sub (我不知道它是什么,所以应该删除它)。

charlie@Precision-7920-Tower:~/Documents/script/mega_data/pnmrnp/pnmrnp40_to_69$ ls -t
ligands57_dir_results.tar.gz.md5sum  ligands40_dir_results.tar.gz.md5sum
ligands57_dir_results.tar.gz         ligands69_dir_results.tar.gz
ligands69_dir_results.tar.gz.md5sum  ligands68_dir_results.tar.gz
ligands68_dir_results.tar.gz.md5sum  ligands67_dir_results.tar.gz
ligands67_dir_results.tar.gz.md5sum  ligands66_dir_results.tar.gz
ligands66_dir_results.tar.gz.md5sum  ligands65_dir_results.tar.gz

我需要一个简单的 bash 工作流程,它将移动到每个子目录

  1. 删除所有 *.md5sub
  2. 将所有 *.tar.gz 解压到同一个子文件夹(保留原始存档的名称)。

这是我在 bash 中的工作流程:

#!/bin/bash
# assuming that the script is in the folder contained all subdirectories
dir="$PWD"

# loop each subdirectory
for subdir in ${dir}
cd ${subdir}
# unzip each archive to the same place
for tar in *.tar.gz; do
tar xzvf $tar
done
# return to initial dir
cd ..
done

有没有可能使这个脚本更有效,以便它可以适应大量的档案?

bash tar
  • 1 个回答
  • 33 Views
Martin Hope
Mainak
Asked: 2020-12-05 06:27:43 +0800 CST

将文件夹与 tarball 进行比较

  • 6

我有这样的目录结构

Code
├── cse701.tgz
└── cse701
    ├── cse701.md
    ├── CSE701.md
    ├── CSE701_pandoc.md
    ├── cse701.pdf
    ├── cse.pdf
    ├── default.yaml
    ├── html2tex.log
    ├── test.md
    └── missing.md

tarball 的结构如下:

drwxrwxr-x zenith/zenith     0 2020-12-02 20:18 cse701/
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/default.yaml
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/CSE701.md
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/CSE701_pandoc.md
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/html2tex.log
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/test.md
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/cse701.pdf
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/cse.pdf
-rw-rw-r-- zenith/zenith     0 2020-12-02 21:19 cse701/cse701.md
tar --diff -zf cse701.tgz  cse701/
tar --compare -zf cse701.tgz  ./cse701/

这篇文章中显示的 diff 和 compare 选项应该指出磁盘中缺少一个不在 tarball 上的文件。

但是,gnu tar 文档说它会显示文件是否已被修改,但会忽略文件系统中在存档中没有相应成员的文件。如果文件在存档中表示但在文件系统中不存在,则 tar 报告差异。

那么,在创建 tarball 后,如何查找是否有新文件添加到文件夹中?

linux tar
  • 1 个回答
  • 178 Views
Martin Hope
Naomi Fridman
Asked: 2020-11-13 23:11:53 +0800 CST

奇怪的 tar 存档

  • 6

我从另一台服务器传输的文件很少。这些文件假设包含带有代码和数据的目录存档。

文件具有 .tar 扩展名。

当我运行时:

tar -xvf xxxxx.tar

我得到以下结果: 在此处输入图像描述

每个目录的内容是: 在此处输入图像描述

json文件的内容是: 在此处输入图像描述 它是什么?如何取消存档?

linux tar
  • 1 个回答
  • 59 Views
Martin Hope
Kuba hasn't forgotten Monica
Asked: 2020-06-03 07:49:16 +0800 CST

由于@PaxHeader 文件,7zip 在提取 POSIX tar 档案时出现问题

  • 10

POSIX tar 档案包含 POSIX 标头作为名为@PaxHeader. 7zip 尽职尽责地提取它们,它们都发生了冲突,因为它们的名称都相同。7z 然后抱怨文件正在使用,并以错误结束。它确实提取了其他文件,并且扩展的标题在 Windows 上无论如何都没有用。

.tar.xz在 Windows 上构建 Qt 时,在提取 Qt 档案的上下文中出现了这个问题。这些.tar.xz文件比文件更紧凑.zip,它有助于减少 CI 环境中的负载,其中 Qt 可能经常被下载和重建。

有没有办法解决这个问题?

7-zip tar
  • 2 个回答
  • 3900 Views
Martin Hope
confused
Asked: 2020-05-23 18:45:50 +0800 CST

tar 还原语法仅还原未隐藏的文件

  • 5

仅恢复未隐藏文件的正确语法是什么?我想根据需要手动复制这些内容,而不会批量覆盖已经存在的任何内容。例如,我想忽略 .thumbnails、.cache、.config 等,但复制其他所有内容。我试过了:

tar zxvf --exclude=.* mybackup.tar.gz

但这没有用。有什么建议么?或者这是否只发生在备份端,并且恢复被困在恢复被焦油的任何内容?

tar restore
  • 1 个回答
  • 39 Views
Martin Hope
mortal36
Asked: 2020-05-15 05:19:08 +0800 CST

如何执行 tar 压缩的空运行?

  • 10

我想执行 tar 压缩的空运行并将条目打印到标准输出而不实际创建 tar。

到目前为止,我已经尝试过:

// just spins
$ tar t -O Downloads
$ tar c -O Downloads
tar: Option -O is not permitted in mode -c

tar --help给出以下内容:

⋊> ~ tar --help                                                                                                                                                                                 08:06:22
tar(bsdtar): manipulate archive files
First option must be a mode specifier:
  -c Create  -r Add/Replace  -t List  -u Update  -x Extract
Common Options:
  -b #  Use # 512-byte records per I/O block
  -f <filename>  Location of archive
  -v    Verbose
  -w    Interactive
Create: tar -c [options] [<file> | <dir> | @<archive> | -C <dir> ]
  <file>, <dir>  add these items to archive
  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma
  --format {ustar|pax|cpio|shar}  Select archive format
  --exclude <pattern>  Skip files that match pattern
  -C <dir>  Change to <dir> before processing remaining files
  @<archive>  Add entries from <archive> to output
List: tar -t [options] [<patterns>]
  <patterns>  If specified, list only entries that match
Extract: tar -x [options] [<patterns>]
  <patterns>  If specified, extract only entries that match
  -k    Keep (don't overwrite) existing files
  -m    Don't restore modification times
  -O    Write entries to stdout, don't restore to disk
  -p    Restore permissions (including ACLs, owner, file flags)
bsdtar 3.3.2 - libarchive 3.3.2 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.6
tar
  • 2 个回答
  • 7662 Views
Martin Hope
psad
Asked: 2020-01-11 13:29:01 +0800 CST

tar 不排除目录?

  • 7

我只是在创建 tar 存档时尝试排除几个目录。目录结构相当简单(Centos 6,tar v1.23):

/test/t1
     /t2
     /t3
     ...

每个子目录 (t1, t2, t3, ...) 都包含一些 txt 文件。没有什么不寻常的。

好吧,让我们试试这个:

tar czvf test.tar.gz test/ --exclude={"t2"}

失败,t2子目录包含在存档中。

tar czvf test.tar.gz test/ --exclude={"t2",""}

成功,t2被排除在外 - 正如预期的那样。

我试图在具有相同目录结构的笔记本电脑(Ubuntu18.04,tar v1.29)上重现相同的情况。在这里,两个命令都失败了 - 该t2目录包含在创建的存档中!

  1. 为什么提供的单个目录条目{}不起作用?
  2. 为什么不同环境下的结果不一样?

这里发生了什么?这是关于 tar 版本的吗?Linux发行版依赖?查看 tar 手册(当前,v1.32)或 tar 更改日志给了我任何答案。

linux tar
  • 1 个回答
  • 790 Views
Martin Hope
bleistift2
Asked: 2019-12-22 10:04:58 +0800 CST

从 Tar 存档中提取特定文件时如何指定目标位置?

  • 7

根据新信息,我重新表述了这个问题。旧信息如下,以免使基于它的答案和评论无效。

我有一个tarball.tar要解压缩到的 tarball destination,一个与我当前工作目录不同的目录,所以我使用 tar 的-C选项,它可以满足我的需求。

但是,如果我尝试指定要从 tarball 中提取的文件,该-C选项似乎被忽略了。这些文件被提取到我当前的工作目录中。

tar -xf tarball.tar -C destination

Tar的版本是

$ tar --version
tar (GNU tar) 1.28

这是一个错误还是我的理解-C错误?

最小的工作示例

这是一个显示行为的 bash 脚本。将其存储(或执行)在空目录中

#!/bin/bash -x
set -e                                 # Abort if any of these commands fails

touch file1 file2 file3                # Create files to be archived
tar -cf tarball.tar file1 file2 file3  # Create the archive
rm file1 file2 file3                   # Remove source files
tar -tf tarball.tar                    # Should contain files 1–3

mkdir -p destination                   # Create destination directory
tar -xf tarball.tar file1 file2 -C destination # Extract two files from 
                                       #+ tarball into destination directory

ls .                                   # Should contain only the script itself,
                                       #+ tarball.tar, and destination
ls destination                         # Should contain file1 and file 2

如果我执行脚本,destination则为空并ls .返回

$ ls .
file1 file2 tarball.tar tar.sh

如果我没有指定要提取的文件(因此 tar -xf tarball.tar file1 file2 -C destination 第 9 行变为 tar -xf tarball -C destination),则行为符合预期。ls destination显示file1 file2 file3。

老问题(忽略这个)

我有一个 tar 存档/path/to/backup.tar.gz,其中包含目录home/bleistift2/stuff和home/bleistift2/more_stuff.

为了将这两个目录提取到/home/bleistift2/tmp(在文件系统中,而不是存档中),我发出以下命令。我的理解是-C指定提取位置。目标目录存在。

tar -zxvf /path/to/backup.tar.gz \                 # The archive to extract
home/bleistift2/stuff home/bleistift2/more_stuff \ # The contents to extract
--same-owner -C /home/bleistift2/tmp               # The destination directory

但是,这些目录存储为存档的兄弟姐妹,所以我最终使用/path/to/home/bleistift2/{stuff, more_stuff}而不是/home/bleistift2/tmp/home/bleistift2/{stuff, more_stuff}.

tar
  • 2 个回答
  • 1794 Views

Sidebar

Stats

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

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve