Portál AbcLinuxu, 2. května 2025 13:13

Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Vložit další komentář
24.2.2005 16:18 finn | skóre: 43 | blog: finnlandia | 49° 44´/13° 22´
Rozbalit Rozbalit vše rename
Odpovědět | Sbalit | Link | Blokovat | Admin
Většina lidí by asi použila rename from to file, které funguje téměř (nebo úplně — Python neznám) stejně, ale každý je svého štěstí strůjcem :)
Užívej dne – možná je tvůj poslední.
24.2.2005 16:31 Michal Marek (twofish) | skóre: 55 | blog: { display: blog; } | Praha
Rozbalit Rozbalit vše Re: rename
Rename je slabota, vůbec nespouští příkazy, které jsou součástí názvů souborů ;-)
24.2.2005 16:39 Michal Čihař | skóre: 61 | blog: Bláboly | Praha
Rozbalit Rozbalit vše Re: rename
No jasně, použít os.rename místo volání mv by tomu ubralo spoustu kouzel :-)
24.2.2005 17:28 Hynek (Pichi) Vychodil | skóre: 43 | blog: Pichi | Brno
Rozbalit Rozbalit vše a rename znáš?
Odpovědět | Sbalit | Link | Blokovat | Admin
#!/usr/bin/perl -w
#
#  This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
#  from Larry Wall's original script eg/rename from the perl source.
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
#

use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
	'v|verbose' => \$verbose,
	'n|no-act'  => \$no_act,
	'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
	warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
	print "$was renamed as $_\n" if $verbose;
    }
    else
    {
	warn  "Can't rename $was $_: $!\n";
    }
}

__END__

=head1 NAME

rename - renames multiple files

=head1 SYNOPSIS

B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>

=head1 DESCRIPTION

C<rename>
renames the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr> 
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
renamed.
If no filenames are given on the command line, filenames will be read
via standard input.

For example, to rename all files matching C<*.bak> to strip the extension,
you might say

	rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

	rename 'y/A-Z/a-z/' *

=head1 OPTIONS

=over 8

=item B<-v>, B<--verbose>

Verbose: print names of files successfully renamed.

=item B<-n>, B<--no-act>

No Action: show what files would have been renamed.

=item B<-f>, B<--force>

Force: overwrite existing files.

=back

=head1 ENVIRONMENT

No environment variables are used.

=head1 AUTHOR

Larry Wall

=head1 SEE ALSO

mv(1), perl(1)

=head1 DIAGNOSTICS

If you give an invalid Perl expression you'll get a syntax error.

=head1 BUGS

The original C<rename> did not check for the existence of target filenames,
so had to be used with care.  I hope I've fixed that (Robin Barker).

=cut
XML je zbytečný, pomalý, nešikovný balast, znovu vynalézané kolo a ještě ke všemu šišaté, těžké a kýčovitě pomalované.
24.2.2005 21:00 snowracer | skóre: 21 | blog: Omniloquent | Praha
Rozbalit Rozbalit vše zsh
Odpovědět | Sbalit | Link | Blokovat | Admin
Pro takové věci je (jako) stvořeno zmv, které je součástí zsh. Doporučuji obojí.

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.