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..

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..:mad:

Use it without Grep

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

I'm sorry but did not understand what you are trying to achieve here.

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

mv $toMove partition.[0-9]_org

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

Hi

Its a file generated during installation thats why the odd filename exists

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

mv $toMove "$toMove"_org

instead of

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

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

And better escape the "."

1 Like

So you search one singe file with this exact name partition.[0-9]+ to name partition.[0-9]_org

It worked.. Thanks