Linux Cheat Sheet for essential Commands
Linux is a versatile operating system, packed with powerful tools and commands to streamline tasks, troubleshoot issues, and manage systems efficiently.
Whether you’re a seasoned sysadmin or a curious beginner, having a handy cheatsheet can save time and make your Linux experience more productive.
In this post, I will explore interesting and useful Linux commands and update this post regularly.
which
The which command in Linux is a simple utility that shows the location of an executable file for a given command in your system’s PATH. It helps users identify which version of a command or program will be executed when they run it in the shell.
which searches through the directories listed in the PATH environment variable and returns the full path of the first matching executable it finds.
# which zypper

Check the location of multiple commands:
# which zypper vi

-a (all matches): Lists all matching executables in the PATH, not just the first one.

which only works for executables in the PATH. If a command is not in the PATH or is an alias, function, or shell built-in, which will not detect it.
whereis
The whereis command in Linux is a utility used to locate the binary, source code, and manual pages for a given command. Unlike the which command previously, which only looks for executables in your PATH, whereis performs a broader search in predefined directories to provide more comprehensive information.
# whereis zypper

Different output when using the which vs. whereis command for the zypper tool.
# whereis -a zypper

Search for a command’s binary only:
# whereis -b zypper

Find only the manual pages:
# whereis -m zypper

The whereis command searches predefined directories for:
- Binaries in directories like /bin, /usr/bin, and others.
- Source files in standard source directories.
- Manual pages in directories like /usr/share/man.
whereis does not search the entire file system (e.g., for custom installations) and is not aware of shell aliases or functions.
history
The history command in Linux is a powerful utility that allows users to view and manage the command history of their terminal sessions.
The following command lists the last 15 entries.
# history 15

Every command executed in the terminal is stored in a history file, and the history command provides a way to access this list. Users can recall previously used commands, re-execute them, or even search through past commands for efficiency. This command is especially useful for users who frequently work in the terminal, as it saves time and reduces the need to manually retype lengthy or complex commands.
More about the history command in my following post.
Grep multiple Patterns
You can use the -e option to specify multiple patterns.
command | grep -e "pattern1" -e "pattern2"
Or by using the \| operator within a single pattern to search for multiple patterns.
# command | grep "pattern1\|pattern2"
The -E option enables extended regular expressions, which allows you to use | for OR operations without escaping it.
# command | -E "pattern1|pattern2"
You can search for multiple patterns across multiple files by specifying the filenames or using wildcards.
# grep -e "pattern1" -e "pattern2" file1.txt file2.txt This will search for error or warning in all .log files in the current directory. # grep -e "error" -e "warning" *.log
If your grep supports -P (Perl regex), you can use lookaheads to output only when two patterns exist.
command | grep -P '^(?=.*pattern1)(?=.*pattern2)'
Limit the output to a specific amount of lines. This will show the first 15 matching rows.
command | grep -e "pattern1" -e "pattern2" | head -n 15
Limit the output to a specific amount of lines. This will show the last 5 matching rows.
command | grep -e "pattern1" -e "pattern2" | tail -n 5
Links
whereis(1) – Linux man page
https://linux.die.net/man/1/whereiswhich(1) – Linux man page
https://linux.die.net/man/1/whichhistory(3) — Linux manual page
https://www.man7.org/linux/man-pages/man3/history.3.html