天天看点

bash 小技巧汇总

1. 按时间先后,列出最后的十个目录

ls /mnt/daily/concord/main -sort -t | awk /_[0-9]+-[0-9]/'{print $nf}' | tail -10

/mnt/daily/lotuslive目录内容如下:

sc10.0_docs                 :dir

sc10.0_docsproxy        :dir

sc20.0_docs                 :dir

sc20.0_docsproxy        :dir

sc30.0_docs                 :dir

sc30.0_docsproxy        :dir

sc30.16_docs               :dir

sc30.16_viewer            :dir

tsm_backup                    :file

run 如下命令后:

find /mnt/daily/lotuslive -mindepth 1 -maxdepth 1 -type d | sort | awk -f '[/]' '{print $5}' | awk -f '[_]' '/sc[234]/{print $1}' | uniq

显示:

sc20.0

sc30.0

sc30.16 

2. 递归删除空目录

# $1必须是绝对路径

crurl=$1

func_hdir(){

echo $crurl

  cd $crurl

  for aitem in `ls -l | grep "^d" | awk '{print $9}'`; do

        crurl=$crurl/$aitem

        func_hdir $aitem

  done

  dirc=`ls $crurl`

  if [ "$dirc" = "" ]

  then

    echo $crurl

    rm -rf $crurl

  fi

  crurl=${crurl%/*}

}

func_hdir

3. sed删除特定的行

    sed -e '/^[  ]*$/d' osgi_file > target_file //删除空行

    sed -d '/concord/d' osgi_file>target_file//包涵concord的行

4. 输出最新的n个目录,

find /mnt/daily/concord/main -mindepth 1 -maxdepth 1 -type d -printf "%t@%tx %p" | sort -n -r | head -n

5. 输出最近5天创建的目录

find /mnt/daily/concord/main -mindepth1 -maxdepth 1 -type d -mtime -5

-mtime 最大数是8,超过8就是输出全部

6. sort by 特定列

如当前工作中的应用,以msg_node_%d排序,可用如下命令

find . -type f -name envconfs.conf | grep -v "chatroom"| grep "appnodemessagepool"| sort -t '.' -k4

find . -type f -name envconfs.conf|grep "appnodemessagepool"|sort -t '/' -k2 

./com.rcloud.appnodemessagepool.msg_node_3/conf/envconfs.conf

./com.rcloud.appnodemessagepool.msg_node_4/conf/envconfs.conf

./com.rcloud.appnodemessagepool.msg_node_5/conf/envconfs.conf