10 Essential Unix Shell Hacks to Boost Your Productivity
This guide presents ten practical Unix shell techniques—including file‑name completion, history expansion, directory stack navigation, large‑file discovery, temporary file creation, curl usage, regular‑expression tricks, user identification, and awk data processing—to help developers work faster and more efficiently from the command line.
1. File name completion
File name completion lets you avoid typing long paths manually. The method varies by shell, so first determine which shell you are using.
Determine your shell
echo $0 ps -p $$
C Shell enables completion via the filec variable (set with set filec) and the Esc key.
Bash provides Tab‑based completion by default; pressing Tab once shows the longest common prefix, pressing twice lists all matches.
Korn Shell uses the EDITOR variable: vi → Esc then \, emacs → double Esc.
2. History expansion
Reuse the previous command’s arguments with !$. For example, after searching a file with grep pickles this‑is‑a‑long‑lunch‑menu‑file.txt, you can edit the same file directly:
vi !$
3. Reusing earlier parameters
The !: operator selects specific arguments from the previous command. !:1 returns the first argument, which can be combined with !$ to manipulate files.
mv kxp12.c file_system_access.c ln -s !$ !:1
4. Managing directory navigation with pushd/popd
pushdadds the current directory to a stack and switches to a new one; popd returns to the previous directory. dirs displays the stack.
pushd . pushd /etc pushd /var pushd /usr/local/bin dirs popd
You can rotate the stack with pushd +n or pushd -n.
5. Finding large files
Use df to check filesystem usage, then locate files larger than a threshold with find and the -size option (size is in kilobytes).
find / -size +10000k -xdev -exec ls -lh {} \;
6. Creating temporary files without an editor
Redirect standard input to a file using cat > filename. Finish input with Ctrl‑D. To append instead of overwrite, use >>.
cat > my_temp_file.txt cat >> my_temp_file.txt
7. Using curl for data transfer
curlretrieves data over many protocols (HTTP, FTP, etc.). Use -o to save the output to a file.
curl -o archive.tar http://www.somesite.com/archive.tar
8. Effective regular‑expression usage
Regular expressions (regex) define patterns for matching text. Common constructs include ^ (start of line), $ (end of line), character classes [...], quantifiers *, and ranges {x,y}. They are frequently used with grep:
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
9. Determining the current user
Run whoami to display the effective username. It can be used in scripts to prevent execution as root:
if [ $(whoami) = "root" ]; then echo "You cannot run this script as root."; exit 1; fi
10. Processing data with awk
awkis a powerful text‑processing tool. Examples:
Print line length: awk '{ i = length($0); print i }' file Find position of a substring: awk '{ i = index($0,"ing"); print i }' file Tokenize a line:
awk 'BEGIN { i = 1 } { n = split($0,a," "); while (i <= n) {print a[i]; i++;} }' fileSummarize CSV data: awk -F, '{print $1,$2+$3+$4}' sales Source: IBM developerWorks (https://www.ibm.com/developerworks/cn/aix/library/au-unixtips/).
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.
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.
