How to Use lsof to Open Files on Linux

In this article we will try to teach you how to Use lsof to Track Down Open Files on Linux. LINUX is an operating system or kernel distributed under an open source license. Its functionality list is much like UNIX. The kernel is a program at the heart of the Linux operating system that takes care of basic things such as: B. to let hardware communicate with software. The lsof command stands for LiSt Open Files and shows open files and which process is using them. Since Linux considers every object like devices, directories, etc. as a file, unidentified open files prevent users from modifying them.

In addition, the sheer number of files makes it difficult to find malicious processes. The lsof command helps identify these processes so you can kill them. “lsof” stands for List Open Files. It is a Linux utility for listing all open files on a system. This command can be combined with various parameters to change its output as desired. Below we have mention the steps Use lsof to Open Files on Linux.

How to Open Files on Linux Using lsof

Installing lsof

lsof isn’t available by default on most Linux distributions but can be easily installed. Use the below command to install lsof:

$ sudo yum install lsof

  • for CentOS/RHEL 8, you can use the DNF command

$ sudo dnf install lsof

$ sudo apt install lsof

Common Usage

List all open files

Running lsof without any options will list all files that are currently open by active processes.

$ sudo lsof | less

List by filename

To list all processes that have opened a specific file, we can specify file-name as an argument:

$ sudo lsof {file-name}

List open files by username

In a multi-user system, you can filter the list of files by specific user-owned processes, using -u flag followed by username.

$ sudo lsof -u {username}

List open files by process

We can also list files opened by a particular process by using -c option followed by the process name.

$ sudo lsof -c {process-name}

List open files by PID

Alternatively, to list files opened by a process but instead of process-name you want to specify its ID, you can use -p flag followed by process-id.

$ sudo lsof -p {process-id}

List open files containing directory

To list processes that opened files under a specific directory, use +D option followed by directory path.

$ sudo lsof +D {path}

Repeat mode

lsof can be run in repeat mode. In repeat mode, lsof will generate and print output at regular intervals. Again, there are two repeat modes supported by lsof, i.e., with -r and +r flags. With -r flag, lsof repeats to execute until it receives an interrupt/kill signal from the user while with +r flag, lsof repeat mode will end as soon as its output has no open files. Additionally, we can specify time delay with -r or +r flag.

$ sudo lsof {arguments} -r{time-interval}

List open files with network protocol

lsof supports the listing of any type of Linux files which includes network sockets etc. As such we can list details of open network connections using -i flag.

$ sudo lsof -i

List open files by port

We can also filter the output of lsof with -i flag by port number using command syntax as below:

$ sudo lsof -i :{port-number}

List open files by IPv4/IPv6

There’s an option to filter network connections listing by limiting it to either IPv4 or IPv6. Use below command syntax to get only IP v4 listing:

$ sudo lsof -i4

Final Words

We hope our article on how to Use lsof to Open Files on Linux distros will help you and resolve all your problems. Linux is a Unix-like, open-source, and community-developed operating system (OS) for computers, servers, mainframes, mobile devices, and embedded devices. It is supported on almost all major computing platforms including x86, ARM and SPARC, making it one of the most widely supported operating systems. If you want to know how to Open Files on Linux Using lsof  then follow the steps mentioned above.

I hope you understand this article, How to Use lsof to Open Files on Linux.

James Hogan
James Hogan
James Hogan is a notable content writer recognized for his contributions to Bollyinside, where he excels in crafting informative comparison-based articles on topics like laptops, phones, and software. When he's not writing, James enjoys immersing himself in football matches and exploring the digital realm. His curiosity about the ever-evolving tech landscape drives his continuous quest for knowledge, ensuring his content remains fresh and relevant.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Articles

Hubspot Service Hub review 2024: a comprehensive platform

When it comes to customer support operations, HubSpot Service Hub is an all-encompassing customer service platform that is meant to...
Read more
When players on Windows 11 or 10 try to log in to Steam, they may get the error code E87....
Users of Windows 11 or 10 may find it frustrating to deal with the error number 147-0 in Microsoft Office....
The Microsoft Store is an important part of the Windows operating system because it gives users a single place to...
It can be hard to find the right balance between usefulness, durability, and cost when it comes to kitchen storage....
Both AirDroid and Vysor are well-known tools that help Android users control their devices and mirror them. One of the...