flock
file managementLinux/Unix
The flock command is one of the most frequently used commands in Linux/Unix-like operating systems. flock Manage file locks from shell scripts
Quick Reference
Command Name:
flock
Category:
file management
Platform:
Linux/Unix
Basic Usage:
flock [options] [arguments]
Common Use Cases
Syntax
flock [options] file|directory command [arguments]
Options
Option | Description |
---|---|
-s, --shared |
Get a shared lock (allowing other shared locks but not exclusive locks) |
-x, --exclusive |
Get an exclusive lock (the default) |
-n, --nonblock |
Fail (with an exit code of 1) rather than wait if the lock cannot be immediately acquired |
-w, --wait seconds |
Wait for a limited amount of time instead of indefinitely or failing immediately |
-o, --close |
Close file descriptor before running command |
-E, --conflict-exit-code number |
Use the specified exit code when conflict occurs (default is 1) |
-u, --unlock |
Drop a lock (usually not needed as locks are dropped automatically when the process terminates) |
-c, --command command |
Run a single command string through the shell |
Examples
How to Use These Examples
The examples below show common ways to use the flock
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
# Basic Examples Basic
flock /tmp/lockfile.lock command args