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

Re: batch renaming for filenames with space



Micha Feigin <michf@math.tau.ac.il> writes:

> I am trying to do a batch rename for a bunch of files with spaces in the
> name (in order to remove the spaces actually). I tried to use bash's for
> .. in command but it splits up the lines on spaces and not on line ends.
> Any ideas?
>
> The files are named "Copy of ..." and I want to drop the Copy of part.
> I tried to do
> for file in `ls -1`; do 
>   cp $file `echo -n "$file" | sed 's/Copy of \(.*\)/\1/'`
> done

You pretty much never want to do `ls` in a shell script; * has the
same effect, saves a process, and is somewhat more predictable in
terms of filename-to-shell-word mapping.  You also need to be careful
about quoting $file when you use it.  Thus, I'd try something like

for file in *; do
  cp "$file" `echo "$file" | sed 's/^Copy of//'`
done

I think you get no guarantees if $file is "Copy of my file.doc",
though; you might need something like

for file in *; do
  newfile=`echo "$file" | sed 's/^Copy of//'`
  cp "$file" "$newfile"
done

to make sure that you really do only have a single word.  (And I'm
still not sure I have this right.  :-)

-- 
David Maze         dmaze@debian.org      http://people.debian.org/~dmaze/
"Theoretical politics is interesting.  Politicking should be illegal."
	-- Abra Mitchell



Reply to: