+3 votes
253 views
See which process and who is using a Linux file

in Linux / Unix by (551k points)
reopened | 253 views

1 Answer

+4 votes
Best answer

1. Use the lsof command to detect the use of files in Linux
2. Use the ps command to detect the use of files in Linux

The Linux operating system has been developed with a series of processes which allow both the applications and the parameters of the system to run without problems according to their respective task. It is necessary that we understand that each process carries out an action in particular, some processes will allow us to connect to the network, others will make it possible for us to access programs, etc., so that if something happens with a process it may harm certain actions to be carried out about this. The processes are represented by those programs that are running and are identified through the PID or process number that is assigned when it is executed or started..

When different users can access the system , as is normal, it can happen that someone has opened a file and makes it impossible for us to access this process. Another thing you have to tell is that if we name you "file" our mind imagines a text document, an image, a spreadsheet, etc., but in Linux operating systems a file goes far beyond this concept and that there a file can be:

Types of files
  • A network file
  • A block file
  • A directory
  • Special character files
  • Applications or programs and much more.

Do you see that Linux changes the way of thinking about the concepts? Therefore, it is more than necessary that we are able to understand how to access these files and processes in order to visualize which user has it running in real time and if it is possible to take the necessary actions as the case may be.

TechnoWikis will explain to you through this tutorial how we can perform the process of visualizing the processes and knowing in detail that the user has it open..


1. Use the lsof command to detect the use of files in Linux

The command lsof is a command integrated in Linux thanks to which we can obtain information about the files that are opened by processes in the system, that is, lsof is responsible for listing the files opened in the Linux distribution used.

To begin to understand its use we will execute the following line:

 lsof / dev / null 
The file / dev / null is a special file which is able to omit the information that is written in it as well as the information that is redirected to it.
As a result of this order, we will see details such as:
  • Command used
  • Process PID
  • Active user
  • Type of service
  • Device
  • Used node
image
Deploy only a user's processes
As it is natural that several users are acting simultaneously, we can apply a filter to display only the processes or files of a particular user, for example the user "TechnoWikis", for this we will use the -u parameter as follows:
 lsof -u TechnoWikis 
As a result, we find much more complete details about that particular user: image
Show details of a protocol service
Since lsof is a very integral command, if we want to know in detail the processes or services of a certain protocol, for example, TCP, we will execute the following:
 sudo lsof -i TCP 
image

In case of defining a particular port we must execute the following:

 sudo lsof -i TCP: 80 
See current network activity
To visualize the network activity in real time in Linux we will execute the following line:
 lsof -i 
image
Validate services and ports
If the objective is to validate the services and / or ports that are listening, we should use the following line:
 lsof -i -nP 
image
View specific open files
If it is required to see the files opened by a specific process or program, the line to be used will be the following:
 lsof -c cupsd 
image

In order to observe the files that are open in a specific directory, we must add the -D parameter like this:

 sudo lsof + D / etc 
image
List files by addressing
To list the files based on the Internet address, we have the following options:
 lsof -i 4 (IPv4 Addressing) lsof -i 6 (IPv6 Addressing) 
image

2. Use the ps command to detect the use of files in Linux

As administrators of Linux on many occasions we must see all the active processes at that moment. This may be due to administrative or support tasks, to visualize the processes in Linux, just execute the ps (process status) command:

List processes initiated from shell
 ps (this option only lists the processes that have been started from the current shell) 
image
Access details of all processes
If we want to access the information of all processes, we must add the -A parameter:
 ps -A 
image
Deploy all processes
In addition to this, we can combine the parameters a, u and x together, with what purpose ?, Simple, deploy all the processes that are being executed for all users of the Linux distribution, as well as detailed information such as:
  • The user name of the process
  • The start time of the process
  • CPU load of the process
  • Command with which the process was started
  • TTY and more details
 ps aux 
image

TechnoWikis always gives you the best advice so that the information and management tasks are as complete as possible and thanks to these options we can know the processes and open files in Linux..


by (3.5m points)
edited

Related questions

+3 votes
1 answer
+4 votes
1 answer
asked Jun 2, 2020 in Linux / Unix by backtothefuture (551k points) | 307 views
+3 votes
1 answer
+5 votes
1 answer
asked Oct 24, 2019 in Linux / Unix by backtothefuture (551k points) | 213 views
+3 votes
3 answers
asked May 10, 2019 in Linux / Unix by backtothefuture (551k points) | 315 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users