linux 下删除一个目录下所有的隐藏文件和非目录文件

前言

在 alpine 基础上编译 OpenWAF,想要达到最简,删除无用文件。

亮点

rm -rf `ls -Fa | grep '^\.\w'`   -- 删除隐藏文件(包含目录)
rm -f `ls -F | grep -v '/$'`     -- 删除非目录文件(不包含子目录)
  1. 自由定义 grep 后的正则,已达到不同的目的
  2. ` 是 TAB 键上的反引号

ls+grep+rm 删除多余文件

当前目录结构

root@iZ94tcbc871Z:/opt/OpenWAF# ls -la
total 236
drwxr-xr-x 8 root root  4096 Jan  8 17:42 .
drwxr-xr-x 4 root root  4096 Jan  8 17:42 ..
drwxr-xr-x 2 root root  4096 Jan  8 17:42 app
-rw-r--r-- 1 root root  2716 Jan  8 17:42 Changelog
-rw-r--r-- 1 root root  5470 Jan  8 17:42 CODE_OF_CONDUCT.md
drwxr-xr-x 3 root root  4096 Jan  8 17:42 conf
-rw-r--r-- 1 root root   369 Jan  8 17:42 CONTRIBUTING.md
-rw-r--r-- 1 root root   267 Jan  8 17:42 dist.ini
drwxr-xr-x 2 root root  4096 Jan  8 17:42 doc
-rw-r--r-- 1 root root  2702 Jan  8 17:42 Dockerfile
drwxr-xr-x 8 root root  4096 Jan  8 17:42 .git
drwxr-xr-x 3 root root  4096 Jan  8 17:42 .github
-rw-r--r-- 1 root root   324 Jan  8 17:42 .gitignore
drwxr-xr-x 5 root root  4096 Jan  8 17:42 lib
-rw-r--r-- 1 root root 11357 Jan  8 17:42 LICENSE
-rw-r--r-- 1 root root   627 Jan  8 17:42 Makefile
-rw-r--r-- 1 root root 79083 Jan  8 17:42 README_CN.md
-rw-r--r-- 1 root root 79371 Jan  8 17:42 README.md

当前目录大小

root@iZ94tcbc871Z:/opt/OpenWAF# du -h | grep "\.$"
43M     .

去除多余文件

目录中仅有 app、conf、lib 三个目录有用
其中 lib/openresty 目录也可删除

rm -rf doc lib/openresty

目录结构变为:

root@iZ94tcbc871Z:/opt/OpenWAF# ls -la
total 232
drwxr-xr-x 7 root root  4096 Jan  8 17:56 .
drwxr-xr-x 4 root root  4096 Jan  8 17:42 ..
drwxr-xr-x 2 root root  4096 Jan  8 17:42 app
-rw-r--r-- 1 root root  2716 Jan  8 17:42 Changelog
-rw-r--r-- 1 root root  5470 Jan  8 17:42 CODE_OF_CONDUCT.md
drwxr-xr-x 3 root root  4096 Jan  8 17:42 conf
-rw-r--r-- 1 root root   369 Jan  8 17:42 CONTRIBUTING.md
-rw-r--r-- 1 root root   267 Jan  8 17:42 dist.ini
-rw-r--r-- 1 root root  2702 Jan  8 17:42 Dockerfile
drwxr-xr-x 8 root root  4096 Jan  8 17:42 .git
drwxr-xr-x 3 root root  4096 Jan  8 17:42 .github
-rw-r--r-- 1 root root   324 Jan  8 17:42 .gitignore
drwxr-xr-x 4 root root  4096 Jan  8 17:56 lib
-rw-r--r-- 1 root root 11357 Jan  8 17:42 LICENSE
-rw-r--r-- 1 root root   627 Jan  8 17:42 Makefile
-rw-r--r-- 1 root root 79083 Jan  8 17:42 README_CN.md
-rw-r--r-- 1 root root 79371 Jan  8 17:42 README.md

仅有 app、conf、lib 三个目录有用
因此,第一步删除所有因此文件(包含目录)

root@iZ94tcbc871Z:/opt/OpenWAF# ls -Fa | grep '^\.\w'    --查看隐藏文件
.git/
.github/
.gitignore
root@iZ94tcbc871Z:/opt/OpenWAF# rm -rf `ls -Fa | grep '^\.\w'`  --删除隐藏文件
root@iZ94tcbc871Z:/opt/OpenWAF# ls -la     -- 剩下的文件
total 220
drwxr-xr-x 5 root root  4096 Jan  8 18:00 .
drwxr-xr-x 4 root root  4096 Jan  8 17:42 ..
drwxr-xr-x 2 root root  4096 Jan  8 17:42 app
-rw-r--r-- 1 root root  2716 Jan  8 17:42 Changelog
-rw-r--r-- 1 root root  5470 Jan  8 17:42 CODE_OF_CONDUCT.md
drwxr-xr-x 3 root root  4096 Jan  8 17:42 conf
-rw-r--r-- 1 root root   369 Jan  8 17:42 CONTRIBUTING.md
-rw-r--r-- 1 root root   267 Jan  8 17:42 dist.ini
-rw-r--r-- 1 root root  2702 Jan  8 17:42 Dockerfile
drwxr-xr-x 4 root root  4096 Jan  8 17:56 lib
-rw-r--r-- 1 root root 11357 Jan  8 17:42 LICENSE
-rw-r--r-- 1 root root   627 Jan  8 17:42 Makefile
-rw-r--r-- 1 root root 79083 Jan  8 17:42 README_CN.md
-rw-r--r-- 1 root root 79371 Jan  8 17:42 README.m

再删除非目录的文件

root@iZ94tcbc871Z:/opt/OpenWAF# ls -F | grep -v '/$'   -- 查看非目录文件
Changelog
CODE_OF_CONDUCT.md
CONTRIBUTING.md
dist.ini
Dockerfile
LICENSE
Makefile
README_CN.md
README.md
root@iZ94tcbc871Z:/opt/OpenWAF# rm -f `ls -F | grep -v '/$'`  -- 删除非目录文件
root@iZ94tcbc871Z:/opt/OpenWAF# ls -la    -- 剩余文件
total 20
drwxr-xr-x 5 root root 4096 Jan  8 18:03 .
drwxr-xr-x 4 root root 4096 Jan  8 17:42 ..
drwxr-xr-x 2 root root 4096 Jan  8 17:42 app
drwxr-xr-x 3 root root 4096 Jan  8 17:42 conf
drwxr-xr-x 4 root root 4096 Jan  8 17:56 lib

再次查看目录大小

root@iZ94tcbc871Z:/opt/OpenWAF# du -h | grep "\.$"
7.7M    .

为何用 ls 和 grep

rm -rf `ls -Fa | grep '^\.\w'`   -- 删除隐藏文件(包含目录)
rm -f `ls -F | grep -v '/$'`     -- 删除非目录文件(不包含子目录)

上面最后这两步,完全可以用下文代替:

rm -f * .*

但会出现如下提示:

rm: 'app' is a directory
rm: 'conf' is a directory
rm: 'lib' is a directory
rm: can't remove '.' or '..'
rm: can't remove '.' or '..'
rm: '.git' is a directory
rm: '.github' is a directory

平时可忽略此提示,但在用 dockerfile 编译时,此提示会中断编译。
因此需先用 ls 和 grep 过滤出要删除的文件。

  • ipset

    ipset 安装参考文档(待整理)ipset 官方文档ipset 7.1版本链接使用参考文档 简单的流程可以用这几条命令概括使用 ipset 和 iptables 进行 IP 封禁的流程 ipset create blacklist ...

    ipset
  • 在 alpine 上编译 openssl 遇到的问题

    alpine 上编译 openssl (v1.1.1) 遇到 ucontext 报错 前言今天在 alpine 基础上编译 openwaf,编到 openssl 时,报 ucontext 相关错误。 ucontext 报错编译 ope...

    在 alpine 上编译 openssl 遇到的问题
  • 批量修改或添加文件后缀名

    前言常见的是对不同文件名进行文件后缀名修改。但今天遇到同一文件名的情况。 常见windows 或 Linux 命令: ren *.jpg *.bmp # 将 jpg 后缀改为 bmp 如:1.jpg 2.txt 改后为:1.bm...

    批量修改或添加文件后缀名
  • hyperscan 安装

    1. 前言本文在 Debian8 中安装 hyperscan 5.0.0内存至少 2 G,不然编译慢而且失败 2. 依赖2.1 C/C++编译器hyperscan使用C++开发,且需要C99和C++11支持,目前支持的编译器有 GC...

    hyperscan 安装
  • linux 查看系统开机时间

    有时候需要查看Linux系统运行了多久时间,此时需要知道上次开机启动时间;有时候由于断电或供电故障突然停机,需要查看Linux开机时间/重启时间;下面总结一些查看Linux开机关机时间的方法(非常全面) 1. who 命令查看who ...

    linux 查看系统开机时间
  • GDB 多线程 (non-stop)

    1. 背景这几天在扩展 ngx_lua 模块,但 gdb 定位时,提示:Thread debugging using libthread_db enabled。 2. GDB non-stop 配置把以下3行添加到 ~/.gdbini...

    GDB 多线程 (non-stop)
  • suricata 从 0 开始

    背景OpenWAF 是在 openresty 基础上发布的,但安全不仅仅是针对 HTTP 协议的防护,而是全方位立体化的防护。因此,为了防护更多的协议,开始接触 suricata,用熟后,争取将 OpenWAF 集成到 suricat...

    suricata 从 0 开始
  • clear: command not found 命令无法找到

    1. 安装 ncurses-binsudo apt-get install ncurses-bin 此时尝试执行 ‘clear’ 命令,若失败,执行第二步(重新安装 ncurses-bin) 2. 重新安装 ncurses-binsu...

    clear: command not found 命令无法找到
  • svn 游记

    前言公司之前用 svn 管理项目代码,我一直用的 windows 版本 近期想要搭建知识库云平台,需要在 linux 上使用 svn 管理代码,因此做些笔记 直接使用公司搭建好的 svn服务器 1. 客户端安装yum install ...

    svn 游记
  • saltstack 从 0 开始

    1. 安装//salt-master安装(环境:OS-debian,IP-120.25.255.213) # apt-get install -t jessie-backports salt-master //salt-minion安...

    saltstack 从 0 开始