Problem with renaming and saving


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with renaming and saving
# 1  
Old 02-22-2011
Problem with renaming and saving

Hello mates,

I am quite new to script programming and I am facing an uphill task to rename files in one folder. I have gone through similar posts but most of them deal with renaming files by changing the file extensions.
Problem : I have a folder which contains files like bild01.jpg,bild02.jpg....and so on..
There are more files in the folders which should remain untouched.
I want to rename these 'bild' files as follows:
Code:
bild01.jpg -----> 1c.jpg
bild02.jpg -----> 2c.jpg
.
.
bild30.jpg------>30c.jpg

I have reached till :
Code:
#!/bin/bash

for i in `ls -1 bild*`
do 
   filename=$i
   j=`echo $filename | sed 's/.*bild\(.\).*/\1/'`
   if [ $j -eq 0 ]
   then
      echo "Good Morning"
   elif [ $j -gt 0 ]
   then
      echo "Good Night"
   fi
done

The if statements here compare the 5th character in each file name.
Instead of 'Good Morning' and 'Good Night', I would like to have a working code which replaces characters 'bild0' or 'bild' with letter 'c'

Any help is appreciated...
Thanks in advance

CJ

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-22-2011 at 07:30 AM.. Reason: code tags
# 2  
Old 02-22-2011
Code:
#!/bin/bash

for i in bild*; do
     mv $i `echo $i| sed 's/^[a-zA-Z]\+0*/c/g'`
done

exit 0

# 3  
Old 02-22-2011
or to avoid spawning an extra sed process

Code:
#!/bin/bash
for f in bild*jpg
do
     mv $f c${f#bild}
done

# 4  
Old 02-22-2011
Thanks Zxxon !..
It worked like I wanted ...how the command would look like if I wanted
1c.JPG ,2c.JPG instead ?
Thanks again..

CJ
# 5  
Old 02-22-2011
See if this works for you:
Code:
#!/bin/ksh
typeset -i mSeq
for mFName in bild*jpg
do
  mSeq=`echo $mFName | sed 's/.*\([0-9]\{2\}\).*/\1/'`
  mNewFName="${mSeq}c.jpg"
  mv ${mFName} ${mNewFName}
done

# 6  
Old 02-22-2011
Guys..thanks for your replies...I got my code working after all your valuable inputs.

CJ
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] File Splitting And Renaming Problem

OK So I Recently Bought A whatbox Seed-box Act!!:cool: I am connected to whatbox via SSH!!! Now i have downloaded a movie and renamed it to 2yify.mp4 (800MB):o When I TYPE the command to split it which is:) split -b 400m 2yify.mp4 It gets renamed into two parts with different names... (4 Replies)
Discussion started by: anime12345
4 Replies

2. UNIX for Advanced & Expert Users

Problem with renaming files

I have about 1000 files containing the character * in the name. I need to find these files and replace the * with a -. I am working with HP UX v11. I am using the following command find . -type f -name '*\**' -exec bash -c 'f="$1"; mv "$f" "${f//\*/-}"' - '{}' \ People tell me it works for... (4 Replies)
Discussion started by: MikeDavid
4 Replies

3. Shell Programming and Scripting

Problem renaming files using variables

Hi, I have the following problem: I have a list of files: 1.txt 2.txt 3.txt 4.txt Then I have a list of variable names inside variable.txt: A B C D I'd like to rename 1.txt, 2.txt etc using the variables from variable.txt (2 Replies)
Discussion started by: hubleo
2 Replies

4. Shell Programming and Scripting

Problem saving return value of subroutine in perl

Hi all, I have this code #This program read the triplets from file named "data" into #an array of array. use strict; use warnings; use Data::Dumper; use Graph; use Graph::Subgraph; my @S; while (<>) { push @S, ; } print "-----TRIPLETS-------\n"; print Dumper \@S; #Make... (6 Replies)
Discussion started by: rushadrena
6 Replies

5. Shell Programming and Scripting

Loop renaming files w/ a count problem

:wall: Hello there, basically in my program where im stuck at is when it comes to rename the files in a loop. - the program counts the number of files w a given name (works!) - and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the... (1 Reply)
Discussion started by: thurft
1 Replies

6. Shell Programming and Scripting

File renaming problem

Can someone tell me how can i remove the RPCFTP word from RPCFTPfilelist.csv file ? (4 Replies)
Discussion started by: JSKOBS
4 Replies

7. Shell Programming and Scripting

Problem with saving mails (fetchmail + procmail + uudeview)

Hello! I try to save attachments from mails (pop3/imap) to folders. I have 2 emails, which i want to scan: user1@host_address user2@host_address I use next .fetchmailrc configuration #defaults protocol pop3 poll "user2@host_address", protocol imap, user "user1", password "1"... (3 Replies)
Discussion started by: optik77
3 Replies

8. UNIX for Advanced & Expert Users

problem with renaming files

Hi, I need to rename all the .txt files present in current directory to .dat files respectively in UNIX. for example: $ ls aaa.txt bbb.txt ccc.txt I need to change them to $ ls aaa.dat bbb.dat ccc.dat Is there any UNIX command to do this in one go? ... (3 Replies)
Discussion started by: Johny001
3 Replies

9. Shell Programming and Scripting

Problem renaming a file with single quotes

Hi, I am trying to create a script which validates the incoming source files. The script has File name Pattern as Argument. The First part of the script validates if there are any files available if then echo "\n Files are available to process \n" else echo "\n File does not... (9 Replies)
Discussion started by: dsshishya
9 Replies

10. UNIX for Advanced & Expert Users

advanced file renaming problem

I know this is probably a question for the newbie forum, where it is also posted, but I thought maybe some of you pros might like to help me out anyway. Here is my problem: I have to rename a batch of files that look like: 2001_0001.asc 2001_0002.asc . 2001_0548.asc 2002_0184.asc . .... (0 Replies)
Discussion started by: sea krait
0 Replies
Login or Register to Ask a Question