Displaying Open Files in Linux

How to Display Open Files in Linux Using Command Line Tools

If you’re working on multiple projects simultaneously, it can be challenging to keep track of which files are currently open for editing or modification. Fortunately, there is a simple command line tool that allows us to view the list of all open files in Linux: lsof (list open files). In this tutorial, we will learn how to use lsof to display open files and understand its various options.

First, let’s start with the basic syntax for using lsof:

bash
#!/bin/bash # This line specifies the interpreter to be used for executing the script

# This script uses the lsof command to display a list of open files in Linux

# Basic syntax for using lsof:
lsof [options] [filter]

# Options:
# -a: Specifies logical AND operation between filters
# -c: Filters by process command name
# -u: Filters by user name
# -p: Filters by process ID
# -i: Filters by internet address
# -t: Displays only process IDs
# -h: Displays help information

# Filter:
# <file>: Filters by file name
# <directory>: Filters by directory name
# <user>: Filters by user name
# <process ID>: Filters by process ID
# <internet address>: Filters by internet address

# Example 1: Display all open files
lsof

# Example 2: Display open files for a specific process
lsof -c <process name>

# Example 3: Display open files for a specific user
lsof -u <user name>

# Example 4: Display open files for a specific process ID
lsof -p <process ID>

# Example 5: Display open files for a specific internet address
lsof -i <internet address>

# Example 6: Display only process IDs for open files
lsof -t

# Example 7: Display help information
lsof -h

# Note: Multiple options and filters can be used together to narrow down the results.

# End of script.

The ‘options’ parameter allows us to customize our output based on specific criteria such as displaying only network connections or filtering by process ID. The ‘filter’ parameter is optional and can be used to narrow down the results further. Let’s take a look at some of the most commonly used options:

-i: This option displays information about network connections, including TCP/IP sockets and UDP ports. For example, if we want to see all open files that are currently connected to the internet, we can use lsof -i.

bash
#!/bin/bash # This line specifies the interpreter to be used for executing the script

lsof -i # This command displays information about network connections, including TCP/IP sockets and UDP ports. It lists all open files that are currently connected to the internet.

# The script is missing a shebang at the beginning, which specifies the interpreter to be used for executing the script. Also, it is not necessary to add annotations for the -i option as it is already explained in the context above.


#!/bin/bash

lsof -i

-c: This option allows us to filter by process name or ID. If we know which application is causing a particular file to be locked, we can use this option to identify it quickly. For example, if we want to see all open files that are being used by the Firefox browser, we can use lsof -c firefox.

# This script uses the lsof command to list all open files being used by the Firefox browser.
# The -c option allows us to filter by process name, in this case, "firefox".
# This helps us quickly identify which application is causing a particular file to be locked.

lsof -c firefox # Lists all open files being used by the Firefox browser.

-p: This option allows us to filter by process ID (PID). If we know which PID is causing a particular file to be locked, we can use this option to identify it quickly. For example, if we want to see all open files that are being used by the process with PID 12345, we can use lsof -p 12345.

# This script uses the lsof command to list all open files being used by a specific process ID (PID).
# The -p option allows us to filter by PID.
# In this example, we are filtering by the process with PID 12345.
lsof -p 12345

-n: This option disables name resolution for network addresses and port numbers. If we want to see all open files that are connected to a specific IP address or port number, but don’t care about the hostname or service name, we can use this option. For example, if we want to see all open files that are connected to the IP address 192.168.0.1 on port 80 (HTTP), we can use lsof -n -i :80 192.168.0.1.

# This script uses the lsof command to list open files connected to a specific IP address and port number.
# The -n option prevents the command from attempting to resolve hostnames and service names, making the output faster and more efficient.
# The -i option specifies that we want to list open files that are connected to a network address.
# The :80 specifies the port number we want to filter by.
# The 192.168.0.1 specifies the IP address we want to filter by.
lsof -n -i :80 192.168.0.1

-F: This option allows us to filter by file type or mode. If we want to see all open files that are currently being written to, for example, we can use lsof -F wr. For a complete list of available modes and their corresponding letters, refer to the man page for lsof (man lsof).

# This script uses the lsof command to list all open files that are currently being written to.
# The -F option allows us to filter by file type or mode.
# In this case, we are filtering for files with the "wr" mode, which stands for "write" and "read".
# This means we will see all open files that are currently being written to or read from.
# For a complete list of available modes and their corresponding letters, refer to the man page for lsof (man lsof).

lsof -F wr # This command will list all open files with the "wr" mode, showing us which files are currently being written to or read from.

-s: This option allows us to filter by file status or state. If we want to see all open files that are currently being read from, for example, we can use lsof -s READ. For a complete list of available states and their corresponding letters, refer to the man page for lsof (man lsof).

#!/bin/bash # This line specifies the interpreter to be used for executing the script

# This script uses the lsof command to list all open files that are currently being read from

lsof -s r # The correct option for filtering by file state is lowercase "r" for read, not uppercase "READ"

# The output of this command will include the following information:
# COMMAND - the name of the process that has the file open
# PID - the process ID of the process
# USER - the user who owns the process
# FD - the file descriptor number of the file
# TYPE - the type of file (e.g. regular file, directory, socket)
# DEVICE - the device number of the file
# SIZE/OFF - the size of the file or the offset of the file pointer
# NODE - the inode number of the file
# NAME - the name of the file

# For a complete list of available states and their corresponding letters, refer to the man page for lsof (man lsof)

-t: This option allows us to filter by file type or mode in combination with other options. For example, if we want to see all open files that are currently being written to and have a size greater than 10 MB (in human-readable format), we can use lsof -F wr -s +SIZE:10M.

# This script uses the lsof command to list open files based on specific criteria.
# The -F option allows us to filter by file type or mode, while the wr option specifies that the files must be open for writing.
# The -s option allows us to specify a size limit for the files, and the +SIZE:10M argument sets the limit to 10 MB.
# The output will be in human-readable format.
lsof -F wr -s +SIZE:10M

In addition to these options, there are many other advanced features and filters available in lsof that allow us to customize our output based on specific criteria. For a complete list of all available options and their corresponding syntax, refer to the man page for lsof (man lsof).

To summarize, using lsof is an easy way to display open files in Linux and troubleshoot file locking issues. By customizing our output based on specific criteria such as network connections or process IDs, we can quickly identify which applications are causing a particular file to be locked and take appropriate action.

SICORPS