Unix Shell - another file re-naming question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > another file re-naming question





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 another file re-naming question
drako

2006-12-16, 1:19 pm

Hi,

For some reason, I'm having a lot of trouble trying to rename all files
within a directory hierarchy that begin with a decimal number.

What I want to do is strip out everything up to the first alphabetical
character. i.e:

change

1file.txt -> file.txt
12 file.txt -> file.txt
12 - file.txt -> file.txt

I've tried many variations on the script below, but can't see, to
figure this one out:

#!/bin/sh
for f in `find . -type f -exec grep '^[0-9]*$' {} \;`
do
n=`echo $f | sed -e 's/\(^[0-9]+\W?\)\(.*$\)/\2/'`
mv $f $n
done


Can't quite pinpoint where this is going wrong.

Any suggestions appreciated.

Rgds
Neil.

Bill Marcum

2006-12-16, 1:19 pm

On 16 Dec 2006 06:14:23 -0800, drako
<neil@invidion.co.uk> wrote:
>
>
> Hi,
>
> For some reason, I'm having a lot of trouble trying to rename all files
> within a directory hierarchy that begin with a decimal number.
>
> What I want to do is strip out everything up to the first alphabetical
> character. i.e:
>
> change
>
> 1file.txt -> file.txt
> 12 file.txt -> file.txt
> 12 - file.txt -> file.txt
>
> I've tried many variations on the script below, but can't see, to
> figure this one out:
>
> #!/bin/sh
> for f in `find . -type f -exec grep '^[0-9]*$' {} \;`
> do
> n=`echo $f | sed -e 's/\(^[0-9]+\W?\)\(.*$\)/\2/'`
> mv $f $n
> done
>
>
> Can't quite pinpoint where this is going wrong.
>

'-exec grep' works on the file contents, not the name.
for f in `find . -type f -name '[0-9]*'
but that won't work with file names that contain spaces.
find . -name '[0-9]*' | while read f


--
Man and wife make one fool.
Stephane CHAZELAS

2006-12-16, 1:19 pm

2006-12-16, 06:14(-08), drako:
> Hi,
>
> For some reason, I'm having a lot of trouble trying to rename all files
> within a directory hierarchy that begin with a decimal number.
>
> What I want to do is strip out everything up to the first alphabetical
> character. i.e:
>
> change
>
> 1file.txt -> file.txt
> 12 file.txt -> file.txt
> 12 - file.txt -> file.txt

[...]

zmv '(**/)[^[:alpha:]]##(*)' '$1$2'


--
Stéphane
Chris F.A. Johnson

2006-12-16, 1:19 pm

On 2006-12-16, drako wrote:
> Hi,
>
> For some reason, I'm having a lot of trouble trying to rename all files
> within a directory hierarchy that begin with a decimal number.
>
> What I want to do is strip out everything up to the first alphabetical
> character. i.e:
>
> change
>
> 1file.txt -> file.txt
> 12 file.txt -> file.txt
> 12 - file.txt -> file.txt
>
> I've tried many variations on the script below, but can't see, to
> figure this one out:
>
> #!/bin/sh
> for f in `find . -type f -exec grep '^[0-9]*$' {} \;`
> do
> n=`echo $f | sed -e 's/\(^[0-9]+\W?\)\(.*$\)/\2/'`
> mv $f $n
> done


find . -type f -name '[0-9]*' |
while IFS= read file
do
name=${file##*/}
dir=${file%"$name"}
number=${name%%[!0-9]*}
mv -i "$file" "${dir:+"${dir%/}"/}${name#"$number"}"
done

There are some potential problems: e.g., files whose names only
contain digits, filenames containing a newlines, etc..

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com