[英]Bash - How can I archive and compress files in subdirectories but only with a certain filename
I have a directory structure that looks like: 我有一个目录结构,看起来像:
main_directory/
directory1:
sub_directory1:
files:
myfile.txt
otherfile.txt
sub_directory2:
files:
myfile.txt
otherfile.txt
sub_directory3:
files:
myfile.txt
otherfile.txt
sub_directory4:
files:
myfile.txt
otherfile.txt
directory2:
sub_directory1:
files:
myfile.txt
otherfile.txt
sub_directory2:
files:
myfile.txt
otherfile.txt
sub_directory3:
files:
myfile.txt
otherfile.txt
sub_directory4:
files:
myfile.txt
otherfile.txt
I am trying to figure out (by trial and error because I'm not an expert at Linux) how to only gzip the myfile.txt
files in all the directories. 我试图找出(通过反复试验,因为我不是Linux的专家)如何只在所有目录中gzip
myfile.txt
文件。 Since they all have the same filename in different paths (there was no way around this), I need to be able to keep the files path in the archive as well. 由于它们在不同的路径中都具有相同的文件名(没有办法解决这个问题),我需要能够将文件路径保存在存档中。 So the final gzipped tar file I am looking to create would have the contents:
所以我想要创建的最终gzipped tar文件将包含以下内容:
mytar.tar.gz
main_directory/directory1/sub_directory1/files/myfile.txt
main_directory/directory1/sub_directory2/files/myfile.txt
main_directory/directory1/sub_directory3/files/myfile.txt
main_directory/directory1/sub_directory4/files/myfile.txt
main_directory/directory2/sub_directory1/files/myfile.txt
main_directory/directory3/sub_directory2/files/myfile.txt
main_directory/directory4/sub_directory3/files/myfile.txt
main_directory/directory5/sub_directory4/files/myfile.txt
Is there a simple bash
way to do this? 有一个简单的
bash
方式来做到这一点? I suppose I could write a python
script to do it, but that seems overkill. 我想我可以写一个
python
脚本来做它,但这似乎有点过分。
Does anyone have any advice? 有人有建议吗?
这克服了另一个答案中描述的这个问题。
find main_directory/ -name "myfile.txt" | tar -czvf mytar.tar.gz -T -
Assuming there are not too many files, you can do something like: 假设没有太多文件,您可以执行以下操作:
cd main_directory/..
find main_directory -name "myfile.txt" | xargs tar zcf mytar.tar.gz
In the event that there are a lot of files, you can pipe the file list into a file/stream and pass that into tar. 如果有大量文件,您可以将文件列表传输到文件/流中并将其传递给tar。
find main_directory -name "myfile.txt" -print0 | tar zcf myar.tar.gz --null -T -
This prints out the filenames separated by nulls ( -print0
to find
) and instructs tar
to parse that correctly from stdin
; 这将打印出由
-print0
分隔的文件名( -print0
to find
),并指示tar
从stdin
中正确解析该文件名; using nulls ensures that any special characters in directories are handled properly 使用空值可确保正确处理目录中的任何特殊字符
使用足够新的(4.0.0+我相信)版本的bash(以及许多其他shell),以下内容将起作用:
tar -czf mytar.tar.gz main_directory/**/myfile.txt
If the directory structure is indeed this regular, the wildcard 如果目录结构确实是常规的,那么通配符
main_directory/*/*/files/myfile.txt
will match the files you want. 将匹配您想要的文件。 However, if there are many files, you may need to revert to
find
/ xargs
in order to avoid the "argument list too long" ( ARG_MAX
) problem. 但是,如果有很多文件,您可能需要恢复
find
/ xargs
以避免“参数列表太长”( ARG_MAX
)问题。
If there are files named myfile.txt
which you do not want to include because their path does not match the wildcard exactly, there are certainly ways to exclude them from find
, too; 如果有一个名为
myfile.txt
文件,你不想包含它们,因为它们的路径与通配符不完全匹配,那么肯定有办法将它们从find
排除; perhaps then this additional constraint should be stated in the question. 或许这个额外的约束应该在问题中说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.