LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete the files except the files in a list (https://www.linuxquestions.org/questions/linux-newbie-8/delete-the-files-except-the-files-in-a-list-802884/)

GeorgeWang 04-19-2010 11:46 AM

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!!!

TB0ne 04-19-2010 12:16 PM

Quote:

Originally Posted by GeorgeWang (Post 3940472)
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.

GeorgeWang 04-20-2010 02:50 PM

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!

TB0ne 04-20-2010 02:55 PM

Quote:

Originally Posted by GeorgeWang (Post 3941782)
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.

fbsduser 04-20-2010 03:16 PM

It could be done with a for loop and an if instruction inside the script.

GeorgeWang 04-20-2010 03:53 PM

Thank you fbsduser!

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

Thanks again for your help!

TB0ne 04-20-2010 04:08 PM

Quote:

Originally Posted by GeorgeWang (Post 3941843)
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?


All times are GMT -5. The time now is 06:56 PM.