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
- ~/tmp $ ls-l | awk ' $6 = = ' Dec '
- -rw-r--r--3 Joe Joe 5096 Dec 14:26 Archive.tar
- -rw-r--r--1 root root 238 Dec 08:19 README
- ~/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