Using Linux for so long, these tips you may not know yet!

Source: Internet
Author: User

We shared 30 Linux usage tips last week, but not enough! Today summed up some, on the road to learning Linux hope to help you.
previous: "30 must know the Linux command skills, you have mastered it?" "

31, the Monitoring directory, the newly created file name is appended to the log
#要安装inotify-tools软件包#!/bin/bashMON_DIR=/optinotifywait -mq --format %f -e create $MON_DIR |while read files; do  echo $files >> test.logdone
32. Find multiple specified file types at once
# find ./ -name ‘*.jpg‘ -o -name ‘*.png‘# find ./ -regex ".*\.jpg\|.*\.png"
33. String splitting
# echo "hello" |awk -F ‘‘ ‘{for(i=1;i<=NF;i++)print $i}‘# echo "hello" |sed ‘s/./&\n/g‘# echo "hello" |sed -r ‘s/(.)/\1\n/g‘
34, real-time monitoring command operation results

# watch -d -n 1 ‘ifconfig‘

35, solve the message garbled problem
# echo `echo "content" | iconv -f utf8 -t gbk` | mail -s "`echo "title" | iconv -f utf8 -t gbk`" [email protected]注:通过iconv工具将内容字符集转换
36. Add a newline or content in the text every three lines
# sed ‘4~3s/^/\n/‘ file# awk ‘$0;NR%3==0{print "\n"}‘ file# awk ‘{print NR%3?$0:$0 "\n"}‘ file
37. Delete the matching line and the last line or the previous line
# sed ‘/abc/,+1d‘ file  #删除匹配行及后一行# sed ‘/abc/{n;d}‘ file #删除后一行# tac file |sed ‘/abc/,+1d‘ |tac  #删除前一行
38, statistics total number of rows
效率1 # wc -l file  效率2 # grep -c . file效率3 # awk ‘END{print NR}‘ file效率4 # sed -n ‘$=‘ file
39. Remove text opening and closing spaces

# sed -i ‘s/^[ \t]*//;s/[ \t]*$//‘ file

40. Add single quotation marks to a single IP
41. Print wait time in script
wait(){echo -n "wait 3s"for ((i=1;i<=3;i++)); do    echo -n "."    sleep 1doneecho }wait
42. Delete the specified line
# awk ‘NR==1{next}{print $0}‘ file #$0可省略# awk ‘NR!=1{print}‘ file# awk ‘NR!=1{print $0}‘ 或删除匹配行:awk ‘!/test/{print $0}‘# sed ‘1d‘ file# sed -n ‘1!p‘ file
43. Add a line before and after the specified line
在第二行前一行加txt:# awk ‘NR==2{sub(‘/.*/‘,"txt\n&")}{print}‘ a.txt # sed‘2s/.*/txt\n&/‘ a.txt在第二行后一行加txt:# awk ‘NR==2{sub(‘/.*/‘,"&\ntxt")}{print}‘ a.txt# sed‘2s/.*/&\ntxt/‘ a.txt
44. Get the NIC name via IP

# ifconfig |awk -F‘[: ]‘ ‘/^eth/{nic=$1}/192.168.18.15/{print nic}‘

45. Floating-point arithmetic (number 46 keeps the decimal point)
# awk ‘BEGIN{print 46/100}‘  0.46# echo 46|awk ‘{print $0/100}‘0.46# awk ‘BEGIN{printf "%.2f\n",46/100}‘0.46# echo ‘scale=2;46/100‘ |bc|sed ‘s/^/0/‘0.46# printf "%.2f\n" $(echo "scale=2;46/100" |bc)0.46
46. Floating-point comparison
方法1:if [ $(echo "4>3"|bc) -eq 1 ]; then    echo yeselse    echo nofi方法2:if [ $(awk ‘BEGIN{if(4>3)print 1;else print 0}‘) -eq 1 ]; then    echo yeselse    echo nofi
47. Replace the newline character with a comma
$ cat a.txt1:23替换后:1,2,3方法1:$ tr ‘\n‘ ‘,‘ < a.txt$ sed ‘:a;N;s/\n/,/;$!b a‘ a.txt$ sed ‘:a;$!N;s/\n/,/;t a‘ a.txt  :方法2:while read line; do    a+=($line)done < a.txtecho ${a[*]} |sed ‘s/ /,/g‘方法3:awk ‘{s=(s?s","$0:$0)}END{print s}‘ a.txt#三目运算符(a?b:c),第一个s是变量,s?s","$0:$0,第一次处理1时,s变量没有赋值为假,结果打印1,第二次处理2时,s值是1,为真,结果1,2。以此类推,小括号可以不写。awk ‘{if($0!=3)printf "%s,",$0;else print $0}‘ a.txt
48, Windows under the text to Linux hidden format removal
方法1:打开文件后输入:set fileformat=unix方法2:打开文件后输入:%s/\r*$//  #^M可用\r代替方法3:sed -i ‘s/^M//g‘ a.txt  #^M的输入方式是ctrl+v,然后ctrl+m方法4:dos2unix a.txt
49, Xargs skillfully use
xargs -n1  #将单个字段作为一行# cat a.txt1 2 3 4# xargs -n1 < a.txt1234
xargs -n2 #将两个字段作为一行$ cat b.txtstringnumbera1b2$ xargs -n2 < a.txt string numbera 1b 2
50. Statistics the total size of files in the current directory ending in. html
方法1:# find . -name "*.html" -maxdepth 1 -exec du -b {} \; |awk ‘{sum+=$1}END{print sum}‘方法2:for size in $(ls -l *.html |awk ‘{print $5}‘); do    sum=$(($sum+$size))doneecho $sum递归统计:# find . -name "*.html" -exec du -k {} \; |awk ‘{sum+=$1}END{print sum}‘

Using Linux for so long, these tips you may not know yet!

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.