Delete old files but with exclusion with file list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete old files but with exclusion with file list
# 1  
Old 02-05-2010
Question Delete old files but with exclusion with file list

Hello Can you please help and check what im missing on script below
the goal is to delete the old files more than 7 days old but not the excluded file list inside excluded.dat file

Code:
#!/bin/sh
EXCLUDE=/path/to/exclude/exclude.dat
FIND=/bin/find
for xfile in '(read $EXCLUDE)'
do
$FIND /path/to/fordelete -type f -mtime +7 | 'grep -i $xfile' | xargs rm-rf
done


Last edited by angst_nu; 02-05-2010 at 08:33 AM..
# 2  
Old 02-05-2010
Strangely this script contains many errors which are not in your previous posts.
What is the format of the file exclude.dat ? The script is easier if exclude.dat contains full path names exactly as would be produced by find.
What Operating System are you using? The syntax for "find" "grep" and shell varies.
# 3  
Old 02-05-2010
Hi methyl,

thanks for you reply.

im using this script on sun solaris 10.

this the exclude.dat

Code:
bash-3.00# cat exclude.dat 
exclude1.ex
exclude2.ex
exclude3.ex
exclude4.ex
exclude5.ex
bash-3.00#

regards,

Last edited by pludi; 02-05-2010 at 09:17 AM.. Reason: code tags, please...
# 4  
Old 02-05-2010
Try,

Code:
$ find /path/to/fordelete -type f -mtime +7 | grep -v "$(cat /path/to/exclude/exclude.dat)" | xargs rm -f

# 5  
Old 02-05-2010
@agn
I don't think that your "grep -v" works if file "exclude.dat" contains more than one line.

@angst_nu
The script becomes too complex to write to commerical quality if the exclude.dat file does not contain full path names. One can then use "grep -x" for an exact match to avoid accidental matches (e.g accidentally matching "foo" in "foobar").

Unless this is coursework it could be worth changing the design to suite the tools.
# 6  
Old 02-05-2010
@agn does not work for me.


Code:
$ find /path/to/fordelete -type f -mtime +7 | grep -v "$(cat /path/to/exclude/exclude.dat)" | xargs rm -f

grep -x is not supported on sun solaris 10?

grep options
grep -hblcnsviw
# 7  
Old 02-05-2010
Solaris 10
/usr/xpg4/bin/grep
You may be able to use the "-F" option with "-f pattern_file" to process your list. There was a post on this board recently using that method.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List and Delete Files which are older than 7 days, but have white spaces in file name

I need to list and delete all files in current older which are olderthan 7 days. But my file names have white spaces. Before deleting I want to list all the files, so that I can verify.find . -type f -mtime +7 | xargs ls -l {} But the ls command is the working on the files which have white... (16 Replies)
Discussion started by: karumudi7
16 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. UNIX for Advanced & Expert Users

Help with a shell script? List files, delete them and log them

Hello, i'm trying to solve this script. List, one at a time, all files larger than 100K in the /home/username directory tree. Give the user the option to delete or compress the file, then proceed to show the next one. Write to a logfile the names of all deleted files and the deletion times. I... (7 Replies)
Discussion started by: jose2802
7 Replies

4. Shell Programming and Scripting

Pattern exclusion between two files

I have a large file (file1) that has 96770154 lines and a smaller file (file2) that has 3 lines. I want to remove all of the strings from file1 that occur in file2. file1 looks like this: DOGDOGNODOGTESTCAT CATHELLOBYEEBYEFAT CATCATDOGDOGCATYESGOOD file2 looks like this: YES... (10 Replies)
Discussion started by: verse123
10 Replies

5. UNIX for Dummies Questions & Answers

Delete from supplied list of files

Hi, I have a file that contains >4000 inode numbers. Each inode number is on separate row. I'd like to delete all this files. Any idea how could I do it? Thank you. Andrej (3 Replies)
Discussion started by: andrejm
3 Replies

6. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

7. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

8. UNIX for Dummies Questions & Answers

Searching a file with exclusion in the search

Hello, I'm looking for a bit of help. Im trying to search a file for lines that contain white spaces at the end of the lines. This is what I'm using where $param is the path and file name and it redirects the output to a txt file : echo | grep -n ' $' $param >> $2 Is it possible to have... (8 Replies)
Discussion started by: gintreach
8 Replies

9. AIX

Get the list, filter and delete the files on remote server

Hi, I need to login to a remote server. Go to a particular path. Get the lists of files on that path.There may be n number of files. I need to delete only those files from above created list which are 7 days older. I have achieved above using ftp protocol, but now the constraint has... (0 Replies)
Discussion started by: mail_amitnagpal
0 Replies

10. Shell Programming and Scripting

Exclusion List of file and directory

dear all i trying to list all files within a directory. I want to exclude all subdirectory and some files, with using below statement, but it not exclude the files which start with "&" and end with "SL" , is there any things wrong with the below statement ? TIA cd /myaccount/mydirectory... (6 Replies)
Discussion started by: ayang
6 Replies
Login or Register to Ask a Question