加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用tar来存档不同目录结构的不同目录中的文件

发布时间:2020-12-14 01:15:24 所属栏目:Linux 来源:网络整理
导读:我在不同的子目录中有几百到几千个文件,它们都位于一个父目录中,例如 /home/dir1/file1/home/dir2/file2/home/dir3/file3.../home/dir10000/file10000 我如何使用tar,所以我的tar存档看起来像这样? file1file2file3 我可以确保文件名是唯一的.我不想包含原
我在不同的子目录中有几百到几千个文件,它们都位于一个父目录中,例如

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

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

file1
file2
file3

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

谢谢你帮助大家!

解决方法

解决这个问题的一种方法,无需考虑或参与其他程序:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读