日期:2014-05-16  浏览次数:20647 次

删除文件排除指定文件或者目录方法(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  spa