I have a lot of files and folders in a specific folder and I want to delete all of them; however, I wanted to keep files X, Y, and Z.
Is there a way I can do something like: rm * | but NOT grep | X or Y or Z
|
I have a lot of files and folders in a specific folder and I want to delete all of them; however, I wanted to keep files X, Y, and Z. Is there a way I can do something like: |
||||
|
|
Instead of using rm, it may be easier to use find. A command like this would delete everything except a file named exactly 'file'
Many versions of should be able to support globbing and regular expression matching. You could also pipe the output of find to rm as well
|
|||||
|
|
Using zsh, with setopt EXTENDED_GLOB
But, you should probably instead move the files elsewhere, then delete everything. It's far safer in terms of finger slips, such as hitting enter too soon. |
|||
|
|
|
Later versions of bash have the
To test, I suggest you first replace |
|||
|
|
Attention: Run the command and if the files to be deleted are the right ones, run it again and delete the hash character "#". If the filenames are more complicated then that, do
Again, first look at the results then remove the hash sign. This version - as suggested in the comments - saves some characters and looks a bit clearer.
|
|||||
|
|
Move the files you want to keep away. Go up one level, delete the folder. Re-create the folder and move those files back. |
|||||||||||
|
|
|||||
|