如何将~/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会被转换成zip1target_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
相关产品推荐
相关产品推荐

