2

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.

1

3 Answers 3

2

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
9
  • that gives lots of errors
    – rubo77
    Sep 22, 2012 at 7:27
  • yes, of course. run it as root.
    – Serge
    Sep 22, 2012 at 7:28
  • Yes i did run it as root. /proc seems to be the problem
    – rubo77
    Sep 22, 2012 at 7:31
  • I modified the command - now it skips the /proc tree
    – Serge
    Sep 22, 2012 at 7:37
  • that's sometimes! working, but it finds other tmp symlinks like /var/www/someapp/html/tmp -> ../tmp
    – rubo77
    Sep 22, 2012 at 7:43
1

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...

0

I found the answer, thanks to this question: Linux: Find all symlinks of a given 'original' file? (reverse 'readlink')

find / -exec ls -ald {} ';' 2>/dev/null | grep '\-> /tmp' |grep -v /proc/
1
  • This will produce the result you are looking for but it will be much slower than solution proposed by me as it will exec ls command for each any file it find, while my solution executes ls only for sym links that ends up with '/tmp' suffix
    – Serge
    Sep 22, 2012 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .