Renaming files recursively according to standard given in Linux commands

3

I'm trying to rename files by replacing certain conforming patterns, but I wanted to do this job recursively and still could not.

- / doc
| ----- text1.txt
| ----- texto2.txt
| - / doc2
_ | ----- text1.txt
_ | ----- texto2.txt

Follow my

#!/bin/bash
#padrao = $1
#substitui = $2

ls | rename "s/$1/$2/g"

This script renames well what I want to modify, but only in the requested folder, I tried the "ls - R" but it did not work to replace the requested folder in the internal directories.

    
asked by anonymous 27.10.2014 / 02:07

2 answers

6

Replaces ls with find, it will bring up the list of files and sub-files. Something like:

find . -name "*.txt" | rename "s/$1/$2/g"

The dot brings the local directory, but you can even replace it with other directories, eg

find /home/diretorio_de_usuario -name "*.txt" | rename "s/$1/$2/g"
    
27.10.2014 / 02:46
0

I made a script to rename screen capture files that appeared in DESKTOP: image.sh

#!/bin/bash
cd ~/Desktop/
j=0
IFS="\n"
for i in 'ls *.png'
do
  j=$(($j+1))
    if [ ! -e $1"$j".png ] ; then
      mv $i $1"$j".png
    else
    continue
    fi
done
cd -

How does the script work?

  
  • You run the script by providing an argument that will be the prefix of the names.   Ex: $ sh image.sh <prefixo> .
  •   

    Command Output:

         

    prefixo1.png, prefixo2.png, etc...

         
  • The script is designed to list .png images, but this can be modified by putting it to be information passed by the command line with argument.

  •   
  • If new images are inserted into the directory it will rename it from the last stop.

  •   

    I suggest you make some additional scripts to apply the above code to each existing directory. It could be: $ find . -type d that will list all directories where you are. The xargs command can be very useful for manipulating find output.

    There are endless possibilities!

    I hope I have helped!

        
    09.06.2015 / 14:43