Linux下如何找出并打包文件成 tar 包

Q:我想找出所有的 *.doc 文件并将它们创建成一个 tar 包,然后存储在 /nfs/backups/docs/file.tar 中。是否可以在 Linux 或者类 Unix 系统上查找并 tar 打包文件?

find 命令用于按照给定条件在目录层次结构中搜索文件。tar 命令是用于 Linux 和类 Unix 系统创建 tar 包的归档工具。

Linux下如何找出并打包文件成 tar 包

How to find and tar files on linux unix

让我们看看如何将 tar 命令与 find 命令结合在一个命令行中创建一个 tar 包。

Find 命令

语法是:

  1. <span class="kwd">find</span><span class="pun">/</span><span class="pln">path</span><span class="pun">/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">search </span><span class="pun">-</span><span class="pln">name </span><span class="str">"file-to-search"</span><span class="pun">-</span><span class="pln">options</span>
  2. <span class="com">##</span><span class="pun">找出所有</span><span class="typ">Perl</span><span class="pun">(*.</span><span class="pln">pl</span><span class="pun">)文件</span><span class="com">##</span>
  3. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.pl"</span><span class="pun">-</span><span class="kwd">print</span>
  4. <span class="com">##</span><span class="pun">找出所有</span><span class="pun">*.</span><span class="pln">doc </span><span class="pun">文件</span><span class="com">##</span>
  5. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="kwd">print</span>
  6. <span class="com">##</span><span class="pun">找出所有</span><span class="pun">*.</span><span class="pln">sh</span><span class="pun">(</span><span class="pln">shell </span><span class="pun">脚本)并运行</span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l </span><span class="pun">命令</span><span class="com">##</span>
  7. <span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">iname </span><span class="str">"*.sh"</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l </span><span class="pun">{}</span><span class="pun">+</span>

最后一个命令的输出示例:

  1. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">1169</span><span class="typ">Apr</span><span class="lit">4</span><span class="lit">2017</span><span class="pun">./</span><span class="pln">backups</span><span class="pun">/</span><span class="pln">ansible</span><span class="pun">/</span><span class="pln">cluster</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">.</span><span class="pln">build</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">1500</span><span class="typ">Dec</span><span class="lit">6</span><span class="lit">14</span><span class="pun">:</span><span class="lit">36</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">cloudflare</span><span class="pun">.</span><span class="pln">pure</span><span class="pun">.</span><span class="pln">url</span><span class="pun">.</span><span class="pln">sh</span>
  3. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">13</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">cmspostupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> postupload</span><span class="pun">.</span><span class="pln">sh</span>
  4. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">12</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">cmspreupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> preupload</span><span class="pun">.</span><span class="pln">sh</span>
  5. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">14</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">cmssuploadimage</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> uploadimage</span><span class="pun">.</span><span class="pln">sh</span>
  6. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">13</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">faqpostupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> postupload</span><span class="pun">.</span><span class="pln">sh</span>
  7. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">12</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">faqpreupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> preupload</span><span class="pun">.</span><span class="pln">sh</span>
  8. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">14</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">faquploadimage</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> uploadimage</span><span class="pun">.</span><span class="pln">sh</span>
  9. <span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">778</span><span class="typ">Nov</span><span class="lit">6</span><span class="lit">14</span><span class="pun">:</span><span class="lit">44</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mirror</span><span class="pun">.</span><span class="pln">sh</span>
  10. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">136</span><span class="typ">Apr</span><span class="lit">25</span><span class="lit">2015</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">nixcraft</span><span class="pun">.</span><span class="pln">com</span><span class="pun">.</span><span class="lit">301.sh</span>
  11. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">547</span><span class="typ">Jan</span><span class="lit">30</span><span class="lit">2017</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">paypal</span><span class="pun">.</span><span class="pln">sh</span>
  12. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">531</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">postupload</span><span class="pun">.</span><span class="pln">sh</span>
  13. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">437</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">preupload</span><span class="pun">.</span><span class="pln">sh</span>
  14. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">1046</span><span class="typ">May</span><span class="lit">18</span><span class="lit">2017</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">purge</span><span class="pun">.</span><span class="pln">all</span><span class="pun">.</span><span class="pln">cloudflare</span><span class="pun">.</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">sh</span>
  15. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">13</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">tipspostupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> postupload</span><span class="pun">.</span><span class="pln">sh</span>
  16. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">12</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">tipspreupload</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> preupload</span><span class="pun">.</span><span class="pln">sh</span>
  17. <span class="pln">lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">14</span><span class="typ">Dec</span><span class="lit">31</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">tipsuploadimage</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-></span><span class="pln"> uploadimage</span><span class="pun">.</span><span class="pln">sh</span>
  18. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">1193</span><span class="typ">Oct</span><span class="lit">18</span><span class="lit">2013</span><span class="pun">./</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">uploadimage</span><span class="pun">.</span><span class="pln">sh</span>
  19. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">29</span><span class="typ">Nov</span><span class="lit">6</span><span class="lit">14</span><span class="pun">:</span><span class="lit">33</span><span class="pun">./.</span><span class="kwd">vim</span><span class="pun">/</span><span class="pln">plugged</span><span class="pun">/</span><span class="pln">neomake</span><span class="pun">/</span><span class="pln">tests</span><span class="pun">/</span><span class="pln">fixtures</span><span class="pun">/</span><span class="pln">errors</span><span class="pun">.</span><span class="pln">sh</span>
  20. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">1</span><span class="pln"> vivek vivek </span><span class="lit">215</span><span class="typ">Nov</span><span class="lit">6</span><span class="lit">14</span><span class="pun">:</span><span class="lit">33</span><span class="pun">./.</span><span class="kwd">vim</span><span class="pun">/</span><span class="pln">plugged</span><span class="pun">/</span><span class="pln">neomake</span><span class="pun">/</span><span class="pln">tests</span><span class="pun">/</span><span class="pln">helpers</span><span class="pun">/</span><span class="pln">trap</span><span class="pun">.</span><span class="pln">sh</span>

Tar 命令

创建 /home/vivek/projects 目录的 tar 包,运行:

  1. <span class="pln">$ </span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">cvf </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">vivek</span><span class="pun">/</span><span class="pln">projects</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">vivek</span><span class="pun">/</span><span class="pln">projects</span>

结合 find 和 tar 命令

语法是:

  1. <span class="kwd">find</span><span class="pun">/</span><span class="kwd">dir</span><span class="pun">/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">search</span><span class="pun">/</span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">rvf out</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">{}</span><span class="pln"> \;</span>

或者

  1. <span class="kwd">find</span><span class="pun">/</span><span class="kwd">dir</span><span class="pun">/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">search</span><span class="pun">/</span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">rvf out</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">{}</span><span class="pun">+</span>

例子:

  1. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">rvf </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">all</span><span class="pun">-</span><span class="pln">doc</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="kwd">tar</span><span class="str">"{}"</span><span class="pln"> \;</span>

或者

  1. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">rvf </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">all</span><span class="pun">-</span><span class="pln">doc</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="kwd">tar</span><span class="str">"{}"</span><span class="pun">+</span>

这里,find 命令的选项:

  • -name "*.doc":按照给定的模式/标准查找文件。在这里,在 $HOME 中查找所有 *.doc 文件。
  • -exec tar ... :对 find 命令找到的所有文件执行 tar 命令。

这里,tar 命令的选项:

  • -r:将文件追加到归档末尾。参数与 -c 选项具有相同的含义。
  • -v:详细输出。
  • -f out.tar : 将所有文件追加到 out.tar 中。

也可以像下面这样将 find 命令的输出通过管道输入到 tar 命令中:

  1. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.doc"</span><span class="pun">-</span><span class="pln">print0 </span><span class="pun">|</span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">cvf </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">file</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">--</span><span class="kwd">null</span><span class="pun">-</span><span class="pln">T </span><span class="pun">-</span>

传递给 find 命令的 -print0 选项处理特殊的文件名。--null-T 选项告诉 tar 命令从标准输入/管道读取输入。也可以使用 xargs 命令:

  1. <span class="kwd">find</span><span class="pln"> $HOME </span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.sh"</span><span class="pun">|</span><span class="kwd">xargs</span><span class="kwd">tar</span><span class="pln"> cfvz </span><span class="pun">/</span><span class="pln">nfs</span><span class="pun">/</span><span class="pln">x230</span><span class="pun">/</span><span class="kwd">my</span><span class="pun">-</span><span class="pln">shell</span><span class="pun">-</span><span class="pln">scripts</span><span class="pun">.</span><span class="pln">tgz</span>

有关更多信息,请参阅下面的 man 页面:

  1. <span class="pln">$ </span><span class="kwd">man</span><span class="kwd">tar</span>
  2. <span class="pln">$ </span><span class="kwd">man</span><span class="kwd">find</span>
  3. <span class="pln">$ </span><span class="kwd">man</span><span class="kwd">xargs</span>
  4. <span class="pln">$ </span><span class="kwd">man</span><span class="kwd">bash</span>

作者简介:

作者是 nixCraft 的创造者,是一名经验丰富的系统管理员,也是 Linux 操作系统/Unix shell 脚本培训师。他曾与全球客户以及 IT、教育、国防和太空研究以及非营利部门等多个行业合作。在 Twitter、Facebook 和 Google+ 上关注他。


via: https://www.cyberciti.biz/faq/linux-unix-find-tar-files-into-tarball-command/

作者:Vivek Gite 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐