Fundamentals 13 min read

Boost Your Unix Productivity: 10 Essential Shell Tricks You Need

This guide walks you through practical Unix command‑line techniques—including filename autocompletion across shells, history expansion shortcuts, pushd/popd directory stack navigation, finding large files, creating temporary files with cat, using curl for downloads, mastering regular expressions, identifying the current user, and powerful awk data processing—each illustrated with clear examples and ready‑to‑run commands.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Boost Your Unix Productivity: 10 Essential Shell Tricks You Need

1. Filename Autocompletion

Filename autocompletion saves you from typing long, error‑prone names. Identify your shell first (e.g., echo $0 or ps -p $$).

C Shell : Enable with set filec. Press Esc after typing part of a name to complete it.

Bash : Autocompletion is on by default and uses the Tab key. Press Tab once to complete a unique name; press twice to list all matches.

Korn Shell : Completion depends on the EDITOR variable. If EDITOR=vi, press Esc followed by \. If EDITOR=emacs, press Esc twice.

2. History Expansion

The !$ operator reuses the last argument of the previous command, which is handy for repeating long filenames.

$ grep pickles this-is-a-long-lunch-menu-file.txt
$ vi !$

3. Reusing Previous Arguments

Combine !$ with !:1 to select specific arguments from the prior command. Example:

$ mv kxp12.c file_system_access.c
$ ln -s !$ !:1

4. Directory Navigation with pushd and popd

pushd

adds the current directory to a stack and changes to a new one; popd returns to the previous directory. Use dirs to view the stack.

$ pushd .
$ pushd /etc
$ pushd /var
$ pushd /usr/local/bin
$ dirs
$ popd
$ popd
$ popd
$ popd

You can rotate the stack with pushd +n or pushd -n.

5. Finding Large Files

Use df to see disk usage per volume. $ df Search for files larger than 10 MB with find:

$ find / -size +10000k -xdev -exec ls -lh {} \;

6. Creating Temporary Files without an Editor

Redirect output from cat to create a file:

$ cat > my_temp_file.txt
This is my temp file text
^D
$ cat my_temp_file.txt

Append to an existing file with >>:

$ cat >> my_temp_file.txt
More text
^D
$ cat my_temp_file.txt

7. Using curl

curl

retrieves data via many protocols. Download a file with the -o option:

$ curl -o archive.tar http://www.somesite.com/archive.tar

8. Effective Use of Regular Expressions

Regular expressions (regex) define pattern strings for matching text. Common metacharacters include:

^ – start of line

$ – end of line

\ – escape next character

[] – character class

[^] – negated class

. – any single character (except newline)

* – zero or more of the preceding element

{x,y} – between x and y repetitions

{x} – exactly x repetitions

{x,} – x or more repetitions

Typical grep examples:

$ grep '^From: ' /usr/mail/$USER
$ grep '[a-zA-Z]' search-file.txt
$ grep '[^a-zA-Z0-9]' search-file.txt
$ grep '[0-9]{3}-[0-9]{4}' search-file.txt
$ grep '^.$' search-file.txt
$ grep '^.[a-z][a-z]' search-file.txt

9. Determining the Current User

Run whoami to print the current username.

$ whoami
John

Example script to prevent running as root:

if [ $(whoami) = "root" ]
then
  echo "You cannot run this script as root."
  exit 1
fi

10. Data Processing with awk

awk

excels at line‑oriented text processing.

Length of each line: $ awk '{ i = length($0); print i }' text Find position of "ing": $ awk '{ i = index($0,"ing"); print i }' text Tokenize a line into words:

$ awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) { print a[i]; i++; } }' text

Summarize CSV data (sales example): $ awk -F, '{print $1,$2+$3+$4}' sales Result:

Gene 42
Dawn 50
Renee 46
David 46
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

UnixScripting
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.