LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to delete all files in a directory EXCEPT some specified exclusions? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-all-files-in-a-directory-except-some-specified-exclusions-4175608373/)

scottmusician 06-22-2017 03:00 AM

how to delete all files in a directory EXCEPT some specified exclusions?
 
Hi everybody,

how can I delete all files in a directory that DON'T match an exclusion list?

For example, I have a file called "exclusions.txt", containing

Code:

File1
File3
File7

My aim is to delete File2, File 4, File5 and File 6 using a single command.

Any thoughts would be great! Thank you again :)

AwesomeMachine 06-22-2017 03:19 AM

If you read the "man" page for "rm" there is a way to read a list from a file. But you would need to invert the exclusions list into a delete list.

aragorn2101 06-22-2017 04:34 AM

Quote:

Originally Posted by scottmusician (Post 5725504)
My aim is to delete File2, File 4, File5 and File 6 using a single command.

I suggest you make a delete.list file looking like this:
Code:

File2
File4
File5
File6

Then you do:
Code:

cat delete.list | xargs rm -v
If you have a large list of files to delete and only few exclusions, you can create the file with
Code:

ls -1 > delete.list
and then edit the file to remove your exclusions.


All times are GMT -5. The time now is 12:49 AM.