Unzipping files with space in name

Asked

Viewed 254 times

2

I’m trying to unpack a large mass of files on linux and I’m trying to use a for that looks like this:

for z in *.zip; do unzip $z ; done

but I get the following message:

unzip: cannot find or open divide, divide.zip or divide.ZIP. unzip: cannot find or open install, install.zip or install.ZIP.

The biggest problem is because there are spaces in the names, etc.

I’ve done another one for replacing the spaces with '_' but I wonder if there is any way to unpack without replacing the spaces.

2 answers

2


You can insert double quotes in the for and in charge unzip that the bash will work well with filenames in which there is space:

for z in "*.zip"; do unzip "$z" ; done
  • Caution: the first quotation marks are extra -- maybe: for z in *.zip; do unzip "$z" ; done to expand the * ?

  • No, the double quotes are just the question, because the colleague was having trouble with filenames with spaces.

1

It is possible, for this, you have to find a way to separate your files. As the division of your files will be by line break (because of ls), we will use it to divide your Strings.

zfiles=$(ls *.zip) # lista os arquivos .zip e armazena em $zfiles
IFS='
' # IFS é a variável responsável por definir o caractere da quebra de linha
lista=($zfiles) # criará um array com os arquivos

for x in "${!lista[@]}"; do
    unzip "$x" #como os nomes terão espaço, mantenha o $x dentro de aspas duplas
done

Inside of IFS has a line break, and in for to ! is mandatory in ${!lista[@]}, so that the vector is traversed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.