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 14:22:37 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2012-04-12 08:19:41 -0400, Greg Wooledge:
> On Thu, Apr 12, 2012 at 11:05:41AM +0800, Clark Wang wrote:
> > On Thu, Apr 12, 2012 at 04:54, DJ Mills <address@hidden> wrote:
> > > This is a TERRIBLE idea, as attempting to word-split a command
> > > substitution will fail horribly on any filenames with spaces (which is
> > > very common in Windows), and will attempt pathname expansion on the
> > > output. A simple glob should be used, or -exec if find is used.
> > 
> > A glob expansion may cause the command line to be too long since the OP has
> > 2.5 million files?
> 
> A glob expansion being passed as an argument list to a command will
> fail, yes.  But a glob expansion being used internally as the list
> in a bash "for" command will be OK.
> 
> for f in *; do
>   mv -- "$f" "$f.txt"
> done
[...]

Note that * will first sort the list of files which will be less
efficient than using find. Bash is also known not to handle
allocation of big things very efficiently.

I'll bring up zsh again sorry, which has globbing qualifiers,
one of which disables the sorting:

zmodload zsh/files # for the builtin mv 
setopt extendedglob # for the negation operator
for f (^*.*(oN)) mv -- $f $f.txt # short for-loop form and no
                                 # implied word splitting or
                                 # globbing upon variable expansion
                                 # in zsh (so quotes not
                                 #  necessary)

Interestingly, there's also a globbing qualifier that can
execute shell code, so you can do the mv without even building
the whole file list in memory:

zmodload zsh/files # for the builtin mv 
setopt extendedglob # for the negation operator
rename() mv -- $REPLY $REPLY.txt
files_that_failed=(^*.*(N^+rename))

(N to not complain if it expands to no file at all, ^ to negate
the following qualifier(s), +rename to execute the rename
command on all the files being considered.

Here the glob only expands to the list of files that failed to
be renamed, not the millions of files in the current directory.

-- 
Stephane




reply via email to

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