Uso das chaves na linha de comando
Dica publicada em Linux / Comandos
Uso das chaves na linha de comando
Um parâmetro pouco empregado e quase desconhecido em bash é o uso das chaves em um comando.
Melhor mesmo é um exemplo:
$ ls -la
total 8
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 .
drwxr-xr-x 80 elgio elgio 4096 2007-10-04 21:47 ..
$ mkdir Arquivo{01,02,03,04,05,06,07,08,09,10}-teste
$ ls -la
total 18
drwxr-xr-x 12 elgio elgio 4096 2007-10-04 21:50 .
drwxr-xr-x 80 elgio elgio 4096 2007-10-04 21:47 ..
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo01-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo02-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo03-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo04-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo05-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo06-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo07-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo08-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo09-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo10-teste
É como se eu tivesse digitado:
mkdir Arquivo01-teste Arquivo02-teste ...
Quem não conhece, certamente iria fazer isto com um for:
$ for i in 01 02 03 04 05 06 07 08 09 10
do
mkdir Arquivo$i-teste
done
Mas pra que?
Veja outro exemplo:
$ echo teste{0,1,2,3,4,5,6,LIXO}
teste0 teste1 teste2 teste3 teste4 teste5 teste6 testeLIXO
Melhor mesmo é um exemplo:
$ ls -la
total 8
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 .
drwxr-xr-x 80 elgio elgio 4096 2007-10-04 21:47 ..
$ mkdir Arquivo{01,02,03,04,05,06,07,08,09,10}-teste
$ ls -la
total 18
drwxr-xr-x 12 elgio elgio 4096 2007-10-04 21:50 .
drwxr-xr-x 80 elgio elgio 4096 2007-10-04 21:47 ..
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo01-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo02-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo03-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo04-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo05-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo06-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo07-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo08-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo09-teste
drwxr-xr-x 2 elgio elgio 4096 2007-10-04 21:50 Arquivo10-teste
É como se eu tivesse digitado:
mkdir Arquivo01-teste Arquivo02-teste ...
Quem não conhece, certamente iria fazer isto com um for:
$ for i in 01 02 03 04 05 06 07 08 09 10
do
mkdir Arquivo$i-teste
done
Mas pra que?
Veja outro exemplo:
$ echo teste{0,1,2,3,4,5,6,LIXO}
teste0 teste1 teste2 teste3 teste4 teste5 teste6 testeLIXO
mkdir Arquivo{01,02,03,04,05,06,07,08,09,10}-teste
dessa maneira?
mkdir Arquivo{`seq -s ',' 30`}-teste
Tentei adaptar ele pra fazer isso, porém ele cria um único arquivo com todos os números concatenados. Tem jeito isso?
Grande abraço e parabéns pela dica.