الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

which

Show the full path of the program that runs for a command name.

المستوى
لينكس اليومي
المجموعة
الطرفية والمساعدة
يحتاج root
لا
الحزمة
which

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

which searches the folders in your $PATH, from left to right, and prints the first file with that name. It answers "where is this program on the disk?".

On RHEL, which for Bash users is an alias that also shows aliases and functions (alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot').

To know which package a program comes from, use rpm -qf $(which NAME).

طريقة الكتابة

which [OPTIONS] PROGRAM [PROGRAM ...]

الخيارات 12

الأكثر استخدامًا

-a, --all
Print all matches in $PATH, not only the first one.
-i, --read-alias
Read aliases from standard input and report them too.
--skip-alias
Ignore --read-alias (use it to override the RHEL alias).
--read-functions
Read shell functions from standard input and report them too.
--skip-functions
Ignore --read-functions.
--skip-dot
Skip folders in $PATH that start with a dot.
--skip-tilde
Skip folders in $PATH that start with ~ and home folders.
--show-dot
If a $PATH folder starts with a dot, print ./PROGRAM.
--show-tilde
Print ~ instead of the full home folder path.
--tty-only
Stop reading options when the output is not a terminal (used by the RHEL alias).
-v, -V, --version
Show the version.
--help
Show help.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Find where a program is

    which passwd
    /usr/bin/passwd

    The program file that runs when you type passwd.

  2. 2See all copies of a program in $PATH

    which -a python3
    /usr/bin/python3
    /usr/local/bin/python3

    The first one wins. Two copies often explain "wrong version" problems.

  3. 3Find the package of a program

    rpm -qf $(which ss)
    iproute-6.x.x.el10.x86_64

    Now you know which package to update or reinstall.

  4. 4Check if a tool exists in a script

    which ansible >/dev/null 2>&1 && echo "ansible installed"

    The exit code is 0 when found, 1 when not found.

  5. 5Show the alias too (RHEL default)

    which ll
    alias ll='ls -l --color=auto'
    	/usr/bin/ls

    Because of the RHEL alias, which also shows aliases in an interactive shell.

  6. 6Use the plain program without the alias

    /usr/bin/which --skip-alias ll

    In a script the alias does not exist; this shows what a script would find.

  7. 7Check the path of a privileged program in a review

    which -a sudo

    Only /usr/bin/sudo should appear. Another sudo earlier in $PATH is a red flag.

  8. 8Find which shell a user would get

    which bash zsh fish
    /usr/bin/bash

    Only installed shells are printed; the others give an error message.

  9. 9Get the path for a cron job or a systemd unit

    which podman
    /usr/bin/podman

    cron and systemd use a short $PATH. Write the full path in ExecStart= and crontab lines.

  10. 10Check many tools at once before a deployment

    for t in git curl jq rsync; do which $t >/dev/null || echo "missing: $t"; done

    Prints only the tools that are not installed.

  11. 11Compare with type

    which cd; type cd

    which finds no file for cd (it is a built-in); type explains what it is.

انتبه

  • which only searches files in $PATH. It does not know built-ins like cd (use type).
  • sudo which NAME can give another answer, because root has a different $PATH.

نصائح للحفظ

  • which = where is the file? type = what will run? rpm -qf = which package?

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man which