天天看點

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