10 bad UNIX or Linux command line usage patterns to avoid

Michael Stutz is author of The Linux Cookbook, has published excellent set of 10 bad UNIX usage patterns. These are applies to Linux, OS X shell as well 🙂

ADVERTISEMENTS

You can adopt 10 good habits that improve your UNIX / Linux command line efficiency — and break away from bad usage patterns in the process. This article takes you step-by-step through several good, but too often neglected, techniques for command-line operations. Learn about common errors and how to overcome them, so you can learn exactly why these UNIX habits are worth picking up.

Adopt 10 good habits

Ten good habits to adopt are:

1. Make directory trees in a single swipe.
2. Change the path; do not move the archive.
3. Combine your commands with control operators.
4. Quote variables with caution.
5. Use escape sequences to manage long input.
6. Group your commands together in a list.
7. Use xargs outside of find.
8. Know when grep should do the counting — and when it should step aside.
9. Match certain fields in output, not just lines.
10. Stop piping cats.

Learn 10 good UNIX usage habits

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
9 comments… add one
  • Doug Keester Dec 13, 2006 @ 18:01

    Overall a decent article.

    However, there is no need to use ‘xargs’ with ‘find’ since ‘find’ has the -exec flag which does the same thing and doesn’t require a pipe.

  • 🐧 nixCraft Dec 13, 2006 @ 18:46

    Doug,

    I must agree with. It always good idea to use KISS philosophy.

    Appreciate your post.

  • Jonathan Dec 14, 2006 @ 3:21

    Yeah, but it will be a cold day in hell before I remember the bizarre -exec syntax without trying it a dozen times or more! “Now where does {} go? And what about that semi-colon? Or is it a colon?” 🙂

  • Paul McNamara Dec 18, 2006 @ 18:23

    Not being fluent in UNIX myself (not speaking a word, actually), I had a little fun with Stutz’s list, in particular, No. 10: “stop piping cats.”
    http://www.networkworld.com/community/?q=node/9764

  • 🐧 nixCraft Dec 18, 2006 @ 23:10

    LOL @ Paul

    Thanks for sharing with us.

  • pip Feb 12, 2007 @ 16:19

    Hey why stop piping cat’s? What’s bad in doing so? redirect the stdinput?

  • pip Feb 12, 2007 @ 16:21

    sorry forgot to tick the notify box… this should solve it… cheers

  • Christopher Cashell May 11, 2008 @ 6:22

    I know this was posted a long time ago, but in case someone else comes across it, a note in response to:

    However, there is no need to use ‘xargs’ with ‘find’ since ‘find’ has the -exec flag which does the same thing and doesn’t require a pipe.

    That’s not actually true. In many simple cases, you can use -exec. However, depending on the number of items returned by find, and the action you’re taking, it can be a *lot* less efficient using -exec in comparison to xargs.

    For example, say you are building a list of files to delete with find, and it is going to return 100k files. If you use find fpo/ -exec rm -rf '{}' ;, then you will be exec()’ing rm 100k times. If you use find foo/ -print | xargs rm -rf, then xargs will build the command argument list for rm to an appropriate length. For the sake of argument, say it runs rm with 1000 entries each time. That means you’re now only exec()’ing rm 100 times, instead of 100k times. When you’re dealing with large numbers like this, the time and CPU cycles to exec() your command that many times can have a very real performance impact.

  • Kevin Jun 6, 2008 @ 0:04

    -exec starts a separate shell for each command.
    xargs does it in one process.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.