varios renombrar para nombre masivo directorio copiar comando cambiar archivos archivo linux bash shell scripting suse

para - renombrar directorio linux



Búsqueda de múltiples archivos de forma recursiva y cambio de nombre en linux (8)

Para renombrar recursivamente uso los siguientes comandos:

find -iname /*.* | rename -v "s/ /-/g"

Estoy teniendo archivos como a_dbg.txt, b_dbg.txt ... en un sistema Suse 10 . Quiero escribir un script de shell bash que debería cambiar el nombre de estos archivos eliminando "_dbg" de ellos.

Google me sugirió usar el comando rename . Así que rename _dbg.txt .txt *dbg* el comando rename _dbg.txt .txt *dbg* en el CURRENT_FOLDER

Mi CURRENT_FOLDER real contiene los archivos a continuación.

CURRENT_FOLDER/a_dbg.txt CURRENT_FOLDER/b_dbg.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt

Después de ejecutar el comando rename ,

CURRENT_FOLDER/a.txt CURRENT_FOLDER/b.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt

No lo está haciendo recursivamente, cómo hacer que este comando cambie el nombre de los archivos en todos los subdirectorios. Como XX e YY tendré tantos subdirectorios cuyo nombre es impredecible. Y también mi CURRENT_FOLDER tendrá otros archivos también.


Puedes usar find para encontrar todos los archivos coincidentes de forma recursiva:

$ find . -iname "*dbg*" -exec rename _dbg.txt .txt ''{}'' /;

EDIT: lo que el ''{}'' y /; ¿son?

El argumento -exec hace que find ejecute rename para cada archivo coincidente encontrado. ''{}'' será reemplazado con el nombre de ruta del archivo. El último token, /; está solo para marcar el final de la expresión ejecutiva.

Todo lo que se describe muy bien en la página del manual para encontrar:

-exec utility [argument ...] ; True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ``{}'''' appears anywhere in the utility name or the argu- ments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.


Puedes usar esto a continuación.

rename --no-act ''s//.html$//.php/'' *.html */*.html


Scipt arriba se puede escribir en una línea:

find /tmp -name "*.txt" -exec bash -c ''mv $0 $(echo "$0" | sed -r /"s|.txt|.cpp|g/")'' ''{}'' /;


Si solo desea cambiar el nombre y no le importa utilizar una herramienta externa, puede usar rnm . El comando sería:

#on current folder rnm -dp -1 -fo -ssf ''_dbg'' -rs ''/_dbg//'' *

-dp -1 lo hará recursivo a todos los subdirectorios.

-fo implica modo de solo archivo.

-ssf ''_dbg'' busca archivos con _dbg en el nombre del archivo.

-rs ''/_dbg//'' reemplaza _dbg con una cadena vacía.

También puede ejecutar el comando anterior con la ruta del CURRENT_FOLDER:

rnm -dp -1 -fo -ssf ''_dbg'' -rs ''/_dbg//'' /path/to/the/directory


con bash

shopt -s globstar nullglob rename _dbg.txt .txt **/*dbg*


pequeño script que escribí para reemplazar todos los archivos con extensión .txt a .cpp en / tmp y subdirectorios de forma recursiva

#!/bin/bash for file in $(find /tmp -name ''*.txt'') do mv $file $(echo "$file" | sed -r ''s|.txt|.cpp|g'') done


solución clásica:

for f in $(find . -name "*dbg*"); do mv $f $(echo $f | sed ''s/_dbg//''); done