从不同目录中的同名文件制作存档

发布于 2024-08-29 19:41:01 字数 271 浏览 4 评论 0原文

我有一些名称相同但位于不同目录下的文件。例如,path1/filea、path1/fileb、path2/filea、path2/fileb、...

将文件归档的最佳方法是什么?在这些目录下,还有许多其他文件我不想归档。我突然想到使用 Bash,可能是 ar、tar 和其他命令,但不知道具体该怎么做。

重命名文件似乎使文件名有点复杂。我倾向于将目录结构保留在存档中。或者我可能错了。欢迎其他想法!

谢谢和问候!


编辑:

例子真的很好!

I have some files with same names but under different directories. For example, path1/filea, path1/fileb, path2/filea, path2/fileb,....

What is the best way to make the files into an archive? Under these directories, there are many other files under these directories that I don't want to make into the archive. Off the top of my head, I think of using Bash, probably ar, tar and other commands, but am not sure how exactly to do it.

Renaming the files seems to make the file names a little complicated. I tend to keep the directory structure inside the archive. Or I might be wrong. Other ideas are welcome!

Thanks and regards!


EDIT:

Examples would be really nice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人哭 2024-09-05 19:41:01

您可以将 tar--exclude PATTERN 选项一起使用。有关更多信息,请参阅手册页。
要排除文件,您可以参阅此页面获取示例。

you can use tar with --exclude PATTERN option. See the man page for more.
To exclude files, you can see this page for examples.

怪异←思 2024-09-05 19:41:01

您可以给 find 命令指定多个目录来搜索。

# example: create archive of .tex files
find -x LaTeX-files1 LaTeX-files2 -name "*.tex" -print0 | tar --null --no-recursion -uf LaTeXfiles.tar --files-from - 

You may give the find command multiple directories to search through.

# example: create archive of .tex files
find -x LaTeX-files1 LaTeX-files2 -name "*.tex" -print0 | tar --null --no-recursion -uf LaTeXfiles.tar --files-from - 
软甜啾 2024-09-05 19:41:01

要仅将文件名为“filea”或“fileb”的文件从 /path/to/source 递归复制到 /path/to/archive,您可以使用:

rsync -avm --include='file[ab]' -f 'hide,! */' /path/to/source/ /path/to/archive/

  • '*/' 是匹配“任意目录”的模式
  • <代码>'! */' 匹配除目录之外的任何内容(即文件)
  • '隐藏,! */'表示隐藏所有文件
  • 过滤规则按顺序应用,并且应用第一个匹配的规则。
    --include='file[ab]' 具有优先权,因此如果文件与 'file[ab]' 匹配,则会包含该文件。
    任何其他文件都会从要传输的文件列表中排除。

Another alternative is to use the find...exec pattern:

mkdir /path/to/archive
cd /path/to/source
find . -type f -iname "file[ab]" -exec cp --parents '{}' /path/to/archive ";"

To recursively copy only files with filename "filea" or "fileb" from /path/to/source to /path/to/archive, you could use:

rsync -avm --include='file[ab]' -f 'hide,! */' /path/to/source/ /path/to/archive/


  • '*/' is a pattern which matches 'any directory'
  • '! */' matches anything which is not a directory (i.e. a file)
  • 'hide,! */' means hide all files
  • Filter rules are applied in order, and the first rule that matches is applied.
    --include='file[ab]' has precedence, so if a file matches 'file[ab]', it is included.
    Any other file gets excluded from the list of files to transfer.

Another alternative is to use the find...exec pattern:

mkdir /path/to/archive
cd /path/to/source
find . -type f -iname "file[ab]" -exec cp --parents '{}' /path/to/archive ";"
蒲公英的约定 2024-09-05 19:41:01

我用来为不同目录中具有相同名称的文件制作 tar 球的方法是

$find <path> -name <filename> -exec tar -rvf data.tar '{}' \;

i.e. tar [-]r --append

希望这会有所帮助。

What I have used to make a tar ball for the files with same name in different directories is

$find <path> -name <filename> -exec tar -rvf data.tar '{}' \;

i.e. tar [-]r --append

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文