首页 > 解决方案 > 批量解压到文件夹路径

问题描述

我有带有域的基于 Linux 的服务器:

domain1.com
   public_html
      archive1.zip
domain2.com
   public_html
      archive2.zip
domain3.com
   public_html
      archive3.zip

我正在尝试通过以下方式运行解压缩:

unzip */public_html/*.zip

它适用于一个域(除了它在我启动此命令的文件夹中解压缩的事实)。

有没有办法批量解压缩以获得以下输出:

domain1.com
   public_html
      archive1.zip
      content_of_archive1
domain2.com
   public_html
      archive2.zip
      content_of_archive2
domain3.com
   public_html
      archive3.zip
      content_of_archive3

感谢您的时间和帮助!

标签: linuxbashunzip

解决方案


这需要一个 for 循环:

for archive in */public_html/*.zip; do
   unzip "$archive" -d "$( dirname "$archive" )"
done

PS:我打算用来移动到目录,但@Mihir在他们的评论中pushd提供了更简单的解决方案。-d

PPS:您不需要脚本来运行它 - 只需在 bash 提示符下逐字输入,包括换行符。或者你可以把它折叠成一个衬里:

for archive in */public_html/*.zip; do unzip "$archive" -d "$( dirname "$archive" )"; done

要么,bash命令历史记录将让您返回到您键入和修改的内容和/或根据需要重新运行它。


推荐阅读