LinuxTips

Tips for Linux daily use

  1. 如果想提取同时包含两个关键词的行,可以用awk命令

    1
    awk '/KEY_WORD_1/&&/KEY_WORD_2/' <filename> 

    比如,如果想在文件cdesigner.log中提取行,这些行同时含有关键词”/o”和”FAILED”,那么就可用命令:

    1
    awk '/\/o/&&/FAILED/' cdesigner.log

    结果会在terminal中打印出来。
    如果这两个关键词需要被精确匹配(比如只找work,而不是worker或者homework之类的),那么就用

    1
    awk '/\<KEY_WORD_1\>/&&/\<KEY_WORD_2\>/' <filename>
  2. Vim中搜索多个keyword
    Go to search mode i.e. type ‘/‘ and then type \v followed by the words you want to search separated by ‘ | ‘ (pipe).
    Example:

    1
    /\vword1|word2|word3

    Go to search mode and type the words you want to search separated by ‘| ‘.
    Example:

    1
    /word1\|word2\|word3
  3. 要取消文件名扩展用

    1
    set noglob

    要取消文件名扩展无匹配出错用

    1
    set nonomatch

    要屏蔽出错信息用

    1
    (cmd >/dev/tty) >&/dev/null

    要屏蔽信息用

    1
    cmd >&/dev/null
  4. 以递归的方式列出文件

    1
    2
    find /path/to/search/ -print
    find /path/to/search/ -ls

    比如

    1
    find . -print

    或者

    1
    find . -ls
  5. 结束后台进程
    a. 列出后台进程:

    1
    jobs

    b. 结束命令:

    1
    kill -9 %N(N是jobs列出的进程序号)
  6. Bash shell中变量自增的办法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 1st
    i=`expr $i + 1`;
    # 2nd
    let i+=1;
    # 3rd
    ((i++));
    # 4th
    i=$[$i+1];
    # 5th
    i=$(( $i + 1 ))
  7. 取出指定文本的第10到15行:

    1
    sed -n '10,15p' FILENAME

    取出指定的行(1015, 23, 49, 169221)

    1
    sed -n '10,15p;23,23p;49,49;169,221p' FILENAME
  8. sed 原地替换

    1
    sed -i 's/annotateCurrents/annotateDesign/g' test.txt
  9. 同时找出多种后缀结尾的文件并grep

    1
    find . -regex '.*\.tcl\|.*\.log' | xargs grep -rn "rtUseKeepoutWidth" > ~/find_result_useKeep.rpt
  10. 在vim中对第10至20行的文本按第5个字段以数值升序进行排序

    1
    :10,20!sort -n -k5
  11. linux中find的结果提供给数组使用

    1
    2
    array=($(find . -name "*.txt"))
    for i in "${array[@]}"; do echo $i; done
  12. 删除一个符号链接的方法:

    1
    rm -rf symbolic_name

    注意不是rm -rf symbolic_name/
    也就是说,如果要删除一个符号链接,如果是符号链接文件,直接删除即可,如果是符号链接文件夹,那么在后面就不可以带斜杠”/“

  13. 如果ps的时候中出现,就是僵尸进程,需要kill掉其父进程来kill这个子进程,单独kill这个子进程是没有用的。命令是:

    1
    ps -e -o ppid,stat | grep Z | cut -d” ” -f2 | xargs kill -9
  14. alt+退格,删除最后一个单词

  15. shell脚本参数
    $0 : ./test.sh,即命令本身,相当于c/c++中的argv[0]
    $1 : -f,第一个参数.
    $2 : config.conf
    $3, $4 ... :类推。
    $# : 参数的个数,不包括命令本身,上例中$#为4.
    $@ :参数本身的列表,也不包括命令本身,如上例为 -f config.conf -v –prefix=/home
    $* :和$@相同,但”$“ 和 “$@”(加引号)并不同,”$“将所有的参数解释成一个字符串,而”$@”是一个参数数组。

  16. 判断字符串为空

    1
    2
    3
    if [ "$str" =  "" ] 
    if [ x"$str" = x ]
    if [ -z "$str" ] (-n 为非空)
  17. shell中if的正则匹配可以用如下变量来捕获

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    ip="121.0"
    if [[ $ip =~ ^([0-9]+)\.([0-9]+)$ ]];then
    echo "match"
    echo ${BASH_REMATCH[1]}
    echo ${BASH_REMATCH[2]}
    else
    echo "Not match"
    fi
  18. Csh中打印某个env

    1
    printenv LANG
  19. 几种快速清空文件内容的方法:

    1
    2
    3
    4
    5
    6
    $ : > filename #其中的 : 是一个占位符, 不产生任何输出.
    $ > filename
    $ echo “” > filename
    $ echo /dev/null > filename
    $ echo > filename
    $ cat /dev/null > filename
  20. XZ格式的文件解压

    1
    xz -kd <FILE_NAME>

    这里,k表示–keep,意思是解压之后不删除源文件包,d表示–decompress,意为解压
    之后再用tar命令解压

    1
    tar xf <FILE_NAME>
  21. tar 打包

    1
    tar -czf <TARGET>.tar.gz <FILES_TO_BE_COMPRESSED>
  22. 查看Linux版本

    1
    2
    3
    4
    5
    6
    cat /proc/version
    uname -a
    uname -r
    lsb_release -a
    cat /etc/issue
    cat /etc/redhat-release

    查看是64位还是32位

    1
    2
    getconf LONG_BIT or getconf WORD_BIT
    file /bin/ls
  23. linux 下最大化窗口用Alt + F10,恢复原窗口大小,再次Alt + F10

  24. grep 显示匹配行之前或之后或者之前以及之后的选项:
    显示匹配行,以及之后3行(共4行)。A-After

    1
    grep -A 3 "PATTERN" file.txt

    显示匹配行,以及之前3行(共4行)。B-Before

    1
    grep -B 3 "PATTERN" file.txt

    显示匹配行,以及之前3行和之后三行(共7行)

    1
    grep -C 3 "PATTERN" file.txt

    grep -l 表示只显示文中有match pattern的文件名列表

    递归搜索目录,并排除文件夹

    1
    grep -R --exclude-dir=node_modules "sa_family_t" <path_to_search>

    只计算匹配的行数,用-c这个选项。

    1
    grep -c "<KEYWORD>" <path_to_file>

    只计算不匹配的行数,用-vc这个选项。

    1
    grep -vc "<KEYWORD>" <path_to_file>

    如果要grep一定行数范围内的(比如从5672行到13677行),可以用awk

    1
    awk '/string_to_search/ && NR >= 5672 && NR <= 13677' stack_execute.cpp

    同时grep多个keyword

    1
    grep 'word1\|word2\|word3' /path/to/file
  25. cshell中的tee命令
    在Cshell中把stderr和stdout的输出信息重定向到一个文件里,用>&,比如

    1
    xxx >& run.log

    (This will force both standard output and standard error to go to the same place as the current standard output, effectively what you have with the bash redirection, 2>&1.)

    如果要同时把stderr和stdout打印到屏幕上,并记到log里面,用 |&,比如

    1
    xxx |& tee run.log
  26. 目录中最旧和最新的文件:

    1
    2
    find -type f -printf '%T+ %p\n' | sort | head -n 1
    find -type f -printf '%T+ %p\n' | sort | tail -n 1
  27. find查找多个关键字文件:在-name之间用”-o”

    1
    find . -name "*.txt" -o -name "*.pdf"
  28. CGDB
    设置source window中指向当前马上要执的代码行的箭头格式为长箭头格式

    1
    :set arrowstyle=long

    设置source window中当前马上要执的代码行为block格式

    1
    :set selectedlinedisplay=block
  29. Check linux kernel version

    1
    uname -r
  30. Check CentOS version

    1
    lsb_release -d
  31. 查看当前目录下(深度为1)各个文件以及目录占用磁盘大小

    1
    du -h --max-depth=1 .
Author

Pyrad Selong

Posted on

2022-04-20

Updated on

2022-04-20

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.