batch formating filenames with zero padding


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers batch formating filenames with zero padding
# 1  
Old 05-14-2009
batch formating filenames with zero padding

My little script for producing pseudo random file names works OK; but I need the file names to be formatted with zero padding, probably %05d

here's the script so far
Code:
#!/bin/bash
ls *.jpg > list
for i in `cat list`
do
rnd=$RANDOM
mv "$i" "$rnd"
done

I've been trying printf but can't get the output to rename the files, probably just a beginner's lack of understanding. Thanks for any suggestions
# 2  
Old 05-14-2009
Can do this without a temporary file:

Code:
#!/bin/bash

for in *.jpg
do
     mv "$i" `printf "%05d\n" $RANDOM`
done

exit 0

If you encounter filenames with blanks you might want to use a while/read construct instead.
# 3  
Old 05-14-2009
That is *way* better - thanks a million!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Padding a csv value with 0's

I have this csv file that I would like to sort on the 20th and 21st field. They are high lighted below. My challenge is that when I sort on those fields they are not in order as I would have liked. It seems like I have to pad those fields to the longest value in that fields data. ... (6 Replies)
Discussion started by: GroveTuckey
6 Replies

2. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

3. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

4. Shell Programming and Scripting

Padding in Unix

I have a file with different character counts on each line how do i make it with unique character counts. example: 1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA 2nd line: ABCD 011 xys 0010 YYYY BBB TEAD 3rd line : ABCD 022 YXU 000 UUU BBB TE 1st line is 43... (3 Replies)
Discussion started by: rudoraj
3 Replies

5. Programming

Byte Padding

Hi, Can someone explain what is byte padding? For ex: struct emp{ char s; int b; char s1; int b1; long b3; char s3; } What will be the size of this structure? Thanks (6 Replies)
Discussion started by: naan
6 Replies

6. Programming

Padding variables

Is there a function in c that will allow me to pad variables? I have an int that can't be longer than 10. I need to pad a numeric value with leading zeros 314 0000000314 (1 Reply)
Discussion started by: flounder
1 Replies

7. UNIX for Dummies Questions & Answers

text formating/Text space padding

Hi, How do I format the following? I have a for loop that retrieves me the path and file name of a file. In column fashion I would like to add the time and another variable containing the file's size. This will result in all the time being under each other and all the file size's being under... (6 Replies)
Discussion started by: hugow
6 Replies

8. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies

9. Programming

Zero Padding to a string

I am writing a C program which a part of it needs to padding zero in front of a string. The program will get a sting from an ASCII file which the maxium length of this string is 5 char long. The string can sometimes less the 5 char long. In order to make it with the same length '0's are being... (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies

10. UNIX for Advanced & Expert Users

Padding issues

Hello, Structure padding & structure size are different on Compaq & HP UNIX. When structures are transfered via netfork from Compaq to HP will this be a problem? If yes, what can be the solution? Thanks, shilpa (2 Replies)
Discussion started by: informshilpa
2 Replies
Login or Register to Ask a Question