---
title: "find 与 rm"
author: "samson7b"
date: "2013-03-12T11:02:25Z"
tags: []
categories: ["操作系统"]
slug: "find-rm"
draft: false
lastmod: "2013-03-12T11:02:25Z"
id: 1828015
url: "https://samson7b.iteye.com/blog/1828015"
---

查找并删除所有不以.zip结尾的所有文件

 

find -maxdepth 1 ! -name "\*.zip" -print0 | xargs -0 rm -rf

 

其中需要注意的有两点

  1. 感叹号（！） 代表“非” 这个逻辑含义，去掉感叹号就是以.zip结尾

  2.-print0 代表以\\0为换行符，配合xargs -0 可以防止在文件名中包含\\n时删除文件不正确的情况

     因为-print 是以\\n为换行符，当碰到文件名中包含\\n时会认为是两个文件，导致删除不正确。（是在linux shell 脚本攻略 - 这本书xargs一章里看到的)