Unix questions - Renaming groups of files modifying only a character

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > July 2006 > Renaming groups of files modifying only a character





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Renaming groups of files modifying only a character
Shaman

2006-07-11, 8:10 am

How could I modify groups of files which are defined by a progressive
number, like:

100**.example

varying just a char:

101**.example

Thank you!

Stephane CHAZELAS

2006-07-11, 1:32 pm

2006-07-11, 04:51(-07), Shaman:
> How could I modify groups of files which are defined by a progressive
> number, like:
>
> 100**.example
>
> varying just a char:
>
> 101**.example

[...]

Using zmv (from zsh):

zmv '100(??.example)' '101$1'

--
Stéphane
Chris F.A. Johnson

2006-07-11, 1:32 pm

On 2006-07-11, Shaman wrote:
> How could I modify groups of files which are defined by a progressive
> number, like:
>
> 100**.example
>
> varying just a char:
>
> 101**.example


Some systems have a rename command; for those that don't:

n=100
for file in 100*.example
do
mv -i -- "$file" "$n${file#100}"
n=$(( $n + 1 ))
done

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Chuck Dillon

2006-07-12, 1:21 pm

Shaman wrote:
> How could I modify groups of files which are defined by a progressive
> number, like:
>
> 100**.example
>
> varying just a char:
>
> 101**.example
>
> Thank you!
>


One of many ways might be:

ls 100??.example | cut -c4- | xargs -i: mv 100: 101:

-- ced

--
Chuck Dillon
Manager of Software Development, Bioinformatics
NimbleGen Systems Inc.
Keith Thompson

2006-07-13, 1:24 am

"Shaman" <quercianellense@gmail.com> writes:
> How could I modify groups of files which are defined by a progressive
> number, like:
>
> 100**.example
>
> varying just a char:
>
> 101**.example


If I were going to be doing this repeatedly, I'd write a script of
some sort that loops over the file names and does the right thing for
each one. Others have shown examples.

If it were a one-time thing, I'd probably just do an "ls" of the
appropriate files, redirecting the output to a file, then edit the
file and execute or source it.

It's not entirely clear from your example what you're trying to do (at
least not to me), but suppose I wanted to rename the files
foo1.txt bar1.txt baz1.txt
to
foo2.txt bar2.txt baz2.txt

% ls *1.txt > tmp
% vi tmp # or use your favorite editor
:%s/.*/mv & &
:%s/1.txt$/2.txt
:wq
% cat tmp
mv bar1.txt bar2.txt
mv baz1.txt baz2.txt
mv foo1.txt foo2.txt
% . ./tmp # or "source tmp" if you use csh or tcsh

(I haven't tested this particular sequence, so there are no guarantees
-- which is not to imply that where would be any guarantees if I *had*
tested it.)

This isn't always going to be the best approach, of course. One
advantage is that you can see the individual commands before you
execute them; another is that you can manually handle exceptions if
necessary (for example, if you have a file "1.txt" and you don't want
to rename it, just delete that line before running the script).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Shaman

2006-07-14, 7:25 am

Thank you so much!!!!!!

Keith Thompson ha scritto:

> "Shaman" <quercianellense@gmail.com> writes:
>
> If I were going to be doing this repeatedly, I'd write a script of
> some sort that loops over the file names and does the right thing for
> each one. Others have shown examples.
>
> If it were a one-time thing, I'd probably just do an "ls" of the
> appropriate files, redirecting the output to a file, then edit the
> file and execute or source it.
>
> It's not entirely clear from your example what you're trying to do (at
> least not to me), but suppose I wanted to rename the files
> foo1.txt bar1.txt baz1.txt
> to
> foo2.txt bar2.txt baz2.txt
>
> % ls *1.txt > tmp
> % vi tmp # or use your favorite editor
> :%s/.*/mv & &
> :%s/1.txt$/2.txt
> :wq
> % cat tmp
> mv bar1.txt bar2.txt
> mv baz1.txt baz2.txt
> mv foo1.txt foo2.txt
> % . ./tmp # or "source tmp" if you use csh or tcsh
>
> (I haven't tested this particular sequence, so there are no guarantees
> -- which is not to imply that where would be any guarantees if I *had*
> tested it.)
>
> This isn't always going to be the best approach, of course. One
> advantage is that you can see the individual commands before you
> execute them; another is that you can manually handle exceptions if
> necessary (for example, if you have a file "1.txt" and you don't want
> to rename it, just delete that line before running the script).
>
> --
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.


Shaman

2006-07-19, 8:04 am

A suitable solution could be:

n=101

for file in 100??.example

do

mv -i "$file" "$n${file#100}"

done

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com