I want to search files by using grep command in the directory /var/run

/var/run stores the processes running in the system and it has files with pid extension. I want to get a list of all the files with the extension pid .

Command I am using sudo grep -nr '*.pid' . (I am executing this command from the directory /var/run )

It shows no output. I am using Ubuntu 14.04 LTS.

  • That is not a correct description of /var/run. At best you are labouring under the misapprehension that PID files are a necessity, when in fact they are a dangerous and deeply flawed mechanism that the world is at oh-so-long last finally learning the wisdom of getting rid of.
    – JdeBP
    Feb 7, 2018 at 6:39

1 Answer 1


TL;DR: grep is wrong tool, use find with correct options

If you do stat /var/run you'll quickly find out that /var/run is symlink to /run directory.

$ stat /var/run
  File: /var/run -> /run
  Size: 4           Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d  Inode: 696874      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-02-07 13:17:01.225178554 +0800
Modify: 2017-12-18 20:44:12.898113636 +0800
Change: 2017-12-18 20:44:12.898113636 +0800

So you really need /run directory instead. As for searching files with specific filename, you need find command:

$ find /run -name "*.pid" 

Because some files in that directory belong to root or other system users, you may need to use that command with sudo.

Alternatively, you can use -L flag to allow following symlinks and call find on /var/run:

$ find -L  /var/run -name "*.pid" 

Please note also, that grep is wrong tool for the job. grep is for searching text patterns inside text files, not in their filenames.

You also mentioned:

/var/run stores the processes running in the system and it has files with pid extension

That's actually incorrect. Process information belongs in /proc. The .pid files are simply used by some programs to prevent multiple copies of same process running (well, one of possible ways these files can be used). See this stackoverflow post for reference, as well as this highly voted answer on unix.se. While the directory belongs to root user, please don't assume that it's for startup and daemon apps only; scripts initiated with root permissions by user could write to the directory just as easily.

  • Using /var/run/ will avoid the need for -L. Also bear in mind that Ubuntu 14 LTS uses Upstart, and the thing that actually prevents multiple copies of the same program running as multiple processes is Upstart knowing not to start them.
    – JdeBP
    Feb 7, 2018 at 6:50
  • @JdeBP Sorry, find /var/run alone doesn't work in my case. find --version reports find (GNU findutils) 4.7.0-git. So at least with my version of GNU find it does need -L flag. I am not sure about how BSD find handles things. Nice to know that about 14.04 Ubuntu's Upstart feature, though that doesn't help with scripts which want to avoid multiple copies of self running, so at least on askubuntu we have been suggesting to use .pid files for that. Feb 7, 2018 at 6:54
  • You speed-read what I wrote. Read it again slowly, and note the exact sequence of characters. (-:
    – JdeBP
    Feb 7, 2018 at 7:38
  • @JdeBP Oh...Adding final slash works... Feb 7, 2018 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.