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.
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 !$ !:14. Directory Navigation with pushd and popd
pushdadds 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
$ popdYou 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.txtAppend to an existing file with >>:
$ cat >> my_temp_file.txt
More text
^D
$ cat my_temp_file.txt7. Using curl
curlretrieves data via many protocols. Download a file with the -o option:
$ curl -o archive.tar http://www.somesite.com/archive.tar8. 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.txt9. Determining the Current User
Run whoami to print the current username.
$ whoami
JohnExample script to prevent running as root:
if [ $(whoami) = "root" ]
then
echo "You cannot run this script as root."
exit 1
fi10. Data Processing with awk
awkexcels 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++; } }' textSummarize CSV data (sales example): $ awk -F, '{print $1,$2+$3+$4}' sales Result:
Gene 42
Dawn 50
Renee 46
David 46Signed-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.
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.)
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.
