Somewhere in my system (the problem is I don't know where) I have a symlink to which is given priority by bash but the link is broken and therefore it doesn't work.

More specifically:

  • current directory: mysymlink
  • somewhere in path (installed by a package): mysymlink

when running mysymlink from current directory, the (broken) symlink mysymlink somewhere in the file system is called.

I am trying to find that symlink, delete it so I can use the one in the current directory.

How do I find it?


command -v testcafe


ls -l /usr/local/bin/testcafe

ls: cannot access /usr/local/bin/testcafe: No such file or directory

(Note: I removed myself the target path by mistake)

How to remove this symlink (somewhere in the system)?

closed as unclear what you're asking by Scott, Archemar, phk, Rui F Ribeiro, Toby Speight Jun 9 '17 at 14:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your question is unclear, and you should tell what you really want to do ... and what commands you have tried... Is your thing a testcafe or a mysymlink? And what exactly is your $PATH ? – Basile Starynkevitch Jun 8 '17 at 13:48
  • I explained it to my best as I am not very very experienced with synlinks. the answer I accepted explains it better. I pasted most of the updated information in my question too – dragonmnl Jun 8 '17 at 16:35

Maybe you should try (assuming that ls is not aliased; if it is replace it by /bin/ls)

ls -l $(which mysymlink)

since which mysymlink gives the full path of the command mysymlink (i.e. expands using $PATH variable)

You need to try that command outside of the directory containing mysymlink.

BTW, it looks like . is in your $PATH. You can check with echo $PATH. And having the current directory in your $PATH can be a security risk.

ls: cannot access /usr/local/bin/testcafe: No such file or directory

Then check with ls -dl /usr/ /usr/local/ /usr/local/bin/ which of these directories don't exist (perhaps you forgot to mount some file system, or you removed by mistake, as root, some of these directories).

And of course do also a ls -l /usr/local/bin/testcafe

(you should try with /bin/ls instead of ls because I guess that your ls is aliased or redefined)

How to remove this symlink (somewhere in the system)?

You need first to find it. Use find(1) appropriately (probably with -lname)

  • I removed myself the broken target path. I'm trying to understand where symlink originally pointing to it is located – dragonmnl Jun 8 '17 at 13:40
  • @dragonmnl: That should go into your question, which you should edit. – Basile Starynkevitch Jun 8 '17 at 13:41
  • sure, just did it – dragonmnl Jun 8 '17 at 13:42

command -v UTILITY will show you what the shell would try to execute if you gave the command UTILITY.

Since it reports


but then

ls: cannot access /usr/local/bin/testcafe: No such file or directory

when you try to ls -l it, it leads me to believe that the reported location of the testcafe utility has been cached by the shell, and since removed.

To refresh the shell's cache, use hash -r (this command outputs nothing). This will flush the cache and the shell will then do a full path lookup the next time you run testcafe.

  • that just shows what the symlink runs, not where the symlink is (i.e. it shows the broken link, but not where the symlink is "taken") – dragonmnl Jun 8 '17 at 13:32
  • 1
    @dragonmnl It will show you the location of the symbolic link. Using ls -l on the link will tell you where it points to. If that too is a symbolic link, repeat until you find the broken ink. – Kusalananda Jun 8 '17 at 13:34
  • please see the concrete example in my question (just added) – dragonmnl Jun 8 '17 at 13:37
  • @dragonmnl It is hashed. Use hash -r to rehash. – Kusalananda Jun 8 '17 at 13:38
  • 1
    @dragonmnl ... and then try the command again. – Kusalananda Jun 8 '17 at 13:41

To find broken symlinks in all directories in your PATH, with bash:

IFS=: read -ra dirs <<<"$PATH"
find -L "${dirs[@]}" -type l -maxdepth 1

Add -delete to find to delete them.

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