10 good habits of using UNIX

Source: Internet
Author: User
Use 10 good habits of UNIX-general Linux technology-Linux technology and application information. For more information, see the following. The information is in the http://www.iteye.com/news/1297

Michael Stutz, author of The Linux Cookbook, has summed up 10 good habits based on his years of UNIX experience. I personally think it is really useful. The following is a summary of this article.



Create a hierarchical Directory: Use the-p option of mkdir, such as mkdir-p tmp/a/B/c.
Unpack to the specified directory: Use the-C option of tar, such as tar xvf newarc.tar.gz-C tmp/a/B/c.
Combined Command: Use;, &, | and other control operators, such as cd tmp/a/B/c & tar xvf ~ /Archive.tar.
Use variables with caution: place the variables in "", as shown in

~ $ Ls tmp/
A B
~ $ VAR = "tmp /*"
~ $ Echo $ VAR
Tmp/a tmp/B
~ $ Echo "$ VAR"
Tmp /*
~ $ Echo $ VARa
~ $ Echo "$ VARa"

~ $ Echo "$ {VAR}"
Tmp/*
~ $ Echo $ {VAR}
Tmp/
~ $


Long command input: broken by \ branch, as shown in figure

~ $ Cd tmp/a/B/c | \
> Mkdir-p tmp/a/B/c &&\
> Tar xvf-C tmp/a/B/c ~ /Archive.tar
Group command: Use () and {} to group commands, as shown in figure

~ $ (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"
Use xargs to filter the output, as shown in figure

~ /Tmp $ ls-l | xargs
-Rw-r -- 7 joe 12043 Jan 27 20:36 december_report1_- rw-r -- 1 \
Root 238 Dec 03 README drwxr-xr-x 38 joe 354082 Nov 02 \
A-rw-r -- 3 joe 5096 Dec 14 archive.tar-rwxr-xr-x 1 \
Joe 3239 Sep 30 mkdirhier. sh
~ /Tmp $
The-c option of grep can be used to calculate the number of output rows, which is faster than the wc-l of the pipeline, as shown in

~ $ Time grep and tmp/a/longfile.txt | wc-l
2811
Real 0m0. 097 s
User 0m0. 006 s
Sys 0m0. 032 s
~ $ Time grep-c and tmp/a/longfile.txt
2811

Real 0m0. 013 s
User 0m0. 006 s
Sys 0m0. 005 s
~ $


Match the output field: Use awk, as shown in figure

~ /Tmp $ ls-l | awk '$6 = "Dec "'
-Rw-r -- 3 joe 5096 Dec 14 archive.tar
-Rw-r -- 1 root 238 Dec 03 08:19 README
~ /Tmp $
Disable cat pipeline output: it can be replaced by grep, as shown in figure

~ $ Time cat tmp/a/longfile.txt | grep and
2811
Real 0m0. 015 s
User 0m0. 003 s
Sys 0m0. 013 s
~ $ Time grep and tmp/a/longfile.txt
2811

Real 0m0. 010 s
User 0m0. 006 s
Sys 0m0. 004 s
~ $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.