renaming jpeg files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting renaming jpeg files
# 1  
Old 07-10-2010
renaming jpeg files

Hi guys im currently trying to write a script which includes rename files and this is the part where i fail

i have a list of files eg

Quote:
1457847135_l.jpg 2nspfk4.jpg DSC0102.jpg m.JPG
2mmcv4n.jpg 9b.JPG DSC0198.jpg
and i want to rename them so

Quote:
myfile1.jpg
myfile2.jpg
myfile3.jpg
myfile4.jpg
myfile5.jpg
myfile6.jpg
myfile7.jpg
any help really would be appreciated
Cheers dunryc
# 2  
Old 07-10-2010
Hi

Try:

Code:
#!/bin/ksh

i=1
for file in `ls *jpg`
do
 mv $file myfile${i}.jpg
 let i=i+1
done

Guru.
# 3  
Old 07-10-2010
For mixed case extensions and file names that may contain spaces:
Code:
n=1
for i in *.jpg *.JPG
do 
  mv "$i" myfile$n.jpg
  n=$((n+1))
done

# 4  
Old 07-10-2010
Code:
#!/bin/bash

I=1
for FILE in $( ls *.jpg *.JPG )
do
  mv -v $FILE myfile$( printf '%03d' "$I" ).jpg
  I=$(($I+1))
done

exit 0
#finis

# 5  
Old 07-10-2010
Quote:
Originally Posted by dr.house
Code:
for FILE in $( ls *.jpg *.JPG )

In the past, people advocated against using ls that way, which I found curious. The difference in processing power is completely insignificant.

Quote:
Originally Posted by Scrutinizer
For mixed case extensions and file names that may contain spaces:
Code:
for i in *.jpg *.JPG


But I just now tried that way and Scrutinizer's way and was surprised to find that simple wildcard expansion does work with filenames that have spaces in them, whereas using ls does not.

That's excellent to know, because I sometimes encounter filenames with spaces. So I always try to make all my scripts work seamlessly with them.
# 6  
Old 07-10-2010
Quote:
Originally Posted by KenJackson
In the past, people advocated against using ls that way
I simply use what I consider most transparent in terms of readability / maintainability (- if it were about efficiency, I'd code in C ... Smilie).
# 7  
Old 07-10-2010
Using ls in a command substitution to generate a list of filenames for a list is almost always a poor approach, no matter how you look at it (efficiency, readability, maintainability, robustness (with regard to IFS characters in filenames).

In my opinion (and that's all it is), it's one of the worst yet common scripting practices. I think I see it used just about every day. At least a UUoC is generally harmless. "$(ls *.jpg)" versus a simple "*.jpg" is often buggy.

Regards,
Alister

---------- Post updated at 06:01 PM ---------- Previous update was at 05:49 PM ----------

Quote:
Originally Posted by KenJackson
But I just now tried that way and Scrutinizer's way and was surprised to find that simple wildcard expansion does work with filenames that have spaces in them, whereas using ls does not.

That's excellent to know, because I sometimes encounter filenames with spaces. So I always try to make all my scripts work seamlessly with them.
The only shell command line parsing step after pattern expansion is quote removal, so it's safe from field splitting and other substitutions/expansions.

Command substitution, however, occurs before field splitting, and is unsafe if the filename contains a character which is used to delimit fields (these characters are denoted by the contents of the IFS variable, whose default value is space, tab, newline). You can quote the command substitution, to prevent field splitting of the command's results, but that would always yield a single word (useless in a loop list context since it would always lead to a single iteration).

Regards,
Alister
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 files

Hi i have to achieve the following i have files as xyz001.csv, xyz002.csv.......xyz0025.csv in a folder, i need to keep xyz001.csv as it is but want to remove the extra zero on filename from 10 say xyz0010 should be renamed to xyz010 xyz0025 should be renamed as xyz025 Note xyz... (8 Replies)
Discussion started by: mad_man12
8 Replies

3. Shell Programming and Scripting

cp RAW files if JPEG file present

hi guys and girls, i have a folder containing RAW and JPG images. eg... 001.jpg 003.jpg 005.jpg 001.raw 002.raw 003.raw 004.raw 005.raw I want to copy only RAW files that have a corresponding JPG file in to a new folder. the jpg files do not need to be copied. in this example i... (6 Replies)
Discussion started by: fxylxy
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 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

6. UNIX for Dummies Questions & Answers

Renaming Files

Hi Alll, I have a script that we use on the servers to change the name of files that have spaces in the name: #!/bin/tcsh set n = 0 foreach f ( * ) echo $f | grep " " if ( $? == 0 ) then mv "$f" `echo $f | sed -e "s/ /_/g"` @ n += 1 endif end echo $n changed I need to write a... (2 Replies)
Discussion started by: abch624
2 Replies

7. UNIX for Dummies Questions & Answers

ftp selected jpeg files from unix filesystem

Hi All We have hp-ux 11iv1 system running with oracle8i database. We have around 350,000 users, each user uploaded their own signatures and are stored in unix filesystems department wise. A database is maintained to keep their particulars with a path to link their signature files. Now... (3 Replies)
Discussion started by: mhbd
3 Replies

8. UNIX for Dummies Questions & Answers

renaming the files

Hi All, Today I got a small problem while handling zipped files in PROD support. There are files in this format and I had to grep them reading some contents A.B.gz.C.D where A,B,C and D stand for variables (like FIRST.NAME.gz.MIDDLE.LAST). I know that these files are zipped files and If I... (1 Reply)
Discussion started by: adurga
1 Replies

9. UNIX for Dummies Questions & Answers

renaming files

i have a set of *.lst files. now i want to change the names from "lst" to "dat". how to do it? ex.: -rw-r--r-- 1 rram group 22 Sep 21 13:10 a.lst -rw-r--r-- 1 rram group 22 Sep 21 13:09 b.lst -rw-r--r-- 1 rram group 22 Sep 21 13:10 c.lst... (4 Replies)
Discussion started by: raguramtgr
4 Replies
Login or Register to Ask a Question