How to renumber recursively numbered files, giving name to other files

1

What I am currently looking to do is rename all files in a folder, where there are also other files of different format. See the example:

PASTA - BEFORE

1.txt 2.txt 3.txt 

Daniela.jpg Amanda.jpg Lucia.jpg

What I can not do is exactly create a loop for with command sed and / or mv so that I can reach the goal.

PASTA - AFTER

Daniela.txt Amanda.txt Lucia.txt 

Daniela.jpg Amanda.jpg Lucia.jpg

Even though it may seem strange to anyone who reads, keep in mind that I want to copy the names and remain extension .

Example of what I tried:

cd /home/$USER/pasta/

QUANTIDADE='ls *.jpg | wc -l'     

LISTA='ls *.txt'
for ARQUIVO in 'seq $QUANTIDADE'
do 
  mv "$ARQUIVO" "$LISTA"
done

cd ..
  

Instructions - For those who wish to have a thought-out idea of what is needed to help with the issue. Here is a step-by-step guide for you to recreate in your MAC system; Minix; OpenBSD; FreeBSD or GNU / Linux my work environment. To know:

1 - Create a directory named "folder" in your home directory / home / user:

$ mkdir pasta

2 - Populate the previously created folder with dummy files:

$ touch 1.txt 2.txt 3.txt /home/$USER/pasta/

$ touch Daniela.jpg Amanda.jpg Lucia.jpg /home/$USER/pasta/

3 - Finally, copy the sample script. Give common execution permission:

$ sudo chmod +x <script.sh> 
  

Now, just perform the tests to see what you give.


I do not have rename command on my system so please anyone who wants to help me by answering the question, do not elaborate with this tool.

Another detail is that, I prefer to use syntax that runs in Bourne shell instead of Bash . Nothing against it, it's just for portability.

    
asked by anonymous 31.01.2018 / 21:51

2 answers

1

If I understood correctly you want to rename all the txt files with the jpg filenames, in the example below I considered that you have the same amount of files of both formats .

#!/bin/bash
# Pega o diretório onde deseja fazer a ação
Dir=$(pwd)
# Lista todos os arquivos do tipo txt
files=$(ls -1 $Dir | grep .txt)
# Lista todos os arquivos do tipo jpg
rename=$(ls -1 $Dir | grep .jpg | cut -d. -f1)
# Declara o array
array=($(echo $rename))
# Posição do array
po=0
# faz o for renomeado arquivo por arquivo, com o nome declarado na possição do array
for i in $files; do
    mv $i ${array[$po]}.txt
    ## Adiciona mais um para alterar a possição do array
    po=$[po + 1]
done

I see several ways to reach your goal, this was the simplest.

    
01.02.2018 / 12:43
2

Due to the portability issue, I decided to bring this script as part of the solution. Noting that the response from fellow colleague Alessandro Schneider was given the height. However, it only has support for Mr. Bash in the syntax array [this is not in the bourne shell]. So I decided to fight a little more in order to do something that would fulfill the task and not use vetores .

See solution:

Bourn Shell

#!/bin/sh

cd /home/$USER/pasta

qtd=$(ls *.jpg | wc -l)

ext1=$(ls -1 *.txt)

ext2=$(ls -1 *.jpg)

num=0
while [ $num -lt $qtd ]
do 
  for j in $ext1
   do
     for p in $ext2
      do
        mv $num.${j##*.} ${p%.*}.txt
        num=$[num + 1]
    done
  done
done

cd ..

Explanation

1 - We define the Shebang for # for being commonly used and distributed on various GNU / Linux systems.

#!/bin/sh   

2 - Enter the folder containing the files to be renamed.

cd /home/$USER/pasta

3 - qtd - that is maximum amount of JPG files in which we will use your name so that we can rename the other TXT files.

qtd=$(ls *.jpg | wc -l)

4 - List TXT files

ext1=$(ls -1 *.txt)

5 - List JPG files

ext2=$(ls -1 *.jpg)

6 - num - that is number start counting, starts at 0

num=0

7 - While [While] - is used to repeat as far as we determine, in my code it will quantify up to the maximum file number '* .jpg'. So while $num is less than $qtd continue, otherwise stop!

while [ $num -lt $qtd ]     

8 - The first and second for in turn, does the work of merge the two stipulated variables at first. Where renamed file-to-file, with the name declared by the position.

  for j in $ext1
    do
    for p in $ext2
        do
  

Note: Here is the process of naming the * .txt files to the name of the * .jpg files.   Notice that between rows of the loop for we have:

    mv $num.${j##*.} ${p%.*}.txt    
  

Note - the command mv followed by Shell Parameter Expansion :

  • We want to capture only the extension after of ". "

    ${j##.}
    
  • We want to capture only the string before . "

    ${p%.*}
    

9 - At this point, we will increase always to the next file and / or file number to be processed.

       num=$[num + 1]           

10 - Not wanting to visualize the output result in terminal [console], we use:

done &>/dev/null    

.. the front of " done ".

Finally, we left the workbook and went back up to the previous directory

cd ..


  

Important! - This script , is in perfect working order with only ordinal numbers: 1 2 3 4 5 6 7 8 9 10 11 12 etc. ...

     

Not being able to operate with due paper on numbers that accompanies in its position tenth, twentieth, thirtieth and so on, the number "0". See:

     

00 01 02 03 04 05 06 07 08 09 .. 020 021 .. 034 035 ... and so on.

    
01.02.2018 / 23:55