Does the Linux system lock the file when I copy it?

bash, linux, shell

Solution

You could check from your script whether the file is being written to, and abort/pause copy if it is...

fuser -v /path/to/your/file | awk  'BEGIN{FS=""}$38=="F"{num++}END{print num}'

If the output is smaller 1 you're good to copy :)

Problem

I have wrote a program and it will update a file periodically, sometimes I want to copy the file into another computer to check its content. If I copied the file when the program was not writing it, there is no problem. But, if I copied the file when the program was writing it, the copied file would be partial. So, I wonder that, if the Linux system exists the lock strategy to prevent the situation. In fact, I copy the file in a bash script, so I want to check if the program is writing it in the bash script. If yes, the bash script will check its state after some seconds and then copy its completed version. So in bash script, how could we check the file was opened or modified by other programs?

Original source