面试题:删除一个目录下的所有文件,但保留一个指定文件

来源:互联网 发布:淘宝联盟的钱返到哪里 编辑:程序博客网 时间:2024/05/22 09:49
面试题:删除一个目录下的所有文件,但保留一个指定文件
 
解答:
假设这个目录是/xx/,里面有file1,file2,file3..file10  十个文件
[root@oldboy xx]# touch file{1..10}[root@oldboy xx]# lsfile1  file10  file2  file3  file4  file5  file6  file7  file8  file9

 
方法一:find
[root@oldboy xx]# lsfile1  file10  file2  file3  file4  file5  file6  file7  file8  file9[root@oldboy xx]# find /xx -type f ! -name "file10"|xargs rm -f [root@oldboy xx]# lsfile10 [root@oldboy xx]# find /xx -type f ! -name "file10" -exec rm -f {} \;     [root@oldboy xx]# lsfile10

0 0
原创粉丝点击