Monday, January 21, 2013

fuser command in Linux

Suppose you want to remove a folder from your system.However,when you try to remove that folder using rm command,it is not able to delete some files in the folder because they are being used by some process.In this situation,the fuser command can come in handy for you.

The fuser command is a powerful tool provided by the unix/linux developers.It gives information about the file user or the process(or executable) that is currently using the file.The file may be a regular file or a directory or even an executable.

Who is using my file/directory

Using the fuser command you can identify which processes are using a particular file/directory.

At the console type this command
fuser .

Include a space between the command and the dot.The dot indicates the current working directory.

We can see a number of process IDs followed by a character 'c'. This indicates the type of access.The type of access can be any one of the following:

c - indicates current directory
e - executable being run
f - Open file.f is omitted in default display mode
F - Open file for writing.F is omitted in the default display mode
r - root directory
m - mapped file or shared library

So 'c' in the output means that these processes are using this directory as their current directory.

Services running on a port

The fuser command can also find out which service(s) are running on a particular port of your system.The port might be a tcp/udp port.For a tcp port (say 3306-the mysql default port) use this command

fuser -n tcp 3306

Next,run the following command to find out the service name from PID you got

ps -ef|grep <Pid obtained from the previous command>

Alternatively,you may use the -v flag to obtain a detailed info
fuser -v -n tcp <port number>

No comments: