Sponsored Content
Top Forums Shell Programming and Scripting Help needed with delete script. Post 302768281 by MadeInGermany on Friday 8th of February 2013 08:41:23 AM
Old 02-08-2013
We are running the following cleantmp.sh in production environment:

Code:
#!/bin/sh

PATH=/usr/bin:/bin:/usr/sbin
export PATH

max_days=30
case $1 in
[0-9]*)
 max_days=$1
 shift
 ;;
esac

dirs_to_clean=${@:-/tmp}

owner_to_keep="root nobody"

omit=""
for i in $owner_to_keep
do
 omit="$omit ( ! -user $i )"
done

for dir in $dirs_to_clean
do
 [ -d $dir ] &&
 cd $dir &&
 find . -depth \! -type d \( -mtime +$max_days -o -mtime -0 \) \( -atime +$max_days -ctime +5 -o -ctime +$max_days -atime +5 -o -type l \) $omit -exec rm -f {} \; -o -type d -empty $omit -mtime +$max_days -exec rmdir {} \; 2>/dev/null
 sleep 1
done

exit 0
# We assume that a full backup (where atime or ctime changes) happens less often than every 5 days.
# We cd first, so there is a good chance that a too long directory path can be accessed
# if -empty is unsupported, replace it by -links 2
# A just emptied directory is deleted immediately or after another max_days
# -mtime -0 detects files with a future time stamp (were extracted from an obscure archive)

A nightly root crontab entry (crontab -e; crontab -l) could be
Code:
59 23 * * * /path/to/cleantmp.sh 10 /path/to/clean

You can add more /2ndpath/to/clean /3rdpath/to/clean ...
Nightly means, the remaining files are never older than 10+2=12 days.
For safety, frequently read files are not deleted.

Last edited by MadeInGermany; 02-08-2013 at 11:21 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

2. UNIX for Dummies Questions & Answers

Urgent help needed to delete some text without opening the file in unix

Hi To delete some text in 2 files in line1 ( not complete line) in unix without opening the files. For example: source file is like this <?xml version="1.0"... (5 Replies)
Discussion started by: pyaranoid
5 Replies

3. Shell Programming and Scripting

Need to delete the latest two files..Help needed

Suppose I have a directory called jeet and inside that directory so many files will be there.... Example: /abc/xyz/jeet $ ls -ltr total 0 -rw-r--r-- 1 oracle dba 0 Jan 13 11:36 naresh -rw-r--r-- 1 oracle dba 0 Jan 13 11:36 sreeni -rw-r--r-- 1 oracle dba ... (1 Reply)
Discussion started by: satyajit007
1 Replies

4. UNIX for Dummies Questions & Answers

Cron to delete email help needed

I would like to know if I can place a cron job (and what it might be of course) to delete all the mail in an inbox? Here are the servers specs: Operating systemLinuxService StatusClick to ViewKernel version2.6.28.9Machine Typei686Apache version2.2.11 (Unix)PERL version5.8.8Path to... (2 Replies)
Discussion started by: markmatu
2 Replies

5. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

6. UNIX for Dummies Questions & Answers

help in script needed

Hi, I am developing a script for writing out the failed jobs in file, 1)First step i need to create an empty file Status with today's date. 2)After that i need to execute the below command: dsjob -server servername -user uname -password pwd -ljobs projectname This will listout all... (5 Replies)
Discussion started by: pandeesh
5 Replies

7. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

8. Shell Programming and Scripting

Help needed for script

Hi, I have a big list like this --> 3285 3289 328D 3291 3295 3299 329D 32A1 I need to make it like --> 3285|3289|328D|3291|3295|3299|329D|32A1 Please suggest. This is Linux OS. (8 Replies)
Discussion started by: solaris_1977
8 Replies

9. 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

10. Shell Programming and Scripting

Sh script help needed

Can anyone tell me what this would do in an .sh script @@ -912,10 +912,12 @@ Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: samd12
3 Replies
TMPWATCH(8)						   System Administrator's Manual					       TMPWATCH(8)

NAME
tmpwatch - removes files which haven't been accessed for a period of time SYNOPSIS
tmpwatch [-u|-m|-c] [-MUXadfqstvx] [--verbose] [--force] [--all] [--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet] [--atime|--mtime|--ctime] [--dirmtime] [--exclude path] [--exclude-user user] [--exclude-pattern pattern] time dirs DESCRIPTION
tmpwatch recursively removes files which haven't been accessed for a given time. Normally, it's used to clean up directories which are used for temporary holding space such as /tmp. When changing directories, tmpwatch is very sensitive to possible race conditions and will exit with an error if one is detected. It does not follow symbolic links in the directories it's cleaning (even if a symbolic link is given as its argument), does not switch filesystems (including non-trivial bind mounts), skips lost+found directories owned by the root user, and only removes empty directories, regular files, symbolic links, and on some systems also unused sockets. By default, tmpwatch dates files by their atime (access time), not their mtime (modification time). If files aren't being removed when ls -l implies they should be, use ls -u to examine their atime to see if that explains the problem. If the --atime, --ctime or --mtime options are used in combination, the decision about deleting a file will be based on the maximum of these times. The --dirmtime option implies ignoring atime of directories, even if the --atime option is used. The time parameter defines the threshold for removing files. If the file has not been accessed for time, the file is removed. The time argument is a number with an optional single-character suffix specifying the units: m for minutes, h for hours, d for days. If no suffix is specified, time is in hours. Following this, one or more directories may be given for tmpwatch to clean up. OPTIONS
-u, --atime Make the decision about deleting a file based on the file's atime (access time). This is the default. Note that the periodic updatedb file system scans keep the atime of directories recent. -m, --mtime Make the decision about deleting a file based on the file's mtime (modification time) instead of the atime. -c, --ctime Make the decision about deleting a file based on the file's ctime (inode change time) instead of the atime; for directories, make the decision based on the mtime. -M, --dirmtime Make the decision about deleting a directory based on the directory's mtime (modification time) instead of the atime; completely ignore atime for directories. -a, --all Remove all file types, not just regular files, symbolic links and directories. On systems where tmpwatch can remove unused sockets, make the decision only based on file times, ignoring possible use of the socket. -d, --nodirs Do not attempt to remove directories, even if they are empty. -f, --force Remove root-owned files even if root doesn't have write access (akin to rm -f). -l, --nosymlinks Do not attempt to remove symbolic links. -q, --quiet Report only fatal errors. -s, --fuser Attempt to use the "fuser" command to see if a file is already open before removing it. Not enabled by default. Does help in some circumstances, but not all. Dependent on fuser being installed in /sbin. Not supported on HP-UX or Solaris. -t, --test Don't remove files, but go through the motions of removing them. This implies -v. -U, --exclude-user=user Don't remove files owned by user, which can be an user name or numeric user ID. -v, --verbose Print a verbose display. Two levels of verboseness are available -- use this option twice to get the most verbose output. -x, --exclude=path Skip path; if path is a directory, all files contained in it are skipped too. If path does not exist, it must be an absolute path that contains no symbolic links. -X, --exclude-pattern=pattern Skip paths matching pattern; if a directory matches pattern, all files contained in it are skipped too. pattern must match an abso- lute path that contains no symbolic links. SEE ALSO
cron(1), ls(1), rm(1), fuser(1) AUTHORS
Erik Troan <ewt@redhat.com> Preston Brown <pbrown@redhat.com> Nalin Dahyabhai <nalin@redhat.com> Miloslav Trmac <mitr@redhat.com> 4th Berkeley Distribution 2009-10-15 TMPWATCH(8)
All times are GMT -4. The time now is 05:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy