Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71821
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-17 20:15
个人简介

我叫什么干什么的我还是我不知道

文章分类

全部博文(26)

文章存档

2016年(4)

2015年(22)

我的朋友

分类: LINUX

2015-06-05 00:45:13

面试题:删除一个目录下的所有文件,但保留一个指定文件
 
解答:
假设这个目录是/xx/,里面有file1,file2,file3..file10  十个文件


点击(此处)折叠或打开

  1. [root@oldboy xx]# touch file{1..10}

  2. [root@oldboy xx]# ls

  3. file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

方法一:find

点击(此处)折叠或打开

  1. [root@oldboy xx]# ls

  2. file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

  3. [root@oldboy xx]# find /xx -type f ! -name "file10"|xargs rm -f

  4. [root@oldboy xx]# ls

  5. file10

  6.  

  7. [root@oldboy xx]# find /xx -type f ! -name "file10" -exec rm -f {} \;

  8. [root@oldboy xx]# ls

  9. file10


阅读(924) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~