Como renomear múltiplos arquivos adicionando uma string comum no início dos arquivos?

3

Eu tenho 100 arquivos. Eu quero adicionar um texto 'novo' antes de todos os nomes de arquivo.

Por favor me ajude. Agradecemos antecipadamente.

Exemplo:

file1.txt  --->  new_file1.txt
.
.
.
.
file100.txt  ---> new_file100.txt

Por favor, forneça uma solução para renomear vários arquivos. Aqui está o que eu tentei. Mas esta não é uma solução melhor.

 
bala@SMS:~/test1$ ls -l
total 0
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file1.txt
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file2.txt
-rw-rw-r-- 1 bala bala 0 Aug 31 19:10 file3.txt
bala@SMS:~/test1$ mv file1.txt new_file1.txt
bala@SMS:~/test1$ mv file2.txt new_file2.txt
bala@SMS:~/test1$ mv file3.txt new_file3.txt
bala@SMS:~/test1$ ls
new_file1.txt  new_file2.txt  new_file3.txt
bala@SMS:~/test1$ 
    
por Dipankar Nalui 31.08.2017 / 15:56

4 respostas

3

Você deve usar o loop para alterar o nome do arquivo de vários arquivos:

for file in *
do
    mv -v ${file} new_${file}
done

O mesmo código em uma linha:

for file in *; do mv -v ${file} new_${file}; done
    
por 31.08.2017 / 16:04
2
$ find . -type f -name "file*.txt" -execdir mv {} new_{} \;

Isso localizará todos os arquivos regulares no diretório atual (ou abaixo) que tenham nomes que correspondam ao padrão file*.txt e os renomeiem adicionando o prefixo new_ aos seus nomes.

Isso requer um find que entenda -execdir (a maioria das implementações find modernas). A opção -execdir funciona como -exec , mas executa o utilitário ( mv ) no diretório da coisa encontrada. Além disso, {} conterá o nome de base da coisa encontrada.

Para limitar ao diretório atual somente , adicione -maxdepth 1 em algum lugar antes de -execdir .

bash-4.4$ mkdir dir{1..10}
bash-4.4$ touch dir{1..10}/file{1..10}.txt

bash-4.4$ ls
dir1  dir10 dir2  dir3  dir4  dir5  dir6  dir7  dir8  dir9

bash-4.4$ ls dir5
file1.txt   file2.txt   file4.txt   file6.txt   file8.txt
file10.txt  file3.txt   file5.txt   file7.txt   file9.txt

bash-4.4$ find . -name "file*.txt" -execdir mv {} new_{} \;

bash-4.4$ ls dir5
new_file1.txt    new_file2.txt    new_file4.txt    new_file6.txt    new_file8.txt
new_file10.txt   new_file3.txt    new_file5.txt    new_file7.txt    new_file9.txt
    
por 31.08.2017 / 21:17
0

Você pode usar o comando rename; verifique se você instalou em seu sistema ou o instalou com seu gerenciador de pacotes do SO

[root@archy pippo]# rename -h

Usage:
 rename [options] <expression> <replacement> <file>...

Rename files.

Options:
 -v, --verbose    explain what is being done
 -s, --symlink    act on the target of symlinks

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see rename(1).

no seu caso, você tem que fazer essa simples substituição

[root@archy pippo]# ls -lrta
total 8
drwxr-x--- 6 root root 4096 Aug 31 14:12 ..
-rw-r--r-- 1 root root    0 Aug 31 14:12 file1.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file2.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file3.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 file4.txt
drwxr-xr-x 2 root root 4096 Aug 31 14:24 .
[root@archy pippo]# rename -v file new_file *.txt
'file1.txt' -> 'new_file1.txt'
'file2.txt' -> 'new_file2.txt'
'file3.txt' -> 'new_file3.txt'
'file4.txt' -> 'new_file4.txt'
[root@archy pippo]# ls -lrta
total 8
drwxr-x--- 6 root root 4096 Aug 31 14:12 ..
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file1.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file2.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file3.txt
-rw-r--r-- 1 root root    0 Aug 31 14:12 new_file4.txt
drwxr-xr-x 2 root root 4096 Aug 31 14:27 .
[root@archy pippo]#

et voilà:)

    
por 31.08.2017 / 16:28
0

Use mv com Expansão de contraventamentos em um loop.

for N in {1..100}; do
     mv {,new_}file$N.txt
done

Antes de executar o comando mv , o shell irá expandi-lo para o comando como este para cada arquivo em que $N substituirá o número de sequência correspondente.

mv file$N.txt new_file$N.txt

então isso anexando um prefixo ao nome do arquivo.

    
por 31.08.2017 / 17:45