I'm trying to get a auto completion for folders that I don't have permission to view / enter, my first thought was to use compgen and sudo to do the completion manually, but looks like compgen is a shell builtin, so launching it with sudo may not work.

Also, if I wrote customized completion function for folders, how can I overwrite for every command? I don't want to do complete command for everything.

Or is there any alternative ways?


This is useful in a environment where you grant user sudo permission (to run arbitrary commands), but don't want them to login / su as root, so when they need to navigate to some folders accessible only by root, they can quickly view the contents without run commands like sudo ls XX

  • How about su root? :O – goldilocks Mar 31 '13 at 10:44
  • 2
    Why complete names of directories you can't access? It makes no sense at all. – vonbrand Mar 31 '13 at 14:55
  • Hi @vonbrand can you see my updates? – daisy Apr 1 '13 at 1:27
  • What exactly is wanted as an answer for this question? I proposed using TAB autocompletion, but it was downvoted before I deleted my answer. – Devyn Collier Johnson Jun 9 '13 at 19:14
  • Hi @DevynCollierJohnson, everyone know about Tab key ;-) But have you tried completion for /root? It won't work because you don't have permission to read the contents of it. That's what I want by inaccessible folders – daisy Jun 10 '13 at 1:44

I would recommend using an interactive sudo session as an alternative; use either

sudo -i


sudo -s

to get a root shell.

sudo -s will keep your current environment (so e.g. $HOME is still the home directory of your normal user), while sudo -i will simulate an initial login as root, so e.g. $HOME will point to /root.

Adapting the tab completion of your normal user to work with sudo may be possible, but it seems rather problematic: if you haven't used sudo in the last 15 minutes, it would either prompt you for your password while you are trying to use the tab completion, or it just wouldn't work and you would have to run sudo manually to refresh the credential cache before it could work again. Also, I don't know how to do it and it sure seems like a complicated workaround for a simple issue :)


What you're really asking for is a restricted shell run with elevated privileges. The problem is the bash shell the user is running doesn't have permission to get those directory listings so it cannot leverage auto-complete.

If you're opening whole directories for certain people with sudo access, then you should create a group for those people and set rX permission accordingly (or use facls).

If you're only providing specific commands in sudo, then users can do:

sudo -l

to get a listing. Someone clever might be able to translate the output of 'sudo -l' into something that bash auto-complete can handle, but I haven't seen it yet. (And again, it would only work if you're specifying the commands explicitly.)

If you're literally doing as in your example (giving people sudo to ls only) then just go ahead and make all the specified directories +rx for those folks...

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.