1

Is it possible to remove a terminal command? For example, if there was a command call lookup (not an alias), how would I go about removing it?

  • 1
    I don't this it's clear what is meant by "command call lookup," but if the goal is to "remove" external commands, I don't think it's clear what sense of removal is meant. Considering the content of the accepted answer, the obvious interpretation--to delete the executable providing the command--does not seem to have been insisted upon. So I think this question is unclear and likely even too broad. Usually I am reluctant to close questions with answers people have found useful, even if they are vague, but in this case I cannot imagine what this could (correctly) help anyone solve or understand. – Eliah Kagan Sep 26 '17 at 00:57
  • This sounds like an X-Y Problem. What are you actually trying to achieve? – David Foerster Sep 26 '17 at 13:14

3 Answers3

7

A better way would be to redirect the output of the command to /dev/null. That way, the command will 'be there' but will show no results.

Example: the ls command.

  1. Make an alias
    alias ls = "ls > /dev/null"
  2. Running ls now will not show any results.

EDIT: Thanks to @scai
In the answer above, the command will still show errors because we have redirected stdout but not stderr plus a more serious issue, it will still run the command.

Quoting @scai:

If the command in question does things you don't like, for example creating or deleting files, changing settings, painting your nose blue, then this alias won't stop it from doing these things.

For the workaround, we will just alias the command to an empty string, then just redirect the resulting error to /dev/null.

alias ls = "" > /dev/null 2>&1

That's much better now.


EDIT 2: Thanks to @scai again :-)
This is a temporary solution.
To revert back to the original function of the command (in our case ls), just run unalias ls.

Parto
  • 15,325
  • 24
  • 86
  • 117
3

You can do that but you shouldn't.

Anyway there are four main places for commands.

/bin & /usr/bin & /sbin & /usr/sbin

To know the command you want where is

whereis COMMAND

Then as a root you can delete or just rename or move to other directory

sudo rm /path/command

Or

sudo mv /path/commad /newPath

Be sure that the newPath is not in your $PATH environment

Maythux
  • 84,289
  • I tried that, but instead of getting a path, I got "lookup: " – George Newton Mar 07 '14 at 07:38
  • Then there is no command lookup. you can check that what is output of command lookup – Maythux Mar 07 '14 at 07:47
  • In that case lookup could be a shell-builtin and not a real application. – scai Mar 07 '14 at 10:16
  • lookup command gives: The program 'lookup' is currently not installed. You can install it by typing: sudo apt-get install lookup This means lookup is not shell-builtin – Maythux Mar 07 '14 at 10:17
  • 1
    Are you using GeorgeNewton's shell or how are you able to check if lookup is a shell-builtin for him? – scai Mar 07 '14 at 10:30
  • I told you in the comment before. just try to run lookup command in your terminal and tell me the result – Maythux Mar 07 '14 at 10:31
  • I told GeorgeNewton he doesn't have this command because when he ran command where is the result was lookup: thi means that the command is not found. you can try it yourself try whereis with ping for example and try it with lookup and you'll understand me – Maythux Mar 07 '14 at 10:33
  • Every shell has different shell-builtins. Unless you know which shell George is using you can't make any assumptions about it. – scai Mar 07 '14 at 10:34
  • @scai I know that and I told you how I know... Whan whereis return no result then the command is not found. I'm not a foreteller – Maythux Mar 07 '14 at 10:35
  • You didn't get my point. whereis isn't able to locate shell-builtins. Likewise, command-not-found can suggest to install a package containing a command which is also shell-builtin at the same time. – scai Mar 07 '14 at 10:44
3

I think this "proper" way to do this would be to use permissions. Let's take the ls command as an example as well.

-rwxr-xr-x 1 root root 105840 /bin/ls*

Here's how I would do it :

Create a group for users allowed to use ls, let's say we call it lsusers.

sudo addgroup lsusers

Add users to this group accordingly

sudo usermod -aG lsusers [username]

Remove world permissions on /bin/ls

sudo chmod o= /bin/ls

Change the group ownership of /bin/ls

sudo chgrp lsusers /bin/ls

Make sure group permission allow at least executing

sudo chmod 0710 /bin/ls

This should bring something like :

-rwx--x--- 1 root lsusers 105840 /bin/ls*

Which allows root and lsusers members to execute /bin/ls. Actually, the big problem of aliases is that they can be overridden most of the time. Changing permissions of something belonging to root, now that is a little harder.

Now, about real removal, well this is a package problem. If you want to remove a program (command) from your system, just ask for its package to be removed with :

sudo apt-get --purge remove [package name]

Of course, this is not the kind of things you could do with /bin/ls (and I'm not sure you'd like to actually rm it :p)

John WH Smith
  • 2,018
  • 14
  • 21
  • Note that even this will not usually achieve any security-related goal of making a command unavailable--ordinarily users can run their own executables, so a user could simply make (or bring from the outside) their own copy and run that. The exception is setuid/setgid executables like sudo--while users could still run their own copy, they could not cause them to be owned by root, thus they would not be setuid root. – Eliah Kagan Aug 12 '14 at 04:06