面试题:删除一个目录下的所有文件,但保留一个指定文件
解答:
假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件[root@oldboy xx]# touch file{1..10}
[root@oldboy xx]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
方法一:find
[root@oldboy xx]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@oldboy xx]# find /xx -type f ! -name "file10"|xargs rm -f
[root@oldboy xx]# ls
file10
[root@oldboy xx]# find /xx -type f ! -name "file10" -exec rm -f {} \;
[root@oldboy xx]# ls
file10
[root@oldboy xx]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@oldboy xx]# find /xx -type f ! -name "file10"|xargs rm -f
[root@oldboy xx]# ls
file10
[root@oldboy xx]# find /xx -type f ! -name "file10" -exec rm -f {} \;
[root@oldboy xx]# ls
file10