Shell10 good habits

Source: Internet
Author: User
MichaelStutz, author of TheLinuxCookbook, summed up 10 good habits based on years of UNIX experience. I personally think it is really useful. The following is a summary of this article. 1. create a hierarchical Directory: Use the-p option of mkdir, for example, mkdir-ptmp/a/B/c2. unpackage to the specified directory: Use the tar-cstrap, for example, tarxvfnewarc.tar. 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.



1. Create a hierarchical Directory: Use the-p option of 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. Combined commands: Use;, &, |, and other control operators, such as cd tmp/a/B/c & tar xvf ~ /Archive.tar


4. Use variables with caution: place the variables in "", as shown in figure

~ $ 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. 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  

6. Group commands: 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"  

7. Use xargs to filter the output, as shown in figure

~/tmp $ ls -l | xargs  -rw-r--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf -rw-r--r-- 1 \  root root 238 Dec 03 08:19 README drwxr-xr-x 38 joe joe 354082 Nov 02 \  16:07 a -rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar -rwxr-xr-x 1 \  joe joe 3239 Sep 30 12:40 mkdirhier.sh  ~/tmp $ 

8. You can use the-c option of grep 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.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 the output field: Use awk, as shown in figure

  1. ~ /Tmp $ ls-l | awk '$6 = "Dec "'
  2. -Rw-r -- 3 joe 5096 Dec 14 archive.tar
  3. -Rw-r -- 1 root 238 Dec 03 08:19 README
  4. ~ /Tmp $


10. 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.015s   user 0m0.003s   sys 0m0.013s   ~ $ time grep and tmp/a/longfile.txt   2811   real 0m0.010s   user 0m0.006s   sys 0m0.004s   ~ $  

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.