Linux 中排除掉筛选的文件

本文介绍如何在Linux环境下使用shell命令删除指定目录内的文件,同时保留某些特定文件或目录。通过不同的命令组合,可以实现仅保留指定文件、删除特定扩展名的文件等多种需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下命令以网站目录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

转载于:https://www.cnblogs.com/itxdm/p/6360145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值