我如何在 bash 中获取具有特定扩展名的文件名

How i get the file name with certain extension in bash

我在这里尝试解压缩 roms(iso) 文件,它们通常以 zip 或 7z 格式提供 一旦进入 iso 文件,我想再次将其压缩为 chd(模拟器的可读格式),所以我虽然可以使用命令 find 来查找文件,但看起来就像我只是执行 find 指令时显示的文件正确地(每行一个)但是当我尝试让每个文件名进行处理时,它看起来只是被 space 分割(是的,这个文件中有 spaces)而不是实际的完整文件名,值得一提的是,这个 iso 文件位于一个名称等于文件本身的子目录中(显然没有 *.iso)这就是我正在尝试的:

#/bin/bash
dir="/home/creeper/Downloads/"
dest="/home/creeper/Documents/"
for i in $(find $dir -name '*.7z' -or -name '*.zip' -or -name '*.iso');
do
  
  if [[ $i == *7z ]]
  then
    7z x $i
    rm -fr $i
  fi
 
  if [[ $i == *zip ]]
  then
    unzip $i
    rm -fr $i
  fi
 
 
  if [[ $i == *iso ]]
  then
    chd_file="${i%.*}.chd"
    chdman createcd -i $i -o $chd_file;
    mv -v $chd_file $dest
    rm -fr $i
  fi
done;```

when i try to get each file name to process it looks like it just split by space (yes this files had spaces in it) and not the actual full filename

那是因为for在其输入是命令的输出时进行分词等操作。有关详细信息,请参阅 bash wiki 中的 Don't Read Lines with For

一种替代方法是使用 bashextended globbing 功能而不是 find:

#!/usr/bin/env bash
shopt -s extglob globstar
dir="/home/creeper/Downloads/"
for i in "$dir"/**/*.@(7z|zip|iso); do
  # Remember to quote expansions of $i!
  # ...
done