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

use*_*422 5 linux tar

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

/home/dir1/file1
/home/dir2/file2
/home/dir3/file3
...
/home/dir10000/file10000
Run Code Online (Sandbox Code Playgroud)

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

file1
file2
file3
Run Code Online (Sandbox Code Playgroud)

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

谢谢你帮助大家!

trs*_*trs 6

没有cat或没有|其他程序解决这个问题的一种方法:

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
Run Code Online (Sandbox Code Playgroud)

与Aaron Okano暗示的不同,xargs如果您在命令行中指定的单个/少数父目录中有很长的文件列表,则无需管道.


小智 -3

打开命令行并输入

tar -cvf NAME.tar /home/dir1/file1 /home/dir1/file2 /home/dir1/file3
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)