script to change filename with numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to change filename with numbers
# 1  
Old 11-09-2006
script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills.
Hopefully, there is somebody who can help me with this:

I have a folder with files in it named

0.ppm
10.ppm
2.ppm
...
5.ppm
50.ppm
55.ppm
...
355.ppm
360.ppm
etc.

As you will notice, the order in which the files are processed is not
right, because it should be 5,10,15,20etc. However, because of the
shorter filenames for some of the files, they are sorted in a different
than the mathematical sense.

What I need right now is a script which runs through the data and
checks the filenames. If it is shorter than three, there have to be
zeroes added in front of the number such that I get

005.ppm
010.ppm
015.ppm
020.ppm

and thus a natural way of sorting..

It would be just perfect if anyone could point me in the right
direction.

Thanks so much!
# 2  
Old 11-09-2006
If you have Python, here is an alternative:

Code:
#!/usr/bin/python
import glob
files = glob.glob("*.ppm")
for f in sorted(files):
	print f

output:
Code:
/home> python test.py
1.ppm
10.ppm
2.ppm
5.ppm
50.ppm

# 3  
Old 11-09-2006
You could use sort -n to sort numerically, or to rename files try...
Code:
for i in *.ppm
do
  j=$(printf "%03d.%s" ${i%.*} ${i#*.})
  [[ $i != $j ]] && mv $i $j
done

# 4  
Old 11-10-2006
Thanks...

I used

for file in *.ppm
do
case $file in
??.ppm) mv "$file" "0$file,ppm" ;;
?.ppm) mv "$file" "00$file,ppm" ;;
esac
done

which works seamlessly Smilie

Thanks for the suggestions
# 5  
Old 11-10-2006
rename folders?

renaming directories works with

> for file in *
> do
> case $file in
> ??) mv "$file" "0$file" ;;
> ?) mv "$file" "00$file" ;;
> esac
> done

so please forget about this last post where I originally asked exactly this question Smilie
# 6  
Old 11-10-2006
use following

#! /usr/bin/ksh

for fname in `ls *.ppm`
do
wcount=`echo $fname | wc -c`
echo $wcount
if [[ $wcount -eq 6 ]]
then
newfile=00$fname
mv $fname $newfile
fi

if [[ $wcount -eq 7 ]]
then
newfile=0$fname
mv $fname $newfile
fi
#echo $newname
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change numbers

Hallo This is the content of the file 3 4 5 6 7 8 9 10 11 12 And I want the following output 1 2 3 4 5 6 7 (4 Replies)
Discussion started by: thailand
4 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

increase/decrease multiple numbers in a filename.

I got a game that output map tiles of the session with the 0,0 position at the place you login/spawn. That makes making a map somewhat troublesome since the 0,0 will move. So I've been looking for a way to change the numbers in the filenames of all files in a folder by a certain value. The... (5 Replies)
Discussion started by: Ravenholdt
5 Replies

4. UNIX for Dummies Questions & Answers

How to change date in a filename?

Hi i want to list files based on date and change the date alone in the files in a directory abc20120101.txt xyzxyxz20120101.txt ccc20120201.txt ddd20120301.txt In the above i want to select only files having date 20120101 and rename the date for those files like below abc20111231.txt... (3 Replies)
Discussion started by: Dewdrop
3 Replies

5. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

Change the filename

I have 100 files in a directory with a.1 a.2 a.3 a.4 How do i remove a. and i need the file names as 1 2 3 4 please help (2 Replies)
Discussion started by: srichunduru
2 Replies

7. Shell Programming and Scripting

How to remove numbers from filename

Hi all, Can I edit this script: find . -type f | while read i;do && mv "$i" "${i//abc/}" ;done so that it will not only take out abc from the filename but also take out any numbers that might be in the filename as well. An example would be, Input: filename abc 2009.mov Output:... (7 Replies)
Discussion started by: Monkey Dean
7 Replies

8. Shell Programming and Scripting

delete numbers in a filename

I have some files where numbers are part of like eg 1add1.txt 23sub41.txt etc I want to remove numbers from the filenames(whereever it may be). I used echo `ls *.txt | sed -e "s///"` But its removing first digits like 1add1.txt becomes add1.txt My intention is to make 1add1.txt... (3 Replies)
Discussion started by: villain41
3 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. Shell Programming and Scripting

Change new filename with date ??

Hi all, I am newbie and hope that you can help me to rename a file If I have a file name Perform.01222006.12345.Log now I would like to backup another file with another name like perform-20060112.dat This is a flat file, and I want to collect some field, then put it in a new file from... (9 Replies)
Discussion started by: sabercats
9 Replies
Login or Register to Ask a Question