Score:18

How do I find the source of a command line operation?

ng flag

Suppose I’m able to type abc in the command line and it will run (so the shell doesn't say "abc: command not found").

How can I find out what abc is or does? Is it a script? Program? Alias?

cn flag
Can you share more details? If you type `abc` and the shell returns `command not found`, then there is no script, program, or alias on your system that can answer the given question
Peter Cordes avatar
fr flag
@NicoHaase: I read it that way initially, but it seems the intended meaning was "running `abc` does not produce "command not found". Instead it does something, anything." Having the only quoted output in the question be "command not found" makes that part really stand out, and the phrasing is arguably ambiguous with meanings like "abc is supposed to do something, but instead ot returns command not found". So yeah, the question could use an edit to rephrase. I submitted an edit to clarify, since there has already been 1 comment and 1 answer based on the wrong interpretation.
Score:26
hr flag

You can use the type command, ex. type abc. For example, in a bash shell:

$ type while cd ls gcc apt
while is a shell keyword
cd is a shell builtin
ls is aliased to `ls --color=auto'
gcc is /usr/bin/gcc
apt is hashed (/usr/bin/apt)

The plain type command only shows the first result. If there are multiple versions of abc in different locations on your PATH, or abc is provided as both a shell keyword and an external executable, or to see both the aliased and unaliased versions of a command, you can use type -a to list all of them ex.:

$ type -a time
time is a shell keyword
time is /usr/bin/time

$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

$ type -a datamash
datamash is /usr/local/bin/datamash
datamash is /usr/bin/datamash

In bash, type itself is a shell builtin. Other shells such as zsh and ksh and dash (which provides /bin/sh in Ubuntu) provide similar functionality (although dash doesn't currently provide type -a). In tcsh, the nearest equivalent is the builtin which command - not to be confused with the external which command - see Why not use “which”? What to use then?

For commands that are identified as external programs (i.e. have a path, like /usr/bin/gcc) you can use the file command to find out what sort of program:

$ file /bin/ls /usr/bin/gcc /usr/sbin/adduser
/bin/ls:           ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=2f15ad836be3339dec0e2e6a3c637e08e48aacbd, for GNU/Linux 3.2.0, stripped
/usr/bin/gcc:      symbolic link to gcc-9
/usr/sbin/adduser: Perl script text executable
ilkkachu avatar
co flag
and for shell functions, in Bash, `type func` would print the function definition too. In Zsh it doesn't, but you can run `typeset -p -f func` to see it
Score:6
zw flag

For already installed commands use steeldriver's answer.

For not installed commands read below.

There is special package named command-not-found. Its purpose is

Suggest installation of packages in interactive bash sessions

Once installed this package will do its job and suggest you installation of deb-package with known executable name.


If you know executable name and/or part of its filepath, then you can find its package using one of two options:

  • local apt-file by

    sudo apt-get install apt-file
    sudo apt-file update
    apt-file search bin/htop
    

    to get something like

    htop: /usr/bin/htop
    
  • online using package contents search on https://packages.ubuntu.com - see results at this link.

Score:3
cn flag

There are a couple of other possibilities:

which abc

will return the location of the program abc in your system.

For example,

which cat
/bin/cat

If your program abc came with some documentation, it is possible to find more about it by running

man abc

This would show you the manual page if any for this program. You can learn much more about its usage, command line options and parameters. You might even find examples of how to use abc or a web page where the maintainers keep the program going.

An alternative to man or manual pages is a utility called info. Some program maintainers wish to give you the same or similar content to a man page using info instead.

info abc

for example will show you what help may be offered.

Since you mentioned aliases, you may show the aliases and their definitions with the alias command

alias

Here's the sample output on my Ubuntu 20.04 machine

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echoterminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
dave58 avatar
tr flag
:-) The methods I use...
mondotofu avatar
cn flag
These techniques are transferable to many Unix / Linux flavors as well.
ilkkachu avatar
co flag
This would miss shell functions, and e.g. shell builtins don't generally have man or info pages of their own.
dave58 avatar
tr flag
@ilkkachu : In which case (the shell function built-ins) the method discussed in the primary answer above, the 'type' built-in, provide suitable results. You should note that mondotofu's answer was an expansion of previous responses... [ e.g. "There are a couple of other possibilities:" ]
ilkkachu avatar
co flag
@dave58, in that case, it should probably refer to the other answers and solutions there, and discuss the limitations of the solutions presented here. "other possibilities" also sounds like they'd be alternatives, and not just supplementary information, but that might just be me.
Score:-1
br flag

I'm assuming we're talking about bash shells (or maybe zsh if it's similar enough) here...

Commands like which and whereis, which other answers have mentioned, look for matching files in the directories listed in the PATH environment variable. So you could also just check for a command called abc in those directories manually. The whereis command does a bit more, checking places listed in other environment variables, looking for files matching the name given with some standard extensions, etc... Also, if abc is found in multiple locations, which only shows the one that would be run whereas whereis shows them all.

The shell might also map abc to a shell alias or a shell function. Aliases can be listed with the alias built-in command and they are pretty straight forward.

Shell function names can be listed with typeset -F, so you can search the output from that command to look for abc. If you want to set what code is associated with a given shell function you can use the type abc.

Score:-3
st flag

Suppose I’m able to type abc in the command line and it does anything but return

abc: command not found

How can I find out what abc is or does? Is it a script? Program? Alias?

None of the above. If it wasn't found, then it isn't anything. You could create or install (or add to the search path) a script or a program or an alias named abc, then it will be a script or a program or an alias. But at the moment, it is nothing.

ng flag
The question says that typing `abc` does *not* return "command not found".
ilkkachu avatar
co flag
if something is "anything but X", it means it's definitely not X, likely the exact opposite. I'm anything but cheerful in the morning before I've had my coffee, and it should be clear here that the intent is to ask about a situation where the command does something, anything, _other than_ produce that error.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.