10 Essential UNIX Command-Line Habits to Boost Your Efficiency
This guide presents ten practical UNIX shell habits—ranging from using mkdir -p for directory trees to leveraging &&, ||, and xargs wisely—each illustrated with before‑and‑after command examples that demonstrate how to eliminate common inefficiencies and avoid typical pitfalls.
Introduction
The article introduces ten useful UNIX command‑line habits that help users break bad patterns, improve speed, and reduce errors when working in the shell.
1. Create directory trees in a single command
Bad habit: creating each level of a directory hierarchy with separate mkdir calls.
~ $ mkdir tmp
~ $ cd tmp
~ $ mkdir a
~ $ cd a
~ $ mkdir b
~ $ cd b
~ $ mkdir cGood habit: use the -p option to create the entire tree at once. ~ $ mkdir -p tmp/a/b/c Complex trees can be built with brace expansion:
~ $ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}On systems lacking -p, the mkdirhier script provides equivalent functionality.
~ $ mkdirhier project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}2. Change directory with -C instead of moving archives
Instead of moving a .tar file to the target directory before extraction, use the -C option of tar to specify the extraction directory directly.
~ $ tar xvf -C tmp/a/b/c newarc.tar.gz3. Combine commands with control operators
Use && to run a second command only if the first succeeds, and || to run a second command only if the first fails. ~ $ cd tmp/a/b/c && tar xvf ~/archive.tar Another example using both operators:
~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && tar xvf -C tmp/a/b/c ~/archive.tar4. Quote variables carefully
Always enclose variable expansions in double quotes unless you have a specific reason not to. When concatenating a variable with alphanumeric text, use braces to avoid accidental merging.
~ $ VAR="tmp/*"
~ $ echo "$VAR"tmp/*
~ $ echo "${VAR}a"tmp/*a5. Use escape sequences for long input
Backslashes ( \) let you split long commands over multiple lines while the shell treats them as a single line.
~ $ cd tmp/a/b/c || \>
mkdir -p tmp/a/b/c && \>
tar xvf -C tmp/a/b/c ~/archive.tar6. Group commands in lists
Use a subshell ( ( … )) to run a list of commands in a separate shell, allowing redirection of the whole group.
~ $ ( cd tmp/a/b/c || mkdir -p tmp/a/b/c && \
VAR=$PWD; cd ~; tar xvf -C $VAR archive.tar ) \
| mailx admin -S "Archive contents"Alternatively, use braces ( { …; }) to run a list in the current shell.
~ $ { cp ${VAR}a . && chown -R guest.guest a && \>
tar cvf newarchive.tar a; } | mailx admin -S "New archive"7. Use xargs outside find
Pipe the output of find (or any list) to xargs to build command lines with the listed items as arguments.
~ $ find . -name "*.log" | xargs grep "ERROR" xargscan also reformat whitespace‑separated lists or feed filenames to other tools.
~ $ ls -1 | xargs -I{} echo "File: {}"8. Let grep count with -c when appropriate
Using grep -c is faster than piping grep to wc -l:
~ $ time grep and tmp/a/longfile.txt | wc -l
real 0m0.097s
~ $ time grep -c and tmp/a/longfile.txt
real 0m0.013sFor counting pattern instances, combine grep -o with wc -l:
~ $ grep -o and tmp/a/longfile.txt | wc -l9. Match specific fields with awk instead of grep
When you need to filter on a particular column, awk is more precise than grep.
~ $ ls -l | awk '$6 == "Dec"'10. Avoid piping cat into grep
grepcan read files directly, so using cat adds unnecessary overhead.
~ $ time cat tmp/a/longfile.txt | grep and
real 0m0.015s
~ $ time grep and tmp/a/longfile.txt
real 0m0.010sConclusion
Review your shell habits and replace the listed bad patterns with the recommended practices. Adopting these ten habits will make your UNIX command‑line work faster, safer, and more maintainable.
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.
