Take 10 good habits that will improve your unix® command line efficiency-and get rid of bad usage patterns in the process. This article guides you step-by-step through several techniques for command-line operations, which are very good, but are often ignored. Learn about common mistakes and how to overcome them so you can see exactly why you deserve these UNIX habits.
Introduction
When you use a system frequently, you tend to get stuck in some sort of fixed usage pattern. Sometimes, you don't get into the habit of doing things in the best way possible. Sometimes, your bad habits can even lead to confusion. One of the best ways to correct such shortcomings is to consciously adopt a good habit of resisting these bad habits. This article presents 10 Unix command-line habits that are worth taking--a good habit to help you overcome many common usage quirks and improve command-line productivity in the process. These 10 good habits are listed below, followed by a more detailed description.
Use 10 good habits
The ten good habits to adopt are:
Create a directory tree in a single command.
Change the path, and do not move the archive.
Use the command and control operators in combination.
Reference variables carefully.
Use an escape sequence to manage longer input.
Groups the commands in the list.
Use Xargs outside of find.
Learn when grep should perform the count-when it should be bypassed.
Matches some of the fields in the output, not just the rows.
Stop using the pipe for cat.
Creating a directory tree in a single command
Listing 1 illustrates one of the most common UNIX bad habits: Defining a directory tree at a time.
Listing 1. Bad habits. 1 Example: Individually define each directory tree~ $ mkdir tmp
~ $ cd tmp
~/tmp $ mkdir a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $
It is much easier to use the mkdir-P option and to create all the parent directories and their subdirectories in a single command. But even for administrators who know this option, they are still bound to progressively create each level of subdirectories when they create subdirectories on the command line. It is worthwhile to take the time to consciously develop this good habit:
Listing 2. Good habits. 1 Example: Use a command to define a directory tree
~ $ mkdir -p tmp/a/b/c
You can use this option to create an entire complex directory tree (which is ideal for use in scripts), rather than just creating a simple hierarchy. For example:
Listing 3. Good habit. Another example of 1: Using a command to define a complex directory tree
~ $ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
In the past, the only excuse for defining a directory individually is that your mkdir implementation does not support this option, but it is no longer the case on most systems. IBM, aix®, mkdir, GNU mkdir, and other systems that comply with the Single UNIX specification (the one UNIX specification) now have this option.