In Linux, files are a crucial part of the operating system because everything is a file in Linux. While working in Linux or UNIX operating systems, several folders and files are used. Some of them are visible, and some are not. Is there any method to check open files in Linux? In that case, the “lsof” command is used.
This article will demonstrate the method to check open files in Linux. The content for the post is:
Let’s start with the basics of the lsof utility.
What is lsof Utility?
The “lsof” (List Open Files) is a utility to list all the open files in the operating system. By default, it comes pre-installed in most of the Linux distributions but can be installed manually if it is not found in your system. Below are the commands for various Linux distributions:
$ sudo apt install lsof -y
$ sudo dnf install lsof
$ sudo pacman -S lsof
To verify the installation of the “lsof” command run the given command in the terminal:
$ lsof --version
The “lsof” version 4.93.2 is installed.
For all the options that can be used with the “lsof” command, run the help command in the terminal:
$ lsof --help
All the options will be listed in the terminal.
How to Check Open Files Using lsof Utility?
Using the “lsof” utility, users can check all the open files in Linux, and the output of the “lsof” depends upon the options used with “lsof”. There are various options available that can be utilized with “lsof”; the few most important options are discussed below.
Example 1: Listing All Open Files in Linux
To list down all the open files in Linux, simply type the “lsof” in the terminal:
The output of the above command is shown below:
All the open files have been listed.
Example 2: Listing All Open Files For Specific User
To list all open files for a particular user, use the “u” option and type the user’s name:
$ sudo lsof -u itslinuxfoss
Open files for specific users will be listed.
Example 3: Listing All Open Files For Specific Directory
To list down the files open in the specific directory, put the “D” option and then give the path for the directory:
$ sudo lsof +D /home/itslinuxfoss/
Files open in the specified directory will be listed
Example 4: Listing All Open Files For Specific File System
To list down the open files for the particular file system is obtained as follows:
$ lsof /proc
The above command will display the open files for the entire file system.
Example 5: Listing All Open Files For Specific Internet Protocol
You can also use the “lsof” command for specific internet protocols such as TCP, UDP, IPv4, IPv6, etc. Let’s list down the open files for IPv6. To do so, use the “i” option and type the version number:
$ sudo lsof -i 6
All open files belonging to IPv6 will be listed as seen in the above image.
Example 6: Listing All Open Files For Specific Process ID
To list down the open files for a specific process id, run the “lsof” command with the “p” option and then type the process ID:
$ sudo lsof -p 2584
All the open files belonging to the process ID (2584) have been listed.
That’s how to check open files in Linux.
To check the open files in Linux, the “lsof” (List open Files) utility is used, depending on various options that can be considered while running. This utility lets the user list the open files for a specific user, file system, directory, or process ID. This write-up has illustrated the method to check open files in Linux.