删除文件排除指定文件或者目录方法(linux)

删除文件排除指定文件或者目录方法
以下命令以网站目录www为例做介绍,有时候更新网站的时候需要保留比如图片目录,或者其他目录就需要这样的操作
实例一:
删除文件夹内所有文件只保留一个文件命令
# cd html
# ls
a dede include install plus special templets
data images index.php member robots.txt tags.php uploads
# rm -rf `ls -I index.php`
# ls
index.php
#
注释:
``是TAB上面那个,不是引号 2010-1-14
index.php是要保留的文件名字,此名字可以是文件夹也可以是文件名都可以
实例二:
删除文件夹内所有文件保留部分文件
# cd html
# ls
a dede include install plus special templets
data images index.php member robots.txt tags.php uploads
# rm -rf `ls|egrep -v '(index.php|data)'`
# ls
data index.php
#
ls前的` 符号是tab上面的符号
()紧跟的是单引号
index.php|data 是要保留的文件,此处可以保留多个文件
实例三:
删除指定扩展名的某类文件但是要保留部分文件
环境如下:具体环境具体解释和文件名即可
# 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
#

实例四:
删除当前目录下所有 *.php文件,除了buy.php
方法一:
# rm -rf `ls *.php |egrep -v buy.php`
# ls
buy.php images inc js paycenter space templets
#
方法二:
# rm -rf `ls *.php |awk '{if($0!="buy.php")print $0}'`
# ls
buy.php images inc js paycenter space templets
#
实例五:
排除多个文件 buy.php caicai.php login.php
方法一:
# rm -rf `ls *.php |egrep -v '(buy.php|caicai.php|login.php)'`
# ls
buy.php images js paycenter templets
caicai.php inc login.php space
#
方法二:
这里是用ls得到原始数据,也可以用find命令
# rm -rf `find *.php |egrep -v '(buy.php|caicai.php|login.php)'`
# ls
buy.php images js paycenter templets
caicai.php inc login.php space
#
此命令只删除了当前目录下的所有php结尾的文件,子目录中的php文件是不会被删除的如inc目录中的php文件在执行命令后还是存在的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值