How to delete all but one(or some) file in Unix ?
Something like
rm -rf -ignore myfile.txt *
|
feedback
|
This question came from our site for professional and enthusiast programmers.
Example :
To remove all files except 'test2' :
Then 'ls' output is :
EDIT: Thanks for the comment. If the directory contains some files with spaces :
You can use (with bash) :
'ls' output :
| |||||||
feedback
|
|
Assuming you're using the bash shell (the most common case), you can use the negation globbing (pathname expansion) symbol:
This uses extended globbing, so you would need to enable this first:
| |||
|
feedback
|
|
This page gives a variety of options depending on the shell: http://www.unix.com/unix-dummies-questions-answers/51400-how-remove-all-except-one-file.html | |||
|
feedback
|
|
For a recursive | |||
|
feedback
|
rm -rf, is it possible to exclude certain subdirectories? – Tom Wijsman Mar 11 at 12:30