Linux which command alternative

Is there an alternative to the `which` command? [duplicate]

If the which command is not available, is there another ‘standard’ method to find out where a command’s executable can be found? If there is no other ‘standard’ method available, the actual system I face currently is a bare Android emulator with an ash Almquist shell, if that means anything.

@StephaneChazelas that may mean my question is a duplicate. I’ve tried to search for this question but I missed what you linked. thanks!

well, the other question is perhaps properly described with that all you never thought you would ever not want to know about it — this leaves some room for mine . 🙂

3 Answers 3

This should be a standard solution:

you’re right, it works, thank you, I forgot type . Is there a reference that declares it standard, by the way? like some document at opengroup. If there is, can you link it perhaps?

type and command -V/v are standard (Unix, LSB, but optional in POSIX (XSI), type -p and type -t are not.

You can search the $PATH yourself to find a command:

COMMAND=vim # This is the command to search for (IFS=:; for dir in $PATH; do [ -x $dir/$COMMAND ] && echo $dir/$COMMAND; done) 

(this should work in ash and many other Bourne shell derivatives)

If your shell or keyboard doesn’t support «[]» (I think ash does), you can replace [ -x $dir/$COMMAND ] with test -x $dir/$COMMAND (test is a bash/ash built-in, and may also be available as a standalone executable

🙂 it’s not actually my keyboard, my ash on this Android emulator seems not to come with either [ or test . I’m not sure if I’m doing anything wrong, the system seems stock and reports ash for shell.

I would regard any shell without either test or [ . ] as unusable for any but the most limited purposes.

Not quite the same, but should give you the binary’s location like ‘which’ does.

Linked

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43530

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Читайте также:  Установка заббикс агента linux

How to use `which` on an aliased command?

The problem is that if I want to use which to see where my vim / grep / ls /etc is coming from, the alias gets in the way:

$ which vim vim: aliased to vim -X 

This is useful output, but not what I’m looking for in this case; I know vim is aliased to vim -X but I want to know where that vim is coming from. Short of temporarily un-defining the alias just so I can use which on it, is there an easy way to have which ‘unwrap’ the alias and run itself on that? Edit: It seems that which is a shell-builtin with different behaviors across different shells. In Bash, SiegeX’s suggestion of the —skip-alias flag works; however, I’m on Zsh. Does something similar exist there?

7 Answers 7

which is actually a bad way to do things like this, as it makes guesses about your environment based on $SHELL and the startup files (it thinks) that shell uses; not only does it sometimes guess wrong, but you can’t generally tell it to behave differently. ( which on my Ubuntu 10.10 doesn’t understand —skip-alias as mentioned by @SiegeX, for example.) type uses the current shell environment instead of poking at your config files, and can be told to ignore parts of that environment, so it shows you what will actually happen instead of what would happen in a reconstruction of your default shell.

In this case, type -P will bypass any aliases or functions:

You can also ask it to peel off all the layers, one at a time, and show you what it would find:

$ type -a vim vim is aliased to `vim -X' vim is /usr/bin/vim 

(Expanding on this from the comments:)

The problem with which is that it’s usually an external program instead of a shell built-in, which means it can’t see your aliases or functions and has to try to reconstruct them from the shell’s startup/config files. (If it’s a shell built-in, as it is in zsh but apparently not bash , it is more likely to use the shell’s environment and do the right thing.)

type is a POSIX-compliant command which is required to behave as if it were a built-in (that is, it must use the environment of the shell it’s invoked from including local aliases and functions), so it usually is a built-in.

It isn’t generally found in csh / tcsh , although in most modern versions of those which is a shell builtin and does the right thing; sometimes the built-in is what instead, and sometimes there’s no good way to see the current shell’s environment from csh / tcsh at all.

Источник

How to Use the which Command in Linux

The which command allows users to search the list of paths in the $PATH environment variable and outputs the full path of the command specified as an argument. The command works by locating the executable file matching the given command.

Читайте также:  Linux grep find text in all files

In this tutorial, you will learn to use the which command.

How to use the which command in Linux, with examples.

Linux which Command Syntax and Options

The syntax for the which command is:

The [argument] variable specifies the command or commands you want to find.

For example, the following command outputs the location of the cat command:

Find the location of the cat command executable file using which.

The which command has only one option, -a . It is optional and used to print all the matches it finds.

The command searches for matches from left to right. If there are multiple matches found in the directories listed in $PATH , which prints only the first one. The -a option instructs which to print all the matches.

Important: On many Linux distributions, which excludes the shell built-in commands and does not output their location.

List all instances of a command using which.

Having multiple matches sometimes means one match is a symlink to the other. However, it is possible to have two versions of the same command in different locations or two different commands using the same name.

Note: Unlike many other commands, which has no —help option. To see the command description and help, run man which .

Exit Status

The which command returns one of the following values that indicate its exit status:

  • 0 . All arguments were found and executable.
  • 1 . One or more arguments don’t exist or aren’t executable.
  • 2 . An invalid option has been specified.

Linux which Command Examples

The following examples showcase how the which command works and how to use the available option.

1. Display the Path of Any Executable File

To display the path of any command, pass the command name as an argument after which .

Find the tr command executable with which.

The output shows the path to the tr command executable file, located in /usr/bin/tr.

2. Display Multiple Paths of Executable Files

which accepts multiple arguments and outputs the path to each one in the specified order.

Find the paths to multiple commands using which.

The command works through the supplied list and outputs the results for the nc command, mount command, and sort command, separating each result with a newline character.

3. List All Instances

which only shows the first match it finds in the $PATH variable directory list. Use the -a option to show every match for the specified command.

For example, searching for instances of the less command outputs two results when using the -a option:

Display the path to all instances of a command.

Use the ls command to check file details and determine if both versions are executable files. Run:

ls -lh /usr/bin/less ls -lh /bin/less

Check the details of a command

The output shows two identical versions of the same command in two locations, both 176 KB large, and both executable.

Note: The /bin directory contains executables that can be used by the system administrator and any other user, and which are required for emergency system repairs. The /usr/bin directory is the primary directory for executable commands on the system.

Using the -a option lists all the paths containing an instance of the specified program. While multiple versions of the same program can exist on a system, sometimes one of the instances is only a symbolic link and not a binary file.

Читайте также:  Dell vostro with linux

For example, running the following command outputs two instances of the atq command:

Find all instances of the atq command.

Again, use the ls command to check the details for both files. Run:

ls -lh /usr/bin/atq ls -lh /bin/atq

Finding symbolic links using the ls command.

The output shows that both files are symbolic links ( -> ) only 2 bytes large and pointing to the at command.

5. Exclude Shell Built-ins

As previously mentioned, the which command excludes shell built-ins from its output.

For example, asking for the location of the read and man commands only outputs the location for the man command executable file, as read is a bash shell command.

The which command excluding a shell built-in from its output.

This tutorial showed how to use the which command in Linux to find the path to a command’s executable binary. See and download our Linux commands cheat sheet for other essential Linux commands and examples of using them.

Источник

What is Windows’ equivalent of the «which» command in Unix? Is there an equivalent PowerShell command?

In Linux, we have the «which» command to find out the path of an executable.
What is its Windows equivalent? Is there any PowerShell command for doing that?

6 Answers 6

Newer versions of Windows (I think Windows 2003 and up) have the where command:

C:\>where ping C:\Windows\System32\PING.EXE 

And for PowerShell, explicitly add the .exe suffix:

PS C:\>where.exe ping C:\Windows\System32\PING.EXE 

where /r c:\ fileName adding the /r c:\ allowed me to perform a recursive search starting at the root of the C drive using Windows 7 Professional it seems to not be in access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/…

in Powershell you should say where.exe ping because where is by default aliased to Where-Object cmdlet which is completely different story

Yes, Get-Command will find all commands including executables:

If you want to limit the commands to just executables:

PS\> Get-Command -CommandType Application 

Will find all exes in your path. There is an alias for interactive use:

PS\> gcm net* -CommandType Application 

To get the path of an executable, you can use the Path property of the returned object. For example:

PS\> (Get-Command notepad.exe).Path 

For more info, run man Get-Command -full .

where.exe explicitly rather than where works for me in PowerShell:

PS C:\Users\birdc> where ping PS C:\Users\birdc> where.exe ping C:\Windows\System32\PING.EXE 

In addition to user10404, the help command will work on aliases, so you can use the same command name (gcm) for help and interactive use:

help gcm -Parameter * # or man gcm -Par * 

If you want to make it short, create a one line which.cmd file with the content

This will search the first parameter (%1) fed to the script and display the full path of found file. Good place to put this script in windows 10 is %LOCALAPPDATA%\Microsoft\WindowsApps\which.cmd

And you get your which command in path.

c:\>which cmd.exe c:\>echo C:\Windows\System32\cmd.exe C:\Windows\System32\cmd.exe 

Источник

Оцените статью
Adblock
detector