Shell 10 a good habit

Source: Internet
Author: User

Michael Stutz, author of the Linux Cookbook, has summed up 10 good habits with his many years of experience in using UNIX, and personally believes that it is really useful, and is summarized below to share with you.



1. Create a hierarchy directory: Use the-P option for mkdir, such as Mkdir-p tmp/a/b/c


2. Unpack to the specified directory: Use the-c option of tar, such as tar xvf newarc.tar.gz-c tmp/a/b/c




3. Joint command: Use;, &&, | | such as control operators, such as CD tmp/a/b/c && tar xvf ~/archive.tar


4. Use the variable carefully: Place the variable in the "", as

~ $ ls tmp/  a b  ~ $ var= "tmp/*"  ~ $ echo $VAR  tmp/a tmp/b  ~ $ echo "$VAR"  tmp/*  ~ $ echo $VARa   ~ $ echo "$VARa"    ~ $ echo "${var}a"  tmp/*a  ~ $ echo ${var}a  tmp/a  

5. Input of long command: use \ Branch break, as

~ $ cd TMP/A/B/C | | \  > Mkdir-p tmp/a/b/c && \  > Tar xvf-c tmp/a/b/c ~/archive.tar  


6. Group command: Use (), {} to group commands, such as

~ $ (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"  

7. Use Xargs: Can filter the output, such as

~/tmp $ ls-l |  Xargs  -rw-r--r--7 Joe Joe 12043 Jan 20:36 december_report.pdf-rw-r--r--1 \  root root 238 Dec 08:19 README Drwxr-xr-x Joe Joe 354082 Nov \  16:07 a-rw-r--r--3 Joe Joe 5096 Dec 14:26 1 \  J OE Joe 3239 Sep 12:40 mkdirhier.sh  

8. Use the-c option of grep to calculate the number of rows in the output, which is faster than using the wc-l of the pipe, as

  ~ $ time grep and Tmp/a/longfile.txt | Wc-l    2811     real 0m0.097s    user 0m0.006s    sys 0m0.032s    ~ $ time grep-c and Tmp/a/longfile.txt    2811        Real 0m0.013s    user 0m0.006s  sys 0m0.005s  ~ $  


9. Match output fields: Using awk, such as

    1. ~/tmp $ ls-l | awk ' $6 = = ' Dec '
    2. -rw-r--r--3 Joe Joe 5096 Dec 14:26 Archive.tar
    3. -rw-r--r--1 root root 238 Dec 08:19 README
    4. ~/tmp $


10. Disable cat's pipe output: use grep instead, as

~ $ time Cat Tmp/a/longfile.txt | grep and   2811    real 0m0.015s   user 0m0.003s   sys 0m0.013s   ~ $ time grep and Tmp/a/longfile.txt   2811   Real 0m0.010s   user 0m0.006s   sys 0m0.004s   ~ $  

Shell 10 a good habit

Related Article

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.