renaming files with numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting renaming files with numbers
# 1  
Old 12-12-2007
renaming files with numbers

I have files of the sort

dhfmovie1.txt
dfhmovie2.txt

and want to rename them to

dhfmovie01.txt
dhfmovie02.txt

the files are in a directory with many other files, I only need to do this for files with 1-9 in the name. can anyone help me with the correct sed/mv script?

thanks
# 2  
Old 12-12-2007
Quote:
Originally Posted by pacat
I have files of the sort

dhfmovie1.txt
dfhmovie2.txt

and want to rename them to

dhfmovie01.txt
dhfmovie02.txt

the files are in a directory with many other files, I only need to do this for files with 1-9 in the name. can anyone help me with the correct sed/mv script?

thanks
Code:
ls -1 dhfmovie* | sed -n 's/\(dhfmovie\)\([0-9]\)\(.txt\)/mv & \10\2\3/p' | sh

# 3  
Old 12-12-2007
awk

Hi,

I am not very sure about your requirements, seems you did not specific it.

Suppose you have a file:
a1b3.txt
what should be the result? a01b03.txt or a1b03.txt?

Anyway, follow may help you some or so.

code:
Code:
echo 'Input directory'
read dir
for i in `ls -l $dir | nawk '$9 ~ /[0-9]/ {print $9}'`
do
new=`echo $i | sed 's/[0-9]/0&/g'`
mv $i $new
done

result:
Code:
a.txt --> a01.txt
a1bc --> a01bc

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming file that has multiple numbers as filename

Hi I have a file with filename as "partition-setup-and-ipl.vtcmd.76217657132.9721536798" Now i need to move this file as "partition-setup-and-ipl.vtcmd.76217657132.9721536798_org" i tried with # ls | grep -E "partition-setup-and-ipl.vtcmd.+"... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

4. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

5. Shell Programming and Scripting

Renaming list of files with version numbers

Hi, I have a need to rename a list of files with a suffix .v1, .v2, v3 etc. and would like to keep a maximum of 6 versions. example: I have abc.sql in my current dir. and as part of a successful completion of another script I want to move abc.sql -> abc.sql.v1. When I run the original... (3 Replies)
Discussion started by: try2shell
3 Replies

6. Shell Programming and Scripting

renaming files

Hello, I wanted to rename one file where filename contains space.. How can i rename in unix? The file name is ABC XYZ.TXT I wanted to rename this file as ABCXYZ.TXT. Any help is greatly appreciated... Regards. (4 Replies)
Discussion started by: govindts
4 Replies

7. Shell Programming and Scripting

renaming files

Hi all, using a utility image file was named starting with blank space and a blank space in between. I want to rename the files. file names are in the format " sb 12.tif"," sb 13.tif"," sb 14.tif" the files are in thousands. i want to rename as 12.tif, 13.tif, 14.tif.... thanks. (3 Replies)
Discussion started by: ahkverma
3 Replies

8. UNIX for Dummies Questions & Answers

renaming files

I have a list of files named ab_*.csv I would like to remane them all by removing the ab_ and have *.csv I did the following but I am surely missing something. /* wrong script */ for i in `ls -1 ab_*`; do mv ab_$i $i; done Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies

9. UNIX for Dummies Questions & Answers

renaming files

Hello all- I need to rename files by adding an embedded 0 e.g. aaa_bbb_1234 needs to become aaa_bbb_01234 The aaa and 1234 will change but the bbb_ can be my anchor. TIA (9 Replies)
Discussion started by: ohagar
9 Replies
Login or Register to Ask a Question