Renaming a file to the same name

Hi All,

I was wondering if anyone knows how i take in a file, format the file, and then rename the file as the same name as the input file in shell script. Here is an example of what I am doing:

if [[$1=""]]
then
echo "please enter a filename"
else
export infile=$1

I take in a file "$infile"

Then I do my formatting.....

Now I want to rename the formatted file back to $infile. How can I do this? Right now it is renaming the file before any formatting is done...and the formatted $infile is empty. Please help me if possible. Thanks.

I would reformat the file to a temp file and when formatting was done, copy it back to the original filename.

Depends on your formatting you are doing.....if you're just sorting then you're okay to use the same filename for output - just use the -o flag. If your doing pretty much anything else - which I assume you are...then temp file is the only way.

I'd use mv rather than cp though.....so you don't have left over files.

Thanks guys...I got it. I was just unsure as to how I name the temp file. I got all kinds of errors, but tweaked it and now it works. Thanks for your input!!..much appreciated!

-brian :slight_smile: