例题:删除一个目录下的所有文件,但保留一个指定文件。
解答:
假设这个目录是/xx/,里面有file1,file2,file3,file4,file5 五个文件
[root@oldboy xx]# touch file{1..5}
[root@oldboy xx]# ls
file1 file2 file3 file4 file5
方法一: find
[root@oldboy xx]# ls
file1 file2 file3 file4 file5
[root@oldboy xx]# find /xx -type f ! -name "file5"|xargs rm -f
[root@oldboy xx]# ls
file5
或 [root@oldboy xx]# find /xx -type f ! -name "file5" -exec rm -f {} \;
[root@oldboy xx]# ls
file5
未完待续…
( 原创~oldboy培训)
转载于:https://blog.51cto.com/zhaopengju/1962095