bash 小技巧汇总

简介: bash shell 常用小技巧

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

目录
相关文章
|
2月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如"password"、"confidential"、"secret"),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
145 0
|
2月前
|
Linux Shell Windows
4:Bash shell命令-步入Linux的现代方法
4:Bash shell命令-步入Linux的现代方法
64 0
|
11月前
|
Ubuntu 安全 Linux
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
192 0
|
9月前
|
关系型数据库 MySQL Shell
【Linux命令】-bash: mysql: command not found
【Linux命令】-bash: mysql: command not found
65 0
|
2月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
46 0
Linux Bash 脚本中的 IFS 是什么?
|
2月前
|
Java Shell Linux
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
【linux进程控制(三)】进程程序替换--如何自己实现一个bash解释器?
|
2月前
|
Shell Linux
【Linux】Bash支持各种指令选项的原理:命令行参数
【Linux】Bash支持各种指令选项的原理:命令行参数
|
2月前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
2月前
|
Linux Shell 开发工具
linux】-bash:vim:未找到命令
linux】-bash:vim:未找到命令
35 0

相关课程

更多