Home Linux Commandline Tips How to Lock a Text File in Linux Using flock Command

How to Lock a Text File in Linux Using flock Command

Before we explore the techniques/approaches of locking a text file under a Linux operating system environment, we should first understand the logic behind Linux’s file locking mechanism.

Linux’s file locking mechanism restricts/controls file access among multiple processes. When a text file is successfully locked, only one process can access it on a specific time schedule.

Before proceeding with this article, please understand that file locking is very much different from file encryption or file access control where a passphrase or password is needed to control user access to your files.

When a file is locked under a Linux operating system environment, a mutual exclusion event is created where only a single process can access it at a time.

Problem Statement

The Linux operating system will automatically block any write action attempts directed to an open file being written on by another process. However, what if you want to revoke the read and/or write permissions already invoked by the first process that has your file open and in write mode? Is there a workaround to this problem?

Moreover, we might also want to lock our file so that no other process interferes or attempt to disrupt the write mode status already initiated. This article will walk us through a viable solution to lock a text file in Linux.

Sample Reference File

For this tutorial to be more engaging and worthwhile, we should have some reference text files. We will concentrate on the text files inside the following directory:

Sample Text Files
Sample Text Files

For instance, let us open the file output.txt and start writing on it.

$ sudo nano output.txt 
Write Content to File
Write Content to File

While this file is still open, let us try to open it from another command line tab.

$ sudo nano output.txt 

You will get the following response:

File is being edited by root
The file is being edited by the root

The above screen capture relays the PID (133110) of the process of working on the text file with the option of opening the file while it is still in that process’s write mode.

By keying in Y from our keyboard, we will have opened this text file and handed it over to a different process with exclusive write mode access.

Open Text Files
Open Text Files

Therefore, a file modification by user 1 will lead to the following prompt on user 2 while attempting to save the file.

File was modified
File was modified

This instance is a perfect representation of two different users on the network working on a single file.

Lock a Text File Using Linux’s flock Utility

To solve this issue, we need the aid of Linux’s flock utility. Since the util-linux package hosts flock command, it should be available in almost all Linux OS distributions. This command manages specific file/directory locks via the Linux command line environment.

To lock a text file in Linux, we will reference the following syntax:

$ flock -x PATH_TO_FILE_TO_LOCK -c COMMAND 

The -x option is for obtaining a write lock to the targeted file. Let us attempt to lock the sample output.txt text file. The -c option will enable us to pass a single Linux supported command e.g. cat command.

$ flock -x /home/dnyce/LinuxShellTips_Files/output.txt -c cat

The terminal instance above will remain active to signify that the text file has been locked.

If we open another terminal (while this terminal instance is still running) and execute another flock command on this same file, we should be denied access to it.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $?

1

The -w option is used to relay the wait time of .007 seconds before a lock is placed on the text file. We then execute echo $? to output the exit status of this command.

An exit status of 0 implies that the command was executed successfully and an exit status of 1 implies the command could not be executed due to an error. In this case, the text file is under lock by another process.

To further confirm that the text file is under lock, we can use the lslocks commands to list all active local system locks.

$ lslocks
List File Locks in Linux
List File Locks in Linux

As you can see, our file is present.

Unlocking a Text File in Linux

Canceling the initial flock command (Ctrl+c) or closing the text file should release the lock making it possible to successfully run the following command on the secondary terminal.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $?
Unlocking File in Linux
Unlocking Files in Linux

The exit status of 0 implies that the lock is no longer applicable to the text file.

We have learned how to lock a text file in Linux so that only one process can use it (write mode) at a time. The implementation of this article is particularly useful when different users on a network are accessing a single file.

More on flock command can be found on its man page.

$ man flock
Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.