You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将~/Downloads下的Zip包以自身名称为目录提取到~/Documents?

解决方案

没问题,这正是你需要的命令,能完美实现你想要的目录结构:

for f in ~/Downloads/*.zip; do
  target_dir="$HOME/Documents/$(basename "$f" .zip)"
  unzip -d "$target_dir" "$f"
done

命令细节解释:

  • for f in ~/Downloads/*.zip: 遍历~/Downloads目录下所有的.zip文件,每个文件的完整路径会被存在变量f里
  • $(basename "$f" .zip): 这部分用basename命令提取文件名,同时去掉.zip后缀——比如~/Downloads/zip1.zip会被转换成zip1
  • target_dir="$HOME/Documents/...": 拼接出目标目录路径,也就是~/Documents/zip1、~/Documents/zip2这类
  • unzip -d "$target_dir" "$f": 调用unzip工具,把当前的zip文件$f提取到指定的目标目录$target_dir中

可选优化项:

  • 如果需要强制覆盖已存在的文件,给unzip加上-o参数:
    for f in ~/Downloads/*.zip; do
      target_dir="$HOME/Documents/$(basename "$f" .zip)"
      unzip -o -d "$target_dir" "$f"
    done
    
  • 如果想要跳过已存在的文件不覆盖,加上-n参数即可,unzip会自动跳过已有文件,避免重复操作

内容的提问来源于stack exchange,提问作者user10804351

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:33:24