Monday, June 27, 2016

Erase all files/directories except some of them

To erase all directories except one, execute the following:
$ find . -maxdepth 1 -not -name [file/directory_name] -exec rm -R {} \;
Where:
-maxdepth: prevents to erase recursively inside the directory to keep.

If you want to be sure that the correct files will be removed, run first the beginning part of the command(before "-exec") and when you are sure, execute the complete command:
$ find . -maxdepth 1 -not -name [file/directory_name]
If you want to keep more than one file/directory, execute:
$ find . -maxdepth 1 -not -name [file_1/directory_name_1] \
-and -not -name [file_2/directory_name_2] -exec rm -R {} \;
Where:
-and: is the logical operand and.

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.