Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to delete all files in a directory, but exclude some of them. For example, in a directory with the files a b c ... z, I need to delete all except for u and p. Is there an easy way to do this?

share|improve this question

16 Answers 16

up vote 85 down vote accepted

What I do in those cases is to type

rm *

Then I press Ctrl+X,* to expand * into all visible file names.

Then I can just remove the two files I like to keep from the list and finally execute the command line.

share|improve this answer
18  
I guess this works only as long as the list of files which * expands too isn't getting too long. :-} –  Frerich Raabe Jan 8 '13 at 16:57
1  
@FrerichRaabe: Indeed. If it's too long a different approach will be required. Luckily we now have a list of great options :) –  Oliver Salzburg Jan 8 '13 at 17:00
5  
Esc followed by * will also expand the "*". –  slowpoison Jan 9 '13 at 20:45
1  
@OliverSalzburg Sorry, the combination is little bit confusing. I think you should write like Ctrl + Shift + x + * –  Santosh Kumar Jan 18 '13 at 5:22

Here's another variant. You can type:

rm -i *

or:

rm --interactive *

So rm will ask you to confirm deleting of each file.

share|improve this answer

Yet another:

for FILE in ./*; do if [[ $FILE != ./u* ]] || [[ $FILE != ./p* ]];then rm $FILE; fi; done;

It's kind of lengthy and I don't know if you could easily make it into an function that could easily accommodate and arbitrary number of arguments, but it works well.

And it's pure bash goodness.

share|improve this answer

You can use find

find . ! -name u ! -name p -maxdepth 1 -type f -delete
  • ! negates the next expression
  • -name specifies a filename
  • -maxdepth 1 will make find process the specified directory only (find by default traverses directories)
  • -type f will process only files (and not for example directories)
  • -delete will delete the files

You can then tune the conditions looking at the man page of find

Update

  • Keep in mind that the order of the elements of the expressions is significant (see the documentation)
  • Test your command first by using -print instead of -delete

    find . ! -name u ! -name p -maxdepth 1 -type f -print
    
share|improve this answer
3  
order of predicates is critical here. If one put -delete just after . it will be disaster (will delete all files in CWD) –  Michał Šrajer Jan 8 '13 at 17:34
4  
@kojiro yes but only for files that are just one letter. With more complex names the regex could be a mess. –  Matteo Jan 8 '13 at 20:15

A simple way that is hard to mess up: let's say you want to delete everything except *.pdf:

mkdir tmp
mv *.pdf tmp
rm *
mv tmp/* .
rm -r tmp
share|improve this answer

Yet another version using xargs:

ls -1 | grep -v do_not_delete | xargs -I files rm "files"

Note that xargs -I is needed to handle filenames including spaces correctly.

share|improve this answer

Doing it without find:

ls | grep -v '(u|p)' | xargs rm

(Edit: "u" and "v", as in other places here, are being used as generic versions of entire regexes. Obviously you'll want to be careful to anchor your regexes to avoid matching too many things.)

You're definitely going to want a script if you're going to be doing much of this, as others have suggested.

share|improve this answer
2  
this will exclude any file containing a u or a p –  Matteo Jan 9 '13 at 9:28
3  
Oh, you mean any file who's name contains u or p, not any file containing a u or a p. That is correct. You can fix by using egrep -v '^(u|p)$' –  rjmunro Jan 9 '13 at 12:40

In zsh:

setopt extended_glob  # probably in your .zshrc

then

rm ^(u|p)

or

rm *~(u|p)

The second will work even if you have ^ in $histchars for history substitution, and of course you can put an arbitrary glob before the ~.

share|improve this answer

I always use:

rm [a-o,q-t,v-z]*

This will allow you to define how granular you want to make it. So if you want to delete a through o and Z files you can use:

rm [a-o,z]*
share|improve this answer

For those preferring to specify arbitrary complex exclude patterns (spanning all affected filenames) in a full blown regexp emacs, posix-awk or posix-extended style (see find man page) I would recommend this one. It excludes u and p in current dir in this example. This may be handy for scripts.

find -regextype posix-awk ! -regex './(u|p)' -print0 | xargs -0 rm -rf
share|improve this answer

Simple:

mv the files you want in a upper directory, rm the directory and then mv them back.

share|improve this answer
12  
Offcourse, mv them to a directory higher. Try not to mv them to a subdirectory you are deleting... –  Konerak Jan 8 '13 at 19:21
10  
This will overwrite files with the same name in the destination directory –  Matteo Jan 9 '13 at 9:29
4  
I am downvoting this because while it can be handy, it also is non-atomic and effectively removes all files from the directory during a short period of time; this would not be acceptable if, for instance, the files are being shared on the network. –  Sam Hocevar Jan 10 '13 at 12:19
 find . -maxdepth 1 ! -name "u" ! -name "p" -type f -exec rm -rf {} \;

This will delete all files except u and p in unix

share|improve this answer

Back in the floppy era I had a dos executable called "Except" that would move things out of the current directory temporarially and execute a command, so you could say:

except *.txt del *.*

to delete everything but your text files.

This would be a pretty trivial thing to implement as a shell script and if this is the kind of thing you are likely to do more than twice it seems like it would be a good idea.

share|improve this answer
2  
It reminded me the same thing. But temporarily moving out of folder may not be a good idea in the era of multitasking :) –  ssg Jan 9 '13 at 9:18

GLOBIGNORE takes a colon-separated list

GLOBIGNORE=u:p
rm *
share|improve this answer
12  
This does not work on my shell (GNU bash 4.1.5(1)). Be sure to test it first with something a little less harmful than rm or in a testing directory! –  Michael Kjörling Jan 8 '13 at 20:09

Somewhat similar to this answer but no special options are needed, as far as I know the following is "ancient" functionality supported by any (vaguely) /bin/sh resembling shell (e.g. bash, zsh, ksh, etc)

rm [^up]
share|improve this answer
2  
This works for the 1-char filenames. For longer names, sparkie's answer is better. –  glenn jackman Jan 8 '13 at 15:31
3  
What would be wrong with rm [^up]*? I do similar things rather often. –  Michael Kjörling Jan 8 '13 at 15:33
3  
@MichaelKjörling - this would delete all files beginning with either u or p, not just those with the names u and p. I think the OP (@Ashot) meant the a-z and u,p,etc. symbolically and not literally. –  Sudipta Chatterjee Jan 9 '13 at 9:07
4  
@HobbesofCalvin That would delete all files not beginning with u or p, not those beginning with them. –  rjmunro Jan 9 '13 at 10:48

To rm all but u,p in bash just type:

rm !(u|p)

This requires the following option to be set:

shopt -s extglob

See more: glob - Greg's Wiki

share|improve this answer
1  
you must have 'extglobbing' active: shopt -s extglob –  sparkie Jan 8 '13 at 13:07
14  
You need to shopt -s extglob, @Ashot. Also, it's just files, not directories, which is why I've removed the -rf options in your command. –  slhck Jan 8 '13 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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