# remove files other than selected

**URL:** <https://community.unix.com/t/remove-files-other-than-selected/178043>\
**Category:** Shell Programming and Scripting\
**Created:** [November 23, 2007, 12:09am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043 "2007-11-23T00:09:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 23, 2007, 12:09am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/1 "2007-11-23T00:09:25Z")

</div>

I wanna remove a set files other than some selected files.

Eg.

:rolleyes::rolleyes::rolleyes:

a directory contains n files like

test1.dat  
test2.dat  
test3.dat  
test4.dat  
out5.dat  
out1.dat

i wanna remove all files which doesnot name like \*test\*

I want to use this in shell script, so please make sure that the cmd is independent of the working directory... !!! 😉

Thanks

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/5_5575768a8748004e209b776fc1b2916d.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [November 23, 2007, 12:55am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/2 "2007-11-23T00:55:36Z")

</div>

```nohighlight
find path | while read N
do
    case `basename $N`
      dontDeleteMe | orMe | orMeEither )
          ;;
      * )
          rm "$N"
          ;;
    esac
done

```

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 23, 2007, 3:38am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/3 "2007-11-23T03:38:28Z")

</div>

got it..!!! 🆒:D:D

ls /tmp/ | grep -v test | xargs -i ksh -c 'rm /tmp/"{}" '

can anyone lemme know if it is fool proof..!! :rolleyes:

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/5_5575768a8748004e209b776fc1b2916d.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [November 23, 2007, 5:01am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/4 "2007-11-23T05:01:05Z")

</div>

> [@freakygs](#):
>
> can anyone lemme know if it is fool proof..!! :rolleyes:

/tmp is a shared directory, other people could have all kinds of stuff in there.

---

<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:** [November 23, 2007, 5:35am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/5 "2007-11-23T05:35:39Z")

</div>

ksh:

```nohighlight
rm !(*test*)

```

bash:

```nohighlight
shopt extglob
rm !(*test*)

```

zsh (require setopt extendedglob):

```nohighlight
rm *~*test*

```

or:

```nohighlight
rm ^*test*

```

But be careful 🙂

---

<div class="post-metadata">

**Author:** ![leonard](https://community.unix.com/letter_avatar/leonard/32/5_5575768a8748004e209b776fc1b2916d.png) [@leonard](https://community.unix.com/u/leonard)\
**Post date:** [November 23, 2007, 7:51am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/6 "2007-11-23T07:51:14Z")

</div>

Try this

rm out?

rm \*out\*

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 27, 2007, 6:40am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/7 "2007-11-27T06:40:41Z")

</div>

> [@porter](#):
>
> /tmp is a shared directory, other people could have all kinds of stuff in there.

well.... . m not bothered about the directory.... i mentioned it as example.... !! 😃

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 27, 2007, 6:43am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/8 "2007-11-27T06:43:57Z")

</div>

> [@radoulov](#):
>
> ksh:
> 
> ```plaintext
> rm !(*test*)
> 
> ```
> 
> bash:
> 
> ```plaintext
> shopt extglob
> rm !(*test*)
> 
> ```
> 
> zsh (require setopt extendedglob):
> 
> ```plaintext
> rm *~*test*
> 
> ```
> 
> or:
> 
> ```plaintext
> rm ^*test*
> 
> ```
> 
> But be careful 🙂

none of them worked.... !!:mad:

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 27, 2007, 6:45am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/9 "2007-11-27T06:45:33Z")

</div>

> [@leonard](#):
>
> Try this
> 
> rm out?
> 
> rm \*out\*

thanks dude but that directory contains some files which might not have out in file name.. !!!

so i wanna delete all but \*temp\* files.... 😛

---

<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:** [November 27, 2007, 6:51am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/10 "2007-11-27T06:51:55Z")

</div>

> [@freakygs](#):
>
> none of them worked.... !!:mad:

And your shell is?

---

<div class="post-metadata">

**Author:** ![grial](https://community.unix.com/user_avatar/community.unix.com/grial/32/379_2.png) [@grial](https://community.unix.com/u/grial)\
**Post date:** [November 27, 2007, 7:30am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/11 "2007-11-27T07:30:06Z")

</div>

find /path/to/files -type f ! -name '\*test\*' -exec rm {} \;

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 28, 2007, 5:04am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/12 "2007-11-28T05:04:38Z")

</div>

> [@radoulov](#):
>
> And your shell is?

sh, ksh, bash

---

<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:** [November 28, 2007, 5:09am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/13 "2007-11-28T05:09:07Z")

</div>

> [@freakygs](#):
>
> sh, ksh, bash

Strange,  
could you post the output from your commands?

in ksh:

```plaintext
ls -l
ls -l !(*test*)

```

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/5_5575768a8748004e209b776fc1b2916d.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [November 28, 2007, 5:18am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/14 "2007-11-28T05:18:28Z")

</div>

> [@freakygs](#):
>
> none of them worked.... !!:mad:

Did you try my find|case|rm approach?

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 28, 2007, 5:19am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/15 "2007-11-28T05:19:45Z")

</div>

> [@radoulov](#):
>
> Strange,  
> could you post the output from your commands?
> 
> in ksh:
> 
> ```plaintext
> ls -l
> ls -l !(*test*)
> 
> ```

-----------------------  
$ ls  
g\_test1 test\_11.dat test\_3.dat test\_6.dat test\_9.csv test\_dd.dat test\_mrg1.dat  
test\_1.dat test\_11.txt test\_4.dat test\_7.dat test\_d.dat test\_dt.txt test\_script.ksh  
test\_10.csv test\_2.dat test\_5.dat test\_8.csv test\_d2.dat test\_mrg.dat  
$ ls -l !(\*test\*)  
ls: 0653-341 The file !(\*test\*) does not exist.  
-------------------------------------------------

---

<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:** [November 28, 2007, 5:23am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/16 "2007-11-28T05:23:26Z")

</div>

> [@freakygs](#):
>
> * * *
> 
> ## $ ls g\_test1 test\_11.dat test\_3.dat test\_6.dat test\_9.csv test\_dd.dat test\_mrg1.dat test\_1.dat test\_11.txt test\_4.dat test\_7.dat test\_d.dat test\_dt.txt test\_script.ksh test\_10.csv test\_2.dat test\_5.dat test\_8.csv test\_d2.dat test\_mrg.dat $ ls -l !(\*test\*) ls: 0653-341 The file !(\*test\*) does not exist.

EDIT:

The output is correct!  
You don't have files that don't have the "test" pattern in the name!

Could you post the ksh version also, thanks!

```plaintext
print $KSH_VERSION

```

or

```plaintext
print ${.sh.version}

```

---

<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:** [November 28, 2007, 5:30am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/17 "2007-11-28T05:30:02Z")

</div>

So:

```nohighlight
rm -f !(*test*)

```

Use -f to suppress error messages (I don't know if ksh has the nullglob option :)).

---

<div class="post-metadata">

**Author:** ![freakygs](https://community.unix.com/user_avatar/community.unix.com/freakygs/32/736_2.png) [@freakygs](https://community.unix.com/u/freakygs)\
**Post date:** [November 28, 2007, 5:34am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/18 "2007-11-28T05:34:16Z")

</div>

> [@radoulov](#):
>
> EDIT:
> 
> The output is correct!  
> You don't have files that don't have the "test" pattern in the name!
> 
> Could you post the ksh version also, thanks!
> 
> ```plaintext
> print $KSH_VERSION
> 
> ```
> 
> or
> 
> ```plaintext
> print ${.sh.version}
> 
> ```

both these cmds don't work on my machine

---

<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:** [November 28, 2007, 5:40am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/19 "2007-11-28T05:40:12Z")

</div>

> [@freakygs](#):
>
> both these cmds don't work on my machine

It's possible (you have ksh88 I suppose),  
but the globbing I suggested should work.  
You can always redirect/suppress the error messages  
when no match is found.

---

<div class="post-metadata">

**Author:** ![grial](https://community.unix.com/user_avatar/community.unix.com/grial/32/379_2.png) [@grial](https://community.unix.com/u/grial)\
**Post date:** [November 28, 2007, 6:05am UTC](https://community.unix.com/t/remove-files-other-than-selected/178043/20 "2007-11-28T06:05:39Z")

</div>

> [@grial](#):
>
> find /path/to/files -type f ! -name '\*test\*' -exec rm {} \;

This should work. Have you tryed it?

[Next page](https://community.unix.com/t/remove-files-other-than-selected/178043.md?page=2)
