LinuxQuestions.org
Register a domain and help support LQ
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 04-19-2010, 12:46 PM   #1
GeorgeWang
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Rep: Reputation: 0
Delete the files except the files in a list


Hello everyone,

I want to delete all files in a directory which are older than certain days (I can get it done using grep with -mtime), BUT except the files listed in a temporary file, /tmp/filesnotdelte.txt.

For example,
under directory mydir, there are files older than 10 days, filename1, filename5, filename6, filename10.
in /tmp/filesnotdelete.txt, there are two entries at this time, filename1 and filename6.
looking for a simple way to delete files which are not listed in /tmp/filesnotdelete.txt, at this time, only filename5, filename10 should be deleted. NOTE, the entry in the temporary file is NOT always same.

Thanks in advance!!!
 
Old 04-19-2010, 01:16 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by GeorgeWang View Post
Hello everyone,

I want to delete all files in a directory which are older than certain days (I can get it done using grep with -mtime), BUT except the files listed in a temporary file, /tmp/filesnotdelte.txt.

For example,
under directory mydir, there are files older than 10 days, filename1, filename5, filename6, filename10.
in /tmp/filesnotdelete.txt, there are two entries at this time, filename1 and filename6.
looking for a simple way to delete files which are not listed in /tmp/filesnotdelete.txt, at this time, only filename5, filename10 should be deleted. NOTE, the entry in the temporary file is NOT always same.

Thanks in advance!!!
Well, without details of the script, I'd say grab each file name before you delete it (getting a directory listing into an array springs to mind), then grep for each array element in the do not delete list. If you find it, go to the next array element. If you don't, then delete it. Fairly simple to do.
 
Old 04-20-2010, 03:50 PM   #3
GeorgeWang
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Hello TB0ne,
Thank you very much for your reply!

I am looking for one command to achieve it if it is possible.

I can delete all files except *.c and *.h in my current directory using this command:
$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print0 | xargs -0 /bin/rm -f

Just wanted to know if where is a way to use one command to delete all files except the ones listed in a temporary file, OR I have to use array to approach it?

Thanks again!
 
Old 04-20-2010, 03:55 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by GeorgeWang View Post
Hello TB0ne,
Thank you very much for your reply!

I am looking for one command to achieve it if it is possible.

I can delete all files except *.c and *.h in my current directory using this command:
$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print0 | xargs -0 /bin/rm -f

Just wanted to know if where is a way to use one command to delete all files except the ones listed in a temporary file, OR I have to use array to approach it?

Thanks again!
Well, as I said in my first post, I'd use an array. There may be a way to use a single command-line to do it, but I don't know how, or what that would be.
 
Old 04-20-2010, 04:16 PM   #5
fbsduser
Member
 
Registered: Oct 2009
Distribution: Hackintosh, SlackWare
Posts: 267

Rep: Reputation: 30
It could be done with a for loop and an if instruction inside the script.
 
Old 04-20-2010, 04:53 PM   #6
GeorgeWang
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you fbsduser!

Could you please provide me the sample code, I am not familiar with shell script.

Thanks again for your help!
 
Old 04-20-2010, 05:08 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by GeorgeWang View Post
Thank you fbsduser!
Could you please provide me the sample code, I am not familiar with shell script.

Thanks again for your help!
Well, what have you tried? As a rule, we're always glad to HELP, but we're not going to write your scripts for you. Have you tried to look at any of the bash script tutorials?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
need to rsync only selected files (--files-from) also need to delete files on dest. ? BrianK Linux - General 5 10-22-2009 10:52 PM
LXer: fdupes - Command line tool to find and list/delete duplicate files LXer Syndicated Linux News 0 10-28-2008 04:40 PM
How to delete list of files Nana Linux - Newbie 9 03-09-2007 03:40 AM
List of arguments too long, need to delete 59,000 files stefaandk Linux - General 4 07-12-2006 03:14 AM
can't access/delete/list files even as root... tenshi Linux - General 7 10-13-2004 05:29 AM


All times are GMT -5. The time now is 03:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration