{"id":17379,"date":"2026-02-10T14:37:47","date_gmt":"2026-02-10T09:07:47","guid":{"rendered":"https:\/\/www.youstable.com\/blog\/?p=17379"},"modified":"2026-02-10T14:37:49","modified_gmt":"2026-02-10T09:07:49","slug":"which-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.youstable.com\/blog\/which-command-in-linux","title":{"rendered":"Which Command in Linux Explained With Example in 2026"},"content":{"rendered":"\n<p><strong>The which command in Linux<\/strong> prints the full path of the executable that would run when you type a command, by searching the directories listed in your $PATH. Use which &lt;name&gt; for the first match or which -a &lt;name&gt; to show all matches. <\/p>\n\n\n\n<p>It\u2019s ideal for verifying versions, resolving path conflicts, and debugging \u201ccommand not found\u201d errors. If you\u2019re new to Linux or managing servers, mastering the which command in Linux will save time and prevent misconfigurations. <\/p>\n\n\n\n<p>In this guide, you\u2019ll learn what which does, how it differs from alternatives like command -v and whereis, and see practical examples from real world server administration especially useful for web hosting environments.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"what-is-the-which-command-in-linux\">What is the which Command in Linux?<\/h2>\n\n\n\n<p><strong>The which command tells you<\/strong> the exact path of the executable that the shell would run for a given command name. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2026\/01\/What-is-the-which-Command-in-Linux.jpg\" alt=\"Which Command in Linux\" class=\"wp-image-17440\" srcset=\"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2026\/01\/What-is-the-which-Command-in-Linux.jpg 1280w, https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2026\/01\/What-is-the-which-Command-in-Linux-150x84.jpg 150w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/figure>\n\n\n\n<p><strong>It searches the directories in<\/strong> the $PATH environment variable in order and returns the first match (or all matches with -a). This is essential for confirming which binary actually executes when multiple versions are installed.<\/p>\n\n\n\n<p>In hosting and <strong>DevOps workflows<\/strong>, which helps you confirm the PHP, Python, Node.js, or Git version you\u2019re invoking critical when paths differ per user, virtual environment, or container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"syntax-and-common-options\">Syntax and Common Options<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>which &#91;options] name &#91;name ...]<\/code><\/pre>\n\n\n\n<p>Frequently used options (GNU which; options may vary slightly across distros):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-a, &#8211;all: Show all matching executables in $PATH.<\/li>\n\n\n\n<li>&#8211;skip-alias \/ &#8211;skip-functions: Ignore shell aliases or functions (when supported).<\/li>\n\n\n\n<li>&#8211;read-alias \/ &#8211;read-functions: Print definitions of aliases\/functions (GNU which feature).<\/li>\n<\/ul>\n\n\n\n<p>Shell built ins, aliases, and functions aren\u2019t always handled consistently by which across platforms. For shell aware checks, command -v or type is often more reliable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"practical-examples-of-which\">Practical Examples of which<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"1-find-the-path-of-a-command\">1. Find the path of a command<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>which ls\n# \/usr\/bin\/ls\n\nwhich nginx\n# \/usr\/sbin\/nginx (example)<\/code><\/pre>\n\n\n\n<p>This shows the exact binary that will run when you type the command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"2-show-all-matches-along-path\">2. Show all matches along $PATH<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>which -a python\n# \/usr\/bin\/python\n# \/usr\/local\/bin\/python (example)\n\nwhich -a php\n# \/usr\/bin\/php\n# \/usr\/local\/bin\/php<\/code><\/pre>\n\n\n\n<p>If multiple versions are installed, -a reveals the full resolution order. This is vital when the wrong version appears first in $PATH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"3-check-multiple-commands-at-once\">3. Check multiple commands at once<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>which git curl unzip\n# \/usr\/bin\/git\n# \/usr\/bin\/curl\n# \/usr\/bin\/unzip<\/code><\/pre>\n\n\n\n<p>which returns a nonzero exit code if one or more commands aren\u2019t found (useful in scripts).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"4-aliases-and-functions-caveat\">4. Aliases and functions caveat<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># You may have an alias:\nalias ll='ls -alF'\n\nwhich ll\n# Might print nothing or the ls path; behavior varies by system.\n\n# Better:\ntype -a ll\n# ll is aliased to `ls -alF`<\/code><\/pre>\n\n\n\n<p>Because which is not a shell built in, it may not fully understand aliases or functions. Prefer type or command -v for shell aware results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"5-which-with-sudo-and-path-differences\">5. which with sudo and PATH differences<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>which nginx\n# \/usr\/sbin\/nginx\n\nsudo which nginx\n# which: no nginx in (...root's PATH...)\n# or a different path than your user<\/code><\/pre>\n\n\n\n<p>sudo may use a different PATH for root, so sudo which can yield different or empty results. If you only want to run which with your current PATH but elevated privileges, do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo env \"PATH=$PATH\" which nginx<\/code><\/pre>\n\n\n\n<p><strong>Security tip:<\/strong> Be cautious when modifying PATH under sudo. Use explicit paths in automation where possible.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"which-vs-command-v-vs-type-vs-whereis\">which vs. command -v vs. type vs. whereis<\/h2>\n\n\n\n<p>Several commands help discover <strong>\u201cwhat will run,\u201d<\/strong> but they serve different purposes. Here\u2019s when to use each:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>which:<\/strong> Finds the first (or all) executables in $PATH. Great for quick path checks and resolving version conflicts.<\/li>\n\n\n\n<li><strong>command -v: <\/strong>POSIX-compliant; prints how the shell resolves a name (covers built ins, functions, aliases, and executables). Prefer this in portable scripts.<\/li>\n\n\n\n<li><strong>type -a:<\/strong> Bash\/Zsh built in that explains whether a name is an alias, function, builtin, or file, and shows all locations.<\/li>\n\n\n\n<li><strong>whereis:<\/strong> Searches standard locations for binaries, source, and man pages. Not tied to $PATH; broader and less precise for execution order.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Portable and reliable in scripts:\ncommand -v nginx &gt;\/dev\/null 2&gt;&amp;1 &amp;&amp; echo \"nginx is available\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"understanding-path-and-execution-order\">Understanding $PATH and Execution Order<\/h2>\n\n\n\n<p><strong>$PATH is a colon separated list <\/strong>of directories the shell searches to find executables. which uses this order to determine the command that will run first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo $PATH\n# \/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin (example)<\/code><\/pre>\n\n\n\n<p>To prioritize a custom directory useful when you install a newer PHP or Node.js\u2014prepend it to PATH:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Temporary (current shell session)\nexport PATH=\"\/opt\/php8.2\/bin:$PATH\"\n\n# Persist for a user (Bash)\necho 'export PATH=\"\/opt\/php8.2\/bin:$PATH\"' &gt;&gt; ~\/.bashrc\nsource ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>After changing PATH, verify with which -a to confirm the resolution order is correct.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"exit-codes-and-using-which-in-scripts\">Exit Codes and Using which in Scripts<\/h2>\n\n\n\n<p><strong>GNU which typically returns:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0:<\/strong> All specified commands were found.<\/li>\n\n\n\n<li><strong>1:<\/strong> Some commands were not found.<\/li>\n\n\n\n<li><strong>2:<\/strong> Invalid options or a serious error occurred.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Ensure required tools exist before deployment\nif which git &gt;\/dev\/null 2&gt;&amp;1 &amp;&amp; which rsync &gt;\/dev\/null 2&gt;&amp;1; then\n  echo \"Deps OK\"\nelse\n  echo \"Missing required tools\" &gt;&amp;2\n  exit 1\nfi<\/code><\/pre>\n\n\n\n<p>For POSIX portability, command -v is often recommended:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if command -v php &gt;\/dev\/null 2&gt;&amp;1; then\n  php -v\nelse\n  echo \"PHP not installed or not in PATH\" &gt;&amp;2\nfi<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"troubleshooting-which\">Troubleshooting which<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"command-found-but-not-executable\">Command found but not executable<\/h3>\n\n\n\n<p>If a file exists in PATH but lacks execute permissions for your user, which may not show it. Fix permissions or adjust PATH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"hash-cache-mismatch-bash\">Hash cache mismatch (Bash)<\/h3>\n\n\n\n<p>The shell may cache command locations. If you replace a binary, clear the cache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hash -r<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"different-behavior-with-sudo-or-non-login-shells\">Different behavior with sudo or non login shells<\/h3>\n\n\n\n<p>Login vs non login shells and sudo policies can produce different PATH values. <\/p>\n\n\n\n<p><strong>Compare environments:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf \"User PATH: %s\\n\" \"$PATH\"\nsudo env | grep -E '^PATH='<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"real-world-hosting-use-cases\">Real World Hosting Use Cases<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"pick-the-correct-php-for-cli-and-cron\">Pick the correct PHP for CLI and cron<\/h3>\n\n\n\n<p>Servers often host multiple <a href=\"https:\/\/www.youstable.com\/blog\/how-to-change-php-version\/\">PHP versions<\/a> (e.g., 7.4, 8.1, 8.2). Use which -a php to see available binaries and update PATH or call the absolute path in crontab to avoid running the wrong PHP version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"validate-pip-vs-pip3-during-deployments\">Validate pip vs pip3 during deployments<\/h3>\n\n\n\n<p>In Python environments, pip may point to Python 2 while pip3 targets Python 3. Confirm with which pip and which pip3, or rely on python3 -m pip to ensure the correct interpreter installs dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"confirm-node-and-npm-paths-in-ci-cd\">Confirm Node and npm paths in CI\/CD<\/h3>\n\n\n\n<p>When using Node Version Manager (nvm) or custom installs, check which node and which npm inside build steps to guarantee the expected version is used during asset compilation.<\/p>\n\n\n\n<p>At <strong><a href=\"https:\/\/www.youstable.com\/\">YouStable<\/a><\/strong>, we recommend explicitly setting PATHs in automation and using which -a during build validation to prevent version drift between staging and production. Our managed hosting stacks keep PATH consistent across users to reduce surprises in cron, SSH, and web server contexts.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"security-and-best-practices\">Security and Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Never put <\/strong>the current directory (.) at the start of PATH; it can enable accidental or malicious command execution.<\/li>\n\n\n\n<li><strong>Prefer absolute paths<\/strong> in privileged scripts (e.g., \/usr\/bin\/rsync) to avoid PATH hijacking.<\/li>\n\n\n\n<li><strong>When using sudo<\/strong>, understand what PATH is applied; consult \/etc\/sudoers and secure_path settings.<\/li>\n\n\n\n<li><strong>Use command<\/strong> -v in scripts for portability and clarity when checking command availability.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"faqs\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1768038993487\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"what-does-the-which-command-do-in-linux\">What does the which command do in Linux?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>which searches the directories listed in $PATH and prints the path of the executable that would run for a given command name. Use which -a to list all matches in search order.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768039001435\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"why-does-which-show-nothing-for-my-command\">Why does which show nothing for my command?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Either the command isn\u2019t installed, it\u2019s not in your $PATH, or it\u2019s an alias\/function that which doesn\u2019t recognize. Try command -v or type -a. Also verify permissions and shell type (login vs non-login).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768039008249\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"whats-the-difference-between-which-and-whereis\">What\u2019s the difference between which and whereis?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>which uses $PATH to show what will execute; whereis scans standard locations to find binaries, source, and man pages. whereis is broader but not indicative of execution order.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768039016420\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"is-which-posix-compliant-should-i-use-command-v-instead\">Is which POSIX compliant? Should I use command -v instead?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>which isn\u2019t specified by POSIX and behaves inconsistently across systems. command -v is POSIX-compliant and preferred in portable scripts, especially when you need to detect aliases, functions, and built ins reliably.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768039022992\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-fix-path-so-which-finds-the-right-version\">How do I fix PATH so which finds the right version?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Prepend the desired bin directory to PATH, then verify with which -a. Example: export PATH=&#8221;\/opt\/php8.2\/bin:$PATH&#8221;. Persist changes in your shell profile (e.g., ~\/.bashrc) and reload the shell.<br \/>Mastering the which command in Linux is foundational for clean, predictable server operations. <\/p>\n<p>Use it alongside command -v and type to understand exactly what your shell will execute and keep your environments consistent and secure. If you want a hosting platform where PATH and tooling are configured right out of the box, YouStable\u2019s managed environments are built for reliability and clarity.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The which command in Linux prints the full path of the executable that would run when you type a command, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":18579,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[350],"tags":[],"class_list":["post-17379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledgebase"],"acf":[],"featured_image_src":"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2026\/01\/Which-Command-in-Linux.jpg","author_info":{"display_name":"Sanjeet Chauhan","author_link":"https:\/\/www.youstable.com\/blog\/author\/sanjeet"},"_links":{"self":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/comments?post=17379"}],"version-history":[{"count":10,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17379\/revisions"}],"predecessor-version":[{"id":18581,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17379\/revisions\/18581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media\/18579"}],"wp:attachment":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media?parent=17379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/categories?post=17379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/tags?post=17379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}