导读 bash 是一个为GNU计划编写的Unix shell。它的名字是一系列缩写:Bourne-Again SHell — 这是关于Bourne shell(sh)的一个双关语(Bourne again / born again)。bash我们很常用,但是更高级的用法你知道吗?看完下面的示例,你一定会有启发的。

bash-logo-web

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
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

原文来自:https://yq.aliyun.com/articles/61017?spm=5176.100239.bloglist.92.86B7DB

本文地址: https://www.linuxprobe.com/bash-list.html 编辑:岳国帅,审核员:苏西云

 

本文原创地址:https://www.linuxprobe.com/bash-list.html编辑:public,审核员:暂无