Renaming file and check for the renamed file existence | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Renaming file and check for the renamed file existence

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-28-2013
Priya Amaresh Priya Amaresh is offline
Registered User
 
Join Date: May 2012
Last Activity: 28 February 2014, 4:27 AM EST
Posts: 98
Thanks: 24
Thanked 1 Time in 1 Post
Renaming file and check for the renamed file existence

Hi
Am trying to move a file from one name to another
When I do "ls" to check for the moved filename
I can see the file but when I try the same with a script am unable..
I think am doing some pretty silly error.. please help..


Code:
toMove=`ls | grep -E "partition.[0-9]+"`
mv $toMove partition.[0-9]_org
if [ $? -eq 0 ]; then
        echo "Success.. The value of $toMove"
else
        echo "FAIL"
fi

#Checking for file has transferred..
toFind=`ls | grep -E "partition.[0-9]_org"`
if [ $? -eq 0 ]; then
        echo "Success.. The value of $toFind "
else
        echo "FAIL"
fi

Here the value toFind returns a FAIL state..

Last edited by Priya Amaresh; 02-28-2013 at 07:42 AM..
Sponsored Links
    #2  
Old 02-28-2013
Vikram_Tanwar12 Vikram_Tanwar12 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 21 November 2013, 3:32 AM EST
Posts: 168
Thanks: 13
Thanked 10 Times in 10 Posts
Use it without Grep


Code:
toFind=`ls "partition.[0-9]_org"`


Last edited by Vikram_Tanwar12; 02-28-2013 at 07:53 AM..
Sponsored Links
    #3  
Old 02-28-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 3 April 2014, 10:13 AM EDT
Posts: 1,148
Thanks: 20
Thanked 104 Times in 99 Posts
I'm sorry but did not understand what you are trying to achieve here.

Why are you creating a file with a odd name ???


Code:
mv $toMove partition.[0-9]_org

    #4  
Old 02-28-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 Posts
You should specify path for the ls command
toMove=`ls /etc | grep -E "partition.[0-9]+"`

Do not use back ticks `` if its possible
toMove=$(ls /etc | grep -E "partition.[0-9]+")

and as paynam write: you try to move all file matching pattern partition.[0-9]+
to one singe file with this name partition.[0-9]_org

I think you try to move files like this partition.12 to partition.12_org or this partition.45465 to partition.45465_org

Last edited by Jotne; 02-28-2013 at 07:57 AM..
Sponsored Links
    #5  
Old 02-28-2013
Priya Amaresh Priya Amaresh is offline
Registered User
 
Join Date: May 2012
Last Activity: 28 February 2014, 4:27 AM EST
Posts: 98
Thanks: 24
Thanked 1 Time in 1 Post
Hi

Its a file generated during installation thats why the odd filename exists
Sponsored Links
    #6  
Old 02-28-2013
chacko193 chacko193 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 20 April 2014, 11:41 PM EDT
Posts: 102
Thanks: 21
Thanked 14 Times in 14 Posts
I think here you are renaming the file to partition.[0-9]_org .So when you try using the RE,it won't find any file.

Better use

Code:
mv $toMove "$toMove"_org

instead of

Code:
mv $toMove partition.[0-9]_org

And in the part were you check if the operation was successfull, I think you missed out the "+".It should have been

Code:
toFind=`ls | grep -E "partition.[0-9]+_org"`

And better escape the "."

Last edited by Franklin52; 03-01-2013 at 02:11 AM.. Reason: fixed code tags
The Following User Says Thank You to chacko193 For This Useful Post:
Priya Amaresh (03-01-2013)
Sponsored Links
    #7  
Old 02-28-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 Posts
So you search one singe file with this exact name partition.[0-9]+ to name partition.[0-9]_org
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to check for the file existence, if file exists it should echo the no of modified days karthikeyan_mac Shell Programming and Scripting 2 08-03-2011 07:08 AM
To check for existence of a file manutd UNIX for Dummies Questions & Answers 1 07-20-2011 02:04 AM
How to check for file existence? mingming88 Shell Programming and Scripting 1 04-28-2009 10:20 AM
Check for File Existence dsdev_123 AIX 4 08-08-2008 07:30 PM
check for file existence dsdev_123 AIX 2 06-26-2008 01:08 PM



All times are GMT -4. The time now is 03:05 AM.