linux 不使用 rm 删除文件
有些情况下在脚本中不允许出现 rm 命令,所以如何删除文件?
1.使用 find 命令
- 删除指定文件
# 示例
# 使用 find 命令查找到指定文件之后使用参数 -delete 删除
[root@localhost shell]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月 23 20:10 22
-rw-r--r--. 1 root root 0 4月 23 20:00 test.sh
[root@localhost shell]# find ./ -name 22 -delete
[root@localhost shell]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月 23 20:00 test.sh
- 批量删除文件
[root@localhost shell]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月 24 16:27 11
-rw-r--r--. 1 root root 0 4月 24 16:27 22
-rw-r--r--. 1 root root 0 4月 24 16:27 33
drwxr-xr-x. 2 root root 6 4月 24 16:27 44
-rw-r--r--. 1 root root 0 4月 23 20:00 test.sh
[root@localhost shell]# find ./ -type f -delete
[root@localhost shell]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 24 16:27 44
[root@localhost shell]#
2. 使用 rsync 命令
# 新建一个空目录
mkdir demo
# 将"rsync"目录与空目录进行同步,即可清空"rsync"目录
rsync -a --delete demo/ rsync/