linux 下删除一个目录下所有的隐藏文件和非目录文件
前言
在 alpine 基础上编译 OpenWAF,想要达到最简,删除无用文件。
亮点
rm -rf `ls -Fa | grep '^\.\w'` -- 删除隐藏文件(包含目录)
rm -f `ls -F | grep -v '/$'` -- 删除非目录文件(不包含子目录)
- 自由定义 grep 后的正则,已达到不同的目的
- ` 是 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 过滤出要删除的文件。