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

Re: scripting question



On 2009-07-01 07:22 (-0700), Marc Shapiro wrote:

> How can I rename all of the files ina directory with the new name
> being the old name stripped of its leftmost three characters. If all
> of the files are off the format:
>
> 	xxxyyyyyyyyyyyyyyyyyyyyy.zzz
>
> I want the new names to be of the format:
>
> 	yyyyyyyyyyyyyyyyyyyyy.zzz
>
> I have hundreds of files like this in each of several directories and
> I really don't want to do it all by hand. I did some of that, already,
> and I know there must be a better way.

Single command which affect the current directory and all
subdirectories:

    find -type f -print0 | xargs -0 sh -c 'for file in "$@"; 
      do dir=$(dirname -- "$file") && base=$(basename -- "$file") &&
      (cd "$dir" && echo mv -- "$base" "${base#???}"); done' ignore

It uses "echo mv ..." command so it does nothing but echoes the command
line that would be run inside each directory. Remove the "echo" part to
actually execute the command. Of course you can add your own
file-selection options for "find" command. This works even if you have
spaces in filenames or directory names.

The command launches several processes per file so it's not terribly
elegant but should be OK for one-time rename.


Reply to: