您的位置:首页 > 编程语言 > Go语言

UNIX tips: Learn 10 good UNIX usage habits

2010-04-01 15:31 309 查看
1.Make directory trees in a single swipe

Listing 1 illustrates one of the most common bad UNIX habits around: defining directory trees one at a time.

Listing 1. Example of bad habit #1: Defining directory trees individually

~ $ 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 so much quicker to use the
-p
option to
mkdir
and make all parent directories along with theirchildren in a single command. But even administrators who know about this option are still caught stepping through the subdirectories as they make them on thecommand line. It is worth your time to conscientiously pick up the good habit:

Listing 2. Example of good habit #1: Defining directory trees with one command

~ $ mkdir -p tmp/a/b/c

You can use this option to make entire complex directory trees, which are great to use inside scripts; not just simple hierarchies. For example:

Listing 3. Another example of good habit #1: Defining complex directory trees with one command

~ $ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}

In the past, the only excuse to define directories individually was that your
mkdir
implementation did not support this option, but this is no longer true on most systems. IBM AIX®
mkdir
, GNU
mkdir
, and others that conform to the Single UNIX Specification now have this option.

For the few systems that still lack the capability, use the
mkdirhier
script (see Resources), which is a wrapper for
mkdir
that does the same function:

~ $ mkdirhier project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}

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

more specifically information please link there:http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html#six


Conclusion: Embrace good habits

It is good to examine your command-line habits for any bad usage patterns. Bad habits slow you down and often lead to unexpected errors. This article presents 10 new habits that can help you break away from many of the most common usage errors. Picking up these good habits is a positive step toward sharpening your UNIX command-line skills.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: