简体   繁体   English

Bash - 如何在子目录中存档和压缩文件,但只能使用特定的文件名

[英]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 ),并指示tarstdin中正确解析该文件名; 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.

相关问题 如何修复此 Bash 函数以从指定路径开始并递归列出其子目录和这些子目录等 - How can I fix this Bash function to start at a specified path and recursively list its subdirectories and the subdirectories of those etc 如何仅打印包含特定字符串的文件的文件名部分? - How to print only the filename part of files that contain a certain string? 如何在bash-子目录中用`find`匹配文件? - How to match files with `find` in bash - also in subdirectories? 在Linux中,如何将多个文件归档并压缩为一个文件并删除源文件? - In linux how to archive and compress multiple files into one and remove source files? 如何只压缩未压缩的文件? - How to compress only uncompressed files? 如何制作 bash 脚本,在其中我可以将某些文件移动到基于文件中的字符串命名的某些文件夹? - How can I make a bash script where I can move certain files to certain folders which are named based on a string in the files? 如何找到只有所有者具有特定权限的文件? - How can I find files that only have certain permission for owner? `find` 只在某些子目录中 - `find` only in certain subdirectories 如何将目录中的大量zip文件移动到bash中指定数量的多个子目录中? - How do I move a large number of zip files in a directory to a specified number of multiple subdirectories in bash? Linux bash 只解压一个文件和归档文件 zip - Linux bash unzip only one files with archive file zip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM