I have a directory ( ~/temp/ ) that contains many files and subdirectories, and in some directories they may contain other files and subdirectories. In addition, in the directory ( ~/temp/ ) it contains a special txt file called kept.txt , it lists some direct files and subdirectories that are contained in ~/temp/ , now I want to delete all other files and directories in ~/temp/ , which are not specified in the file kept.txt , how to do this using the shell command, the easier.
eg.
I like the catalog as shown below:
$ tree temp/ -F temp/ βββ a/ βββ b/ βββ c/ β βββ f2.txt β βββ z/ βββ f1.txt βββ kept.txt
Content kept.txt :
$ more kept.txt b kept.txt
In this case:
- I want to remove
a/ , c/ and f1.txt . For c/ the directory itself and all sub content (files and directories) will be deleted. - In
kept.txt format is one item (file or directory) per line.
source share