gpt4 book ai didi

linux - 如何在没有目录结构的情况下使用 tar 归档来自不同目录的文件

转载 作者:IT王子 更新时间:2023-10-29 01:13:44 24 4
gpt4 key购买 nike

我在不同的子目录中有数百到数千个文件,这些文件都位于一个父目录中,例如

/home/dir1/file1
/home/dir2/file2
/home/dir3/file3
...
/home/dir10000/file10000

如何使用 tar 使我的 tar 存档看起来像这样?

file1
file2
file3

我可以确保文件名是唯一的。我不想包含原始目录结构。

感谢您的帮助!

最佳答案

无需 cating 或 |ing 到其他程序即可解决此问题的一种方法:

tar --transform 's,.*/,,g' -cvf mytarfile.tar /path/to/parent_dir

# --transform accepts a regular expression similar to sed.
# I used "," as a delimiter so I don't have to escape "/"
# This will replace anything up to the final "/" with ""
# /path/to/parent_dir/sub1/sub2/subsubsub/file.txt -> file.txt
# /path/to/parent_dir/file2.txt -> file2.txt

与 Aaron Okano 暗示的不同,如果您有一个很长的文件列表在您可以指定的单个/几个父目录中,则无需通过管道传输到 xargs命令行。

关于linux - 如何在没有目录结构的情况下使用 tar 归档来自不同目录的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18675523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com