Script para renomear arquivos

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

UNIX GURU UNIVERSE DICA UNIX

Dica Unix 2470 - 06 de outubro de 2006

http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Script para renomar arquivos

Se você precisa renomear arquivos, utilize o seguinte script Perl :

#!/usr/bin/perl

rename: renames files according to the expr given on the command line.

The expr will usually be a ’s' or ‘y’ command, but can be any valid

perl command if it makes sense. Takes a list of files to work on or

defaults to ‘*’ in the current directory.

e.g.

rename ’s/.flip$/.flop/' # rename *.flip to *.flop

rename s/flip/flop/ # rename flip to flop

rename ’s/^s.(.*)/$1.X/' # switch sccs filenames around

rename ’s/$/.orig/' /.[ch] # add .orig to your source files in */

rename ‘y/A-Z/a-z/’ # lowercase all filenames in .

rename ‘y/A-Z/a-z/ if -B’ # same, but just binaries!

rename chop *~ # restore all ~ backup files

use Getopt::Std; my ($subst, $name);

if (!&getopts(“nfq”) || $#ARGV == -1) { die “Usage: rename [-fnq] [file file…] -f : Force the new filename even if it exists already -n : Just print what would happen, but don’t do the command -q : Don’t print the files as they are renamed e.g. : rename ’s/.c/.c.old/' * rename -q ‘y/A-Z/a-z/’ *n”; }

$subst = shift; # Get perl command to work on @ARGV = < *> if $#ARGV < 0; # Default to complete directory

foreach $name (@ARGV) { $_ = $name; eval “$subst;"; die $@ if $@; next if -e $_ && !$opt_f; # Skip if the file exists if asked to. mext if $_ eq $name; if ($opt_n) { print “mv $name $_n”; next; } print “mv $name $_n” if !$opt_q; rename($name,$_) or warn “Can’t rename $name to $_, $!n”; }

Coloque este script com o nome rename no diretório /usr/local/bin. Tenha certeza que /usr/local/bin seja o $PATH mais conveniente para você.

Para entrar: http://www.ugu.com/sui/ugu/show?tip.subscribe Para sair: http://www.ugu.com/sui/ugu/show?tip.unsubscribe Para enviar uma dica: http://www.ugu.com/sui/ugu/show?tip.today


DECLARAÇÃO: TODAS AS DICAS DE UNIX SÃO PROPIEDADE DA UNIX GURU UNIVERSE E NÃO SÃO PARA SER VENDIDAS, IMPRESSAS OU USADAS SEM O CONSENTIMENTO POR ESCRITO DA UNIX GURU UNIVERSE. TODAS AS DICAS SÃO "USADAS POR SEU PROPIO RISCO". UGU ADVERTE PARA TESTAR TODAS AS DICAS EM UM AMBIENTE QUE NÃO ESTEJA EM PRODUÇÃO.Unix Guru Universe - www.ugu.com - tips@ugu.com - Copyright 1994-2006