LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2017, 03:00 AM   #1
scottmusician
Member
 
Registered: Jul 2011
Location: Melbourne, AU
Distribution: CentOS
Posts: 58

Rep: Reputation: Disabled
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
 
Old 06-22-2017, 03:19 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
1 members found this post helpful.
Old 06-22-2017, 04:34 AM   #3
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Quote:
Originally Posted by scottmusician View Post
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.

Last edited by aragorn2101; 06-22-2017 at 04:47 AM.
 
  


Reply

Tags
bash, rm, terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete all files in particular directory preetham.66666 Linux - Server 5 02-27-2015 10:52 AM
[SOLVED] Cannot delete a recently created directory in Lubuntu, or files in that directory l33y Linux - Newbie 26 06-07-2014 05:49 AM
How to extract all files on a directory and delete all files? moisespedro Slackware 9 01-21-2014 11:55 AM
Delete Files in directory villumanati Linux - General 7 12-30-2008 01:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration