How to rename file-using shellscript?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to rename file-using shellscript?
# 1  
Old 12-07-2011
How to rename file-using shellscript?

Hi All,

need your help in renaming the file ? Please read the below data carefully. Its may b simple for you :-) just information is too long

Code:
Eg-home/user01/exercise   contains 
 
abc.log, ppp.log ,shd.log
 
want to rename this file with timestamp using shellscript


for this i have created shellscript

Code:
 
#! /bin/bash
cd /home/user01/exercise
 
exercise=/home/user01/exercise
 
mDate=$(date +%Y%m%d%H:%M:%S)
for mFName in $exercise/*.log; do
  mPref=${mFName%.log}
 mv ${mFName} ${mPref}.${mDate}.log
done


using above code my file is going to rename as


Code:
 
abc.2011120706:54:59.log
ppp.2011120706:54:59.log
shd.2011120706:54:59.log

up to this everythig is ok. now my problm is that if i have created again new abc.log in this folder again it will accept .

now my exercise folder contains

Code:
 
abc.log
abc.2011120706:54:59.log
ppp.2011120706:54:59.log
shd.2011120706:54:59.log


now if i run the above script for renaming the log files then it will give me the below result (which i dont want )

Code:
 
abc.2011120710:59:40.log
abc.2011120706:54:59.2011120710:59:40.log
ppp.2011120706:54:59.2011120710:59:40.log
shd.2011120706:54:59.2011120710:59:40.log

my aim is to rename any new files ones .it should not increase the timestamp details at files as it is incresing the file name size .and also it useless for me.


MY AIM IS , SECOND TIME IT SHOULD RENAME ONLY abc.log

OUTSHOULD LOOK LIKE

Code:
 
abc.2011120710:59:40.log
abc.2011120706:54:59.log
ppp.2011120706:54:59.log
shd.2011120706:54:59.log


pLEASE HELP ME IN MODIFYING THE ABOVE SCRIPT.


Thanks

Last edited by aish11; 12-07-2011 at 07:12 AM..
# 2  
Old 12-07-2011
Code:
#! /bin/bash
cd /home/user01/exercise
 
exercise=/home/user01/exercise
 
mDate=$(date +%Y%m%d%H:%M:%S)
for mFName in $exercise/*.log
do
    mPref=${mFName%.log}
    echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"
    [ $? -eq 0 ] && continue
    mv ${mFName} ${mPref}.${mDate}.log
done

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-08-2011
Hi balajesuri,

Thanks for the help ...


but getting below error

Code:
 
mv: cannot stat `/*.log': No such file or directory

---------- Post updated at 11:09 PM ---------- Previous update was at 10:53 PM ----------

hey thanks a lot , its working properly Smilie


can u plz explain me the below code what it exactly does


Code:
 
echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"

# 4  
Old 12-08-2011
--> Value in $mPref is echo'd to egrep.
--> egrep stands for extended grep (look for manpages of grep option -E)
--> -q option is to suppress the output.. used to silence grep even when a match is found.
--> "\.[0-9]{10}:[0-9]{2}:[0-9]{2}" is a pattern which will look for a dot followed by a 10-digit number, a colon, a 2-digit number, a colon, a 2-digit number.
# 5  
Old 12-08-2011
MySQL

Code:
echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"

the Variable "$mPref" will contain the file name.
the filename is echoed and the file name is checked whether it has been already formatted or not. if it is already formatted it will continue to the next filename else it will move that file.

That egrep uses a regular expression,

egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"
.[0-9]{10}: - This looks for a ten digit number followed by a colon.
[0-9]{2}: - This looks for a two digit number followed by a colon.
[0-9]{2} - This looks for a two digit number.

which in turn matches the numbers given below format , which is in your formatted file names,

abc.2011120710:59:40.log
--------------------------------
Late reply

Last edited by Kesavan; 12-08-2011 at 01:14 AM.. Reason: Post is answered already
# 6  
Old 12-08-2011
Thank U.


Need one more help plz,

Do u have any studymaterial for learning the shellscript containing simple examples. and also i want to learn awk , sed command. Need some simple example for learning this .

---------- Post updated at 12:40 AM ---------- Previous update was at 12:31 AM ----------

Quote:
Originally Posted by Kesavan
Code:
echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"

the Variable "$mPref" will contain the file name.
the filename is echoed and the file name is checked whether it has been already formatted or not. if it is already formatted it will continue to the next filename else it will move that file.

That egrep uses a regular expression,

egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"
.[0-9]{10}: - This looks for a ten digit number followed by a colon.
[0-9]{2}: - This looks for a two digit number followed by a colon.
[0-9]{2} - This looks for a two digit number.

which in turn matches the numbers given below format , which is in your formatted file names,

abc.2011120710:59:40.log
--------------------------------
Late reply



i need to clarify the below things -just giving example from single file name. if i am wrong plz let me know

1)mFName contains abc.log?

2) mPref contains abc ?

3)meaning of "echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}" "

means if any log file having parameter like \.[0-9]{10}:[0-9]{2}:[0-9]{2}" then it should negelct that file and goes to next line ??
# 7  
Old 12-08-2011
1) Yes
2) Yes
3) `echo $mPref | egrep -q "\.[0-9]{10}:[0-9]{2}:[0-9]{2}"` will not neglect current file. The skipping part is done by next line "[ $? -eq 0 ] && continue" which means, if the exit status of last run command is 0, then skip to next iteration (in this case next file)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

Pivot file using shellscript

Hi, I am new at using shell scripting. So I have a question for the more experienced Linux users. I would like to perform some kind of pivot on a file. Example Input file COL1,"2001-01-01","2001-03-01","2001-03-24" A1,22,,44 B1,56,78,12 C2,5,, I would like to have to following output... (2 Replies)
Discussion started by: shanlinux
2 Replies

3. Shell Programming and Scripting

How to pass a shellscript variable to a sql file?

Hi, i wan't to pass a shellscript variable to a sql file. a.sql select $field from dual; the way i am calling this is through sqlplus field_name="sysdate" sqlplus -s username/password@hostname:port/servicename <<EOF @a.sql $field_name EOF (4 Replies)
Discussion started by: reignangel2003
4 Replies

4. Shell Programming and Scripting

Extract TAG name and XPATH from XML file via shellscript

Hi, Here is a sample xml file and expected output. I need to extract the element/tag name (not value) and xpath (sample output.txt). But the main problem is I put here one simple xml file where I can clearly see the number of elements, but in real time I have a xml file which have over 500... (18 Replies)
Discussion started by: BithunC
18 Replies

5. Shell Programming and Scripting

Please help me to print particular line in the file using shellscript

Plese help me on this below I have to do below 2 things in a single shell script 1) I have to search one word(eg : red) from the below sample file. 2) Then if I am getting word in the 3rd line (red) then I need to print the above 2 lines and below 2 lines from thekeyword For Example:... (5 Replies)
Discussion started by: Maanjesh
5 Replies

6. Shell Programming and Scripting

Create an .csv/excel file using shellscript

In my file, i have 4 Product names(For ex:Microsoft excel, Oracle,.Net,IBM websphere,..etc.,) I want this 4 Products in 4 individual .csv/excel file after running the script with those products related information. (12 Replies)
Discussion started by: Navya
12 Replies

7. Shell Programming and Scripting

Shellscript to sort duplicate files listed in a text file

I have many pdf's scattered across 4 machines. There is 1 location where I have other Pdf's maintained. But the issues it the 4 machines may have duplicate pdf's among themselves, but I want just 1 copy of each so that they can be transfered to that 1 location. What I have thought is: 1) I have... (11 Replies)
Discussion started by: deaddevil
11 Replies

8. Shell Programming and Scripting

Check file exists from a shellscript

Hi, I have a list of files that I want to check to see if they exist and then count how many of these files exist, I also want to do the same for the files that arent found. I have done this by creating temp files see below but want ot do this using variables instead: for FILE in... (7 Replies)
Discussion started by: tonydsam
7 Replies

9. Shell Programming and Scripting

shellscript to find a line in between a particular set of lines of a text file

i have a file a.txt and following is only one portion. I want to search <branch value="/dev36/AREA/" include="yes"></branch> present in between <template_file name="Approve External" path="core/approve/bin" and </template_file> where the no of lines containing "<branch value= " is increasing ... (2 Replies)
Discussion started by: millan
2 Replies

10. Shell Programming and Scripting

shellscript.query Oracle table..populate in a text file

Hi Guys, I'm new to this forum as well as to UNIX shell scripting. I'm looking for a shellscript to query an Oracle database table and populate the result set of the query in a text file. Could you someone help me out with a sample code? Thanks, Bhagat (7 Replies)
Discussion started by: bhagat.singh-j
7 Replies
Login or Register to Ask a Question