help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to bulk rename files


From: Stephane Chazelas
Subject: Re: [Help-bash] how to bulk rename files
Date: Thu, 12 Apr 2012 12:24:20 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2012-04-11 16:56:20 -0400, Greg Wooledge:
[...]
> find . -type f ! -name '*.*' \
>   -exec sh -c 'for f; do mv "$f" "$f.txt"; done' _ {} +
> 
> (assuming you wanted to add .txt)

It calls one mv per file which is going to take a long time for
millions of files.

Alternative is to use a shell with a builtin mv like zsh (which
also has a zmv helper function for this kind of task), or use a
dedicated tool like mmv as already suggested, or use perl or any
programming language that can do the renaming without spawning a
new command:

find . -type f ! -name '*.*' -exec perl -e '
  for $f (@ARGV) {rename "$f", "$f.txt" or warn "rename $f: $!"}' {} +

Beware that perl's rename() clobbers any existing file (that is,
if there's both a file "foo" and "foo.txt", you'll lose the
original "foo.txt").

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]