59

Here is the situation.

I have a directory which contains many files with different extensions. I want to delete all files except one with a specific name.

This could be easily done using the GUI by selecting all and pressing ctrl and deselecting the file in question.

That is exactly what I want to, but how can I do it from the command line?

For example: dirA contains the following files:

a.txt
b.txt
c.php
d.html
a.db
b.db
e.html

I want to delete all files keeping only the file named a.txt.

3
  • 1
    I think this might explain. ;)
    – JoKeR
    May 16, 2015 at 12:20
  • I've come to this before, but this needs more work
    – Maythux
    May 16, 2015 at 12:21
  • These questions are confusing me, not to mention they have no idea how many files are on your system with . Or .txt, use this command simple to use and easy to understand rm -riv ~/Desktop/path/* * = all in folder , r = recursive , v = verbose , i = prompt you before deleting
    – user610658
    Apr 9, 2017 at 23:56

5 Answers 5

99

I've come with this easy simple great command:

rm !(a.txt)

you can use ! as a negation

Test the glob with echo first i.e.

echo !(a.txt)

If it doesn't work, for bash you may need to enable this with

shopt -s extglob

If you wanted to keep both a.txt and b.txt, you can use !(a.txt|b.txt) or !([ab].txt).

Edit:

to make rm working recursively just add -r like

rm -r !(a.txt)

and also, it is working with folder. just need to change the name to the dir name, such as for a_dir

rm -r !(a_dir)
10
  • I never see such thing before now, I discover it by change lol. Commands are magical
    – Maythux
    May 16, 2015 at 12:26
  • 1
    What shell does this work on? Does it work on ubuntu's default bash? EDIT: nevermind, found the info, proposed an edit.
    – Random832
    May 16, 2015 at 15:19
  • 3
    Seems to be dependent on ksh. Bash doesn't like it.
    – Joshua
    May 17, 2015 at 16:16
  • 1
    @PeterMortensen, you can add -r to rm command like this rm -r !(a.txt), it will work recursively
    – lhrec_106
    Jun 9, 2016 at 6:12
  • 1
    For me running this on mac it returns -bash: !: event not found
    – Jim
    Mar 17, 2017 at 18:57
23

You can try this command:

find . \! -name 'a.txt' -delete

But you need be careful because find command is recursive.

1
  • 7
    You can prevent find from being recursive with a -maxdepth 1
    – evilsoup
    May 16, 2015 at 16:57
12

You can do this in terminal:

cd dirA 
export GLOBIGNORE=a.txt
rm *
export GLOBIGNORE=
1
  • Does not work if you set full path Nov 24, 2015 at 9:24
5

Use find and xargs

find folder -type f -not -name 'a.txt' -print0 | xargs -0 rm

To exclude multiple things:

find folder -type f -not -name 'a.txt' -not -name 'b.txt' -print0 | xargs -0 rm

This also works with wildcards:

find folder -type f -not -name '*.png' -not -name 'b.txt' -print0  | xargs -0 rm

To search in the current folder, use . in place of 'folder'.

Base source

2
  • Probably will be good for portability =) +1 May 16, 2015 at 13:42
  • 2
    Note that this will fail if your file names contain whitespace or newlines or glob characters etc. Use find . -type f -not -name 'a.txt' -delete instead.
    – terdon
    May 16, 2015 at 13:45
5

You can use the command :

find ! -name 'a.txt' -type f -exec rm -f {} +

This will look for files (-type f) in the current directory except for file a.txt (! -name 'a.txt) and then will remove them (-exec rm -f {} +)

3
  • But this would not remove the directories if exist?!
    – Maythux
    May 21, 2015 at 16:36
  • @Maythux Yes, the author asks how to remove files, not folders.
    – Sh1d0w
    May 21, 2015 at 16:50
  • -exec rm -f {} + doesn't really have any advantages over -delete. Sep 13, 2017 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.