一半君的总结纸

听话只听一半君

linux tips : find grep xargs awk curl单行命令搞定备忘录

试了半天才试出来 … 有必要收藏一下

  • 搜索所有子文件夹,找到包含 ilm_swapShapes 的所有*.mel 文件,不显示路径里有 versions的 ,结果显示成 @#$%.mel – Line number : XX grep的第一个match
    find /searchPath -not -iwholename "*/versions/*" -name "*.mel" -exec grep -in -m 1 ilm_swapShapes {} /dev/null \; | awk -F: '{print ""$1 " - Line number : "$2" "$3}'
    

    网上搜索以后 有人说不推荐用xargs,除非同时用-print0 不然万一路径里有空格或是特殊符号就会不对了
    edit: xargs 后面加 -0 也许就可以了吧 但是依然
    It is not possible for xargs to be used securely, since there will always be a time gap between the production of the list of input files and their use in the commands that xargs issues. If other users have access to the system, they can manipulate the filesystem during this time window to force the action of the commands xargs runs to apply to files that you didn’t intend.

  • 如何删除其他文件,保留符合match的文件
    如果是bash,extglob必须是on状态

    shopt -s extglob
    # *.lnx留下,其他的删了
    $ rm !(*.lnx)
    
    # 找到所有的结尾不是 _new.html 的 file
    find . -maxdepth 1 -type f | grep -v '_new.html$'
    # 另一种写法
    find . -maxdepth 1 -type f -not -name '*_new.html'
    
  • 关闭显示器,这个在用中古笔记本长时间运行某个任务时也许会需要
    sleep 1;xset dpms force off
    
  • 如何对多个文件逐个执行同一个命令
    find /some/directory -maxdepth 1 -type f -exec cmd option {} \; > results.out
    

    一行必杀奥义 Here is a quick solution using find and also sorting files by their modification date:

    find  directory/ -maxdepth 1 -type f -print0 | \
      xargs -r0 stat -c "%y %n" | \
      sort | cut -d' ' -f4- | \
      xargs -d "\n" -I{} cmd -op1 {}
    
  • 如何完全不显示output到console里,在你不想出来太多垃圾干扰视线的时候用
    curl http://www.google.com > /dev/null 2>&1 &
    

    此处的关键点是 2>&1 其含义是
    The 1 denotes standard output (stdout). The 2 denotes standard error (stderr).
    So 2>&1 says to send standard error to where ever standard output is being redirected as well. Which since it’s being sent to /dev/null is akin to ignoring any output at all.

  • 查看当前shell
    echo $0
    
  • 改变当前shell prompt
    # 临时改比如
    PS1='\u:\W\$ '
    

    永久改变 linux how chagne command prompt

  • 实时查看log

    tail -f /path/to/thefile.log
    
  • curl先下载再解压

    curl /path/to/xxx.tar.gz | tar xz
    curl /path/to/xxx.tar.bz2 | tar xj
    

参考:
commandlinefu.com
Sed – An Introduction and Tutorial by Bruce Barnett
What does > /dev/null 2>&1 mean?

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: