10 Essential Unix Command-Line Tricks to Boost Your Productivity
This article presents ten practical Unix command-line habits—including file name completion, history expansion, directory stack navigation, large‑file searching, temporary file creation, curl usage, regular‑expression tips, user identification, and awk data processing—each illustrated with clear examples and commands to help users work faster and more efficiently.
File name completion
Identify the current shell with echo $0 or ps -p $$. In C shell enable completion by setting filec. Bash provides Tab‑based completion by default. In Korn shell the completion behavior depends on the EDITOR variable: vi mode uses Esc \, emacs mode uses Esc Esc.
History expansion
Reuse the last argument of the previous command with !$. Example: after grep pickles this‑is‑a‑long‑lunch‑menu‑file.txt, edit the same file with vi !$ without retyping the filename.
Reusing previous arguments
!$returns the last argument, while !:1 returns the first argument of the previous command. Combining them allows actions such as renaming a file and creating a symbolic link to the original name:
mv kxp12.c file_system_access.c
ln -s !$ !:1 # creates link file_system_access.c -> kxp12.cDirectory stack with pushd and popd
pushdchanges the current directory and pushes it onto a stack; popd removes the top entry and returns to the previous location. dirs displays the stack. Example sequence:
pushd .
pushd /etc
pushd /var
pushd /usr/local/bin
dirs
popd
popd
# rotate stack
pushd +1 # move second entry to top
pushd -1 # move top entry downFinding large files
Use df to view filesystem usage. To locate files larger than 10 MB:
find / -size +10000k -xdev -exec ls -lh {} \;Creating temporary files without an editor
Redirect standard input with cat:
cat > my_temp_file.txt
This is my temp file text
^D
cat my_temp_file.txt # displays the contentAppend instead of overwrite using >>:
cat >> my_temp_file.txt
More text
^DUsing curl for web requests
Download a file via HTTP/HTTPS:
curl -o archive.tar http://www.somesite.com/archive.tarSee man curl for additional options.
Effective use of regular expressions
Common metacharacters:
^ – 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
Examples with grep:
grep '^From: ' /usr/mail/$USER
grep '[0-9]{3}-[0-9]{4}' file # phone numbersDetermining the current user
Run whoami to display the effective username. In scripts, prevent execution as root:
if [ "$(whoami)" = "root" ]; then
echo "You cannot run this script as root."
exit 1
fiProcessing data with awk
Print line length: awk '{ print length($0) }' file Find position of a substring (e.g., "ing"): awk '{ print index($0,"ing") }' file Tokenize a line into words:
awk '{ n=split($0,a," "); for(i=1;i<=n;i++) print a[i] }' fileSummarize CSV data (comma‑separated values): awk -F, '{ print $1, $2+$3+$4 }' sales The command prints each salesperson’s name followed by the total of the numeric fields.
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.
