Essential Linux Command Cheat Sheet: Master ls, cd, grep, and More
This guide provides concise explanations and practical examples for the most commonly used Linux shell commands—including ls, cd, pwd, mkdir, cp, mv, rm, du, df, cat, echo, less, head, tail, wc, grep, man, logout, pipes, xargs, basename, dirname, and set—helping beginners quickly become proficient with the command line.
ls – List Directory Contents
The ls command lists files and directories. Frequently used options are: -a: show hidden files (those starting with a dot). -l: display detailed information such as permissions, size, and timestamps. -F: append a symbol to each entry to indicate its type (e.g., * for executables, / for directories).
cd – Change Directory
Use cd <dir> to move into a directory. Special forms: cd or cd ~: go to the current user’s home directory. cd -: return to the previous directory. cd ..: move up one level.
pwd – Print Working Directory
Displays the absolute path of the current directory.
mkdir / rmdir – Create and Remove Directories
mkdir <dir>creates a directory; rmdir <dir> removes an empty directory. Both support the -p option to create or remove parent directories recursively.
Examples:
mkdir -p 1/2/3
rmdir -p 1/2/3cp – Copy Files and Directories
Basic forms: cp 1.txt ../test2: copy a file to another directory. cp 1.txt 2.txt: copy and rename. cp -r a b: copy directory a recursively to b.
mv – Move or Rename Files
Examples: mv 1.txt ../test1: move a file. mv 1.txt 2.txt: rename a file. mv 1.txt ../test1/2.txt: move and rename simultaneously.
rm – Remove Files and Directories
Common options: -i: prompt before each deletion. -r: remove directories recursively. -f: force deletion without prompts.
du / df – Disk Usage
dushows the space used by files or directories; df reports free space on mounted filesystems.
Examples: du -hs ./*: human‑readable size of each item in the current directory.
cat – Concatenate and Display Files
Displays file contents or concatenates multiple files. Redirection operators can be used, e.g., cat file1 file2 > file3 to combine files.
echo – Print Text
Outputs strings to standard output. Syntax: echo [-ne] [string]. Options: -n: omit trailing newline. -e: enable backslash‑escaped characters (e.g., \n for newline).
Examples:
echo "123" "456" # prints: 123 456
echo -e "123
456" # prints on two linesmore / less – Paginated Viewing
Both allow scrolling through long files. more pauses at each screenful; less supports forward and backward navigation with keys such as Space, b, g, G, and search with /pattern.
head / tail – Show File Beginnings or Ends
head -n 10 fileshows the first 10 lines; tail -n 10 file shows the last 10 lines. tail -f file follows a file in real time.
wc – Word Count
Counts lines, words, and bytes. Options: -l: lines -w: words -c: bytes
grep – Search Text with Patterns
Searches for lines matching a regular expression. Returns 0 on success, 1 on no match, 2 if a file is missing, making it useful in scripts.
Examples:
ls -l | grep '^a' # lines starting with 'a'
grep 'test' d* # lines containing 'test' in files starting with 'd'
grep -E 'w(es)t.*/1' aa # using extended regexman – Manual Pages
Displays detailed documentation for commands, e.g., man ls. Most commands also support --help.
logout – End Session
Logs out of the current shell, the counterpart of login.
Pipes and xargs – Combining Commands
The pipe symbol | feeds the output of one command as input to the next. xargs builds command lines from standard input, useful when a command expects arguments rather than piped data.
Examples:
find / -name "core" -print | xargs echo "" > /tmp/core.log
ls | xargs rmbasename / dirname – Path Manipulation
basename /path/file.txtreturns file.txt; dirname /path/file.txt returns /path.
set – Show Shell Variables
Running set without arguments lists all shell variables and functions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
