Linux 中排除掉筛选的文件

以下命令以网站目录www为例做介绍,有时候更新网站的时候需要保留比如图片目录,或者其他目录就需要这样的操作

实例一:
删除文件夹内所有文件只保留一个文件命令

[root@linuxzgf www]# cd html
[root@linuxzgf html]# ls
a dede include install plus special templets
data images index.php member robots.txt tags.php uploads
[root@linuxzgf html]# rm -rf `ls -I index.php`
[root@linuxzgf html]# ls
index.php
[root@linuxzgf html]#

注释:
``是TAB上面那个,不是引号 2010-1-14
index.php是要保留的文件名字,此名字可以是文件夹也可以是文件名都可以

实例二:
删除文件夹内所有文件保留部分文件
[root@linuxzgf www]# cd html
[root@linuxzgf html]# ls
a dede include install plus special templets
data images index.php member robots.txt tags.php uploads
[root@linuxzgf html]# rm -rf ls|egrep -v '(index.php|data)'
[root@linuxzgf html]# ls
data index.php
[root@linuxzgf html]#

ls前的` 符号是tab上面的符号
()紧跟的是单引号
index.php|data 是要保留的文件,此处可以保留多个文件

实例三:

删除指定扩展名的某类文件但是要保留部分文件

环境如下:具体环境具体解释和文件名即可
[root@linuxzgf member]# ls
ajax_feedback.php article_edit.php edit_face.php js resetpassword.php uploads_add.php
ajax_loginsta.php buy_action.php edit_fullinfo.php login.php search.php uploads_edit.php
album_add.php buy.php edit_space_info.php mtypes.php shops_orders.php uploads.php
album_edit.php caicai.php feedback.php myfriend.php shops_point.php uploads_select.php
archives_add.php check_card.php flink_main.php mypay.php shops_products.php visit-history.php
archives_do.php config.php guestbook_admin.php mystow.php soft_add.php
archives_edit.php content_list.php images operation.php soft_edit.php
archives_sg_add.php content_sg_list.php inc paycenter space
archives_sg_edit.php control.php index_do.php pm.php spaceskin.php
article_add.php edit_baseinfo.php index.php reg_new.php templets
[root@linuxzgf member]#

实例四:
删除当前目录下所有 *.php文件,除了buy.php

方法一:
[root@linuxzgf member]# rm -rf `ls *.php |egrep -v buy.php`
[root@linuxzgf member]# ls
buy.php images inc js paycenter space templets
[root@linuxzgf member]#

方法二:

[root@linuxzgf member]# rm -rf `ls *.php |awk '{if($0!="buy.php")print $0}'`
[root@linuxzgf member]# ls
buy.php images inc js paycenter space templets
[root@linuxzgf member]#

实例五:
排除多个文件 buy.php caicai.php login.php
方法一:

[root@linuxzgf member]# rm -rf `ls *.php |egrep -v '(buy.php|caicai.php|login.php)'`
[root@linuxzgf member]# ls
buy.php images js paycenter templets
caicai.php inc login.php space
[root@linuxzgf member]#

方法二:

这里是用ls得到原始数据,也可以用find命令
[root@linuxzgf member]# rm -rf find *.php |egrep -v \'(buy.php|caicai.php|login.php)'\
[root@linuxzgf member]# ls
buy.php images js paycenter templets
caicai.php inc login.php space
[root@linuxzgf member]#

此命令只删除了当前目录下的所有php结尾的文件,子目录中的php文件是不会被删除的如inc目录中的php文件在执行命令后还是存在的
本文作者:宗国锋 宗国锋官方网站 转载请注明原文链接http://www.zongguofeng.cn/2010/0115/237.html

posted @ 2017-02-01 17:24  季文康  阅读(2259)  评论(0编辑  收藏  举报