Detect all links that exists pointing to /tmp?

I want to find out which file links exist that point to my /tmp folder on Ubuntu.

Background:
I had a problem, that my /tmp folder privileges were changed by some accident to 755. The system wasn't running correctly any more as a user other than root.

Maybe there is a link to /tmp somewhere and the command

chmod 755 . -Rf 

called there in that folder was affecting the main /tmp folder.

I had to repair it with:

chmod 777 /tmp
chmod +t /tmp
sudo chown root:root /tmp

I have only one file system, so there shouldn't be a problem with that.


try this:

find / -path /proc -prune -o \( -lname '*/tmp' -o -lname '*/tmp/' \) -exec ls -l {} \;

Sample output:

# find / -path /proc -prune -o -lname '*/tmp' -exec ls -l {} \;
lrwxrwxrwx. 1 sergey sergey 4 Sep 22 11:27 /home/sergey/xxx/tmp -> /tmp
lrwxrwxrwx. 1 root root 10 Jun 20 10:28 /usr/tmp -> ../var/tmp
find: `/run/user/sergey/gvfs': Permission denied

As far as I know, there is no easy way to list all symlinks pointing to an inode. Therefore I would recommend using the standard find utility with the -L and -samefile switches:

  • -samefile will compare based on the inode of your target file,
  • -L will include symbolic links in the search, which is what you are looking for.

Here is an example with /bin/dash in /bin:

$ find -L  /bin -samefile /bin/dash -exec ls -lhi {} \;
786515 lrwxrwxrwx 1 root root 4 Mar 29  2012 /bin/sh -> dash
786436 -rwxr-xr-x 1 root root 108K Mar 29  2012 /bin/dash

From this output, you can see both with the inode number (first column) and the arrow (last one) that /bin/sh in a symlink to the /bin/dash. If they were hardlinks, inode number would be the same for different files and there would be no arrow in the last column.

Finally, If I had the need to look for these links on a whole filesystem, I would exclude some directories such as /dev, /proc and /sys from the search. This would be done with the -path and -prune swicthes (you can add as many as you want with -o switch meaning or), 2>/dev/null is here to mask potential errors:

$ sudo find -L  / \( -path /dev -o -path /proc -o -path /sys \) -prune -samefile /bin/dash -exec ls -lhi {} \; 2>/dev/null

As searching in a entire filesystem can take a lot of time (mainly depending on its size) I will consider this option as a last resort. It is like searching a needle in a haystack: it is not impossible but takes a lot of effort...