# how to remove all except one file?

**URL:** <https://community.unix.com/t/how-to-remove-all-except-one-file/183553>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [January 29, 2008, 7:36am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553 "2008-01-29T07:36:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![praveen\_b744](https://community.unix.com/letter_avatar/praveen_b744/32/5_5575768a8748004e209b776fc1b2916d.png) [@praveen\_b744](https://community.unix.com/u/praveen_b744)\
**Post date:** [January 29, 2008, 7:36am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/1 "2008-01-29T07:36:36Z")

</div>

How can I remove all files and folders in a directory except one perticular file?

Suppose in a folder we have files f1,f2,f3.In this I want to delete all files except f1.

can any one tel me?

thanks in advance..

---

<div class="post-metadata">

**Author:** ![pt14](https://community.unix.com/letter_avatar/pt14/32/5_5575768a8748004e209b776fc1b2916d.png) [@pt14](https://community.unix.com/u/pt14)\
**Post date:** [January 29, 2008, 7:52am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/2 "2008-01-29T07:52:35Z")

</div>

ls \*|grep -v f1|xargs rm -rf

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [January 29, 2008, 8:01am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/3 "2008-01-29T08:01:39Z")

</div>

Or copy f1 to /tmp, rm all the files in the current directory and move back /tmp/f1 to the current directory.

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 29, 2008, 8:15am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/4 "2008-01-29T08:15:13Z")

</div>

> [@praveen\_b744](#):
>
> How can I remove all files and folders in a directory except one perticular file?
> 
> Suppose in a folder we have files f1,f2,f3.In this I want to delete all files except f1.  
> [...]

[ksh]

```plaintext
rm !(f1)

```

[zsh]

```plaintext
rm ^f1

```

[bash]

```plaintext
shopt -s extglob
rm !(f1)

```

---

<div class="post-metadata">

**Author:** ![Tuche](https://community.unix.com/letter_avatar/tuche/32/5_5575768a8748004e209b776fc1b2916d.png) [@Tuche](https://community.unix.com/u/Tuche)\
**Post date:** [March 1, 2009, 10:34am UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/5 "2009-03-01T10:34:38Z")

</div>

Sorry for bumping this, but . . .

if i wanted to delete all files and/or folders inside a specific folder but keep the files and folders that start with p and m, i should do . . .

rm -R \*.\* !(p\*) !(m\*)

is that correct ?

Thanks !

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [March 1, 2009, 1:56pm UTC](https://community.unix.com/t/how-to-remove-all-except-one-file/183553/6 "2009-03-01T13:56:23Z")

</div>

After enabling the extended glob you could try something like this:

```nohighlight
rm -r -- !(@(m|p)*)

```
