LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-19-2011, 07:30 PM   #1
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Rep: Reputation: 15
How to delete files keeping the files listed in a text file -urgent


Hi

I could better explain here rather than the subject. ;-)

I have huge number of files under a directory in Linux. And i have listed them in a text file. But our aplications team exclude some files from the list and told us to delete all files except the files excluded in the text file.

in general, delete all files from the directory to keep the files listed in that text file.
 
Old 10-19-2011, 07:44 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Perhaps you could use 'comm -3 --check-order file1 file2' to create a list of files to be deleted.

Or, move the files to be kept to a temporary directory, and then move them back after deleting everything.
 
Old 10-19-2011, 10:28 PM   #3
Wellesly
LQ Newbie
 
Registered: Jan 2011
Distribution: Arch Linux
Posts: 16

Rep: Reputation: 6
To delete all of the files listed in the text file, the following command works.

Code:
for file in `xargs < file-list.txt`; do rm /path/to/files/$file; done
This assumes that there are no spaces in the file names, and it assumes that the excluded files are not listed in this text file.

Last edited by Wellesly; 10-19-2011 at 10:30 PM.
 
Old 10-20-2011, 06:55 PM   #4
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Hi Allend

thanks for your thought. It might be a way, not simple but workable.

I've got the solution, but if any intelligent idea i'will remain open this thread.
 
0 members found this post helpful.
Old 10-21-2011, 11:28 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by jeesun View Post
Hi Allend
thanks for your thought. It might be a way, not simple but workable. I've got the solution, but if any intelligent idea i'will remain open this thread.
That's actually a bit insulting.

You were given three ideas on how to do this task, but are keeping the thread open for 'intelligent' ones?? How about posting your intelligent solution, since you say you came up with one??
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to zip files that are listed in text file? mgltacoma Linux - Newbie 10 10-06-2011 09:30 AM
Thunar File Manager shows text files as Adobe bridge workspace files bret381 Linux - Software 6 01-01-2011 09:23 AM
tar the files listed in other file PMP Linux - Newbie 1 04-21-2009 08:50 AM
Bash script to delete folder's that are listed in a text file Bone11409 Programming 26 01-16-2009 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:06 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