Home > front end >  How i get the file name with certain extension in bash
How i get the file name with certain extension in bash

Time:02-27

Here im trying to uncompress roms(iso) files they usually come in zip or 7z and once in iso file i will like to compress it again to chd (readable format for the emulator) so i though i can use the command find to look up for the file but looks like when i just execute the find instruction the files are display propletly (one per line) but 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, is worth mention that this iso files are inside a subdirectory name equal than the file itself(without *.iso obvsly) this is what im trying:

#/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;```

CodePudding user response:

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

That's because for does word splitting etc. when its input is a command's output. See Don't Read Lines with For in the bash wiki for details.

One alternative is to use bash's extended globbing features instead of 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
  • Related