same file being opened by two users at a time

I want to avoid a situation where because two users simultaneously
open a file and modify and save, leaving the original file in mess.
Is there a way in UNIX to warn a user if that particular file is already being
used by another user.

Thanks in advance

Not by default. At least not portably across all *ix.

With regards to "mess", it's really not a mess issue, it's just that the last one out wins (in most cases).

What kind of file is it? Text files? You might consider some kind of version control system.... at least then there would be ways to prevent multiple editing or at least some way of handling merges.

In addition to cjcox advice.

You could intercept the editor and script a flag file to show that the file is being edited and then stop edits if the flag file exists. The flag method used by unix command "vipw" to prevent two "vipw" users editing the password file at the same time.

Depending on how the file is being edited you may find unix command "fuser" useful. It is however no good for editor processes which don't open the file for write (e.g. vi).

vim creates a temporary file .filename.swp and can identify if the original file is being edited.