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?
- 103
- 4
- 1,459
17 Answers
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
-
1
-
19You need to
shopt -s extglob, @Ashot. Also, it's just files, not directories, which is why I've removed the-rfoptions in your command. – slhck Jan 08 '13 at 13:07 -
4If you need to exclude one file of a selection of files, try this:
rm !(index).html. This will delete all files ending in ".html" with the exception of "index.html". – mzuther Jul 24 '15 at 21:46 -
1
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.
- 87,539
- 63
- 263
- 308
-
26I guess this works only as long as the list of files which
*expands too isn't getting too long. :-} – Frerich Raabe Jan 08 '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 08 '13 at 17:00
-
11
-
-
2@SantoshKumar: That doesn't make sense to me. The expansion will always work, it doesn't depend on what command you want to use afterwards. – Oliver Salzburg Jan 17 '13 at 18:18
-
2@OliverSalzburg Sorry, the combination is little bit confusing. I think you should write like
Ctrl+Shift+x+*– Santosh Kumar Jan 18 '13 at 05:22 -
3
-
1Works in practice but can't be scripted. I think the find solution is the best. – flungo May 10 '15 at 14:10
-
@Oliver Salzburg, the combination is not working on my Korn shell , what I might be doing wrong could you please advise? solution from slowpoision , esc followed by * worked though. Thanks – Forever Learner Feb 02 '18 at 11:22
You can use find
find . ! -name u ! -name p -maxdepth 1 -type f -delete
!negates the next expression-namespecifies a filename-maxdepth 1will make find process the specified directory only (findby default traverses directories)-type fwill process only files (and not for example directories)-deletewill 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
-printinstead of-deletefind . ! -name u ! -name p -maxdepth 1 -type f -print
- 107
- 7,847
-
8order of predicates is critical here. If one put
-deletejust after.it will be disaster (will delete all files in CWD) – Michał Šrajer Jan 08 '13 at 17:34 -
This could be written more compactly as
find . -maxdepth 1 -type f -name '[^up]' -delete– kojiro Jan 08 '13 at 18:09 -
5@kojiro yes but only for files that are just one letter. With more complex names the regex could be a mess. – Matteo Jan 08 '13 at 20:15
-
3
findis my best friend, especially when there are too many files to glob – Terence Johnson Jan 09 '13 at 22:36 -
-
1
Simple:
mv the files you want in a upper directory, rm the directory and then mv them back.
- 617
-
13Offcourse, mv them to a directory higher. Try not to mv them to a subdirectory you are deleting... – Konerak Jan 08 '13 at 19:21
-
1
-
12This will overwrite files with the same name in the destination directory – Matteo Jan 09 '13 at 09:29
-
11I 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
-
1Also you'd need write access on the parent directory, which you likely not have on a shared web server. – Jan Heinrich Reimer Apr 04 '20 at 20:57
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]
-
2This works for the 1-char filenames. For longer names, sparkie's answer is better. – glenn jackman Jan 08 '13 at 15:31
-
3
-
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 09 '13 at 09:07
-
4@HobbesofCalvin That would delete all files not beginning with u or p, not those beginning with them. – rjmunro Jan 09 '13 at 10:48
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.
-
1grep will not handle extended regexpt by default: either use
-Eoregrep– Matteo Jan 09 '13 at 09:28 -
2
-
1@Matteo No it won't. The grep isn't grepping the files, it's grepping the output of the ls command. You're thinking of something like
grep -L (u|p)' * | xargs rmwhere-Lmeans list filenames not containing a match. – rjmunro Jan 09 '13 at 10:52 -
@rjmunro Yes it will:
touch u uu; ls | egrep -v '(u|p)'gives an empty ouput – Matteo Jan 09 '13 at 12:28 -
5Oh, you mean any file who's name contains
uorp, not any file containing auor ap. That is correct. You can fix by usingegrep -v '^(u|p)$'– rjmunro Jan 09 '13 at 12:40 -
1Here is remove everything except these matches!
ls | grep -v 'vuze\|progs' | xargs rm -rf– Nick Sep 25 '14 at 12:32
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 ~.
- 193
GLOBIGNORE takes a colon-separated list
GLOBIGNORE=u:p
rm *
- 3,492
- 1
- 19
- 17
-
14This 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
rmor in a testing directory! – user Jan 08 '13 at 20:09
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.
- 307
-
2It reminded me the same thing. But temporarily moving out of folder may not be a good idea in the era of multitasking :) – Sedat Kapanoglu Jan 09 '13 at 09:18
find . -maxdepth 1 ! -name "u" ! -name "p" -type f -exec rm -rf {} \;
This will delete all files except u and p in unix
- 26,344
- 131
- 1
Use:
find . -type f ! -name 'u' ! -name 'p' ! -name '*.ext' -delete
find . -type d ! -name 'u' ! -name 'p' ! -name '*.ext' -delete
in order to delete all files including directories, except u, p and .ext files.
- 21
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
-
You need to specify a directory before the expression (`find . -regextype ...). – Matteo Jan 09 '13 at 09:25
-
-
no - my find version (debian squeeze) does definitively not require an explicit directory before the expression if the current directory should be used – sparkie Jan 09 '13 at 09:44
-
-
@sparkie: not defining the directory (first parameter) for
findis a GNU extension offindcommand. The same applies for-regextypeoption. In addition, your command will delete files in subdirectories, too, whereas the original question clearly asked about files in a directory. – Mikko Rantalainen Sep 04 '15 at 05:28
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.
- 241
- 2
- 5
Here's another variant. You can type:
rm -i *
or:
rm --interactive *
So rm will ask you to confirm deleting of each file.
- 21
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]*
- 920
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.
- 111
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
- 121
-
1It would be better to use
rmdir tmpin that last line instead ofrm -r tmp-- that way, if previous command failed for any reason or was mistyped, you won't lose all your data. – Matija Nalis Jun 04 '23 at 23:17
shopt -s dotglobbefore runningrm (...). – Jan 28 '13 at 08:07