Sponsored Content
Top Forums Shell Programming and Scripting rm files older then 2 seconds? Post 302370443 by TehOne on Wednesday 11th of November 2009 11:31:32 AM
Old 11-11-2009
Quote:
Originally Posted by mkastin
This should find and delete files older that 1 minute.

Code:
find /path/to/files -type f -mmin +1 -exec rm {} \;

That's not a solution and it's something I already knew just that 1 minut is not an option.

Quote:
Originally Posted by steadyonabix
You can simplify your ls: -
Code:
ls -l | nawk ' /^-/ && ($5 == "0"){print $NF}'

Rather than get involved in date arithmetic can you get away with a sleep of two seconds before doing the delete?
A sleep won't do the job either, that's because the file can change it's size and sure I could check it's size after those two seconds but that's not like I want it.

Quote:
Originally Posted by radoulov
The best way of doing this is not doing it at all ....
Why create a file that need to be immediately removed?

Anyway, you can play with something like this, but be careful,
it's very, very dangerous!

Code:
perl -e'
  -z and (2/86400) < -M and unlink for @ARGV
  ' *

Use it at your own risk.
I need such a check to avoid a race-condition issue...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

files older than 15 minutes

Hi Friends, i have to write a script to raise a flag if there are any files that are older than 15 minutes in the directory.The directory is supplied as the parameter to the script. please help with a sample script. Thanks in advance veera (0 Replies)
Discussion started by: sveera
0 Replies

2. UNIX for Dummies Questions & Answers

rm files older than ...

Hello, How can I remove files older than yesterday or the day before or a given day ... Thank you in advance (2 Replies)
Discussion started by: annemar
2 Replies

3. UNIX for Dummies Questions & Answers

Remove the older files

Hi All, I need to remove some old files which the file creation date is older than a week. I've tried to use command: find . -atime +6 -exec rm{}. but it seems the creation date of files shown above were not as I expected. please your kind advice. Thanks. (1 Reply)
Discussion started by: Prasandha
1 Replies

4. Shell Programming and Scripting

Getting list of files generated last 10 seconds

I was trying to figure out in Korn shell but this may apply elsewhere how to generate a list of files from a directory created in the last 10 seconds or less. I have used the find command in the past with -mtime which is measured in days to get a list of files older than say 7 days for example. ... (1 Reply)
Discussion started by: lee_murray
1 Replies

5. Shell Programming and Scripting

files older than few hours

Hi All I need to know the command which can be used to list the files which are 3 hours old so that it can be deleted. (3 Replies)
Discussion started by: mskalyani9
3 Replies

6. Shell Programming and Scripting

files older than a certain time

I know how to find files, which are newer than a specific time. touch -t 201103300650 dummy find /path/to/files -type f -newer dummy -exec ls -l {} \; Is there a way to find files, which are older than a specific time? (2 Replies)
Discussion started by: BeefStu
2 Replies

7. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

8. Shell Programming and Scripting

Archiving older files

Hello Group, I would request your help to build a shell script in order to find files older than 90 days then create the same directory structure under the second disk (/archive directory) and move the file preserving the same timestamps (ownership, etc). Also keep the log of files moved... (4 Replies)
Discussion started by: csierra
4 Replies

9. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

10. AIX

Getting files through find command and listing file modification time upto seconds

I have to list the files of particular directory using file filter like find -name abc* something and if multiple file exist I also want time of each file up to seconds. Currently we are getting time up to minutes in AIX is there any way I can get file last modification time up to seconds. (4 Replies)
Discussion started by: Nitesh sahu
4 Replies
TMPREAPER(8)						      System Manager's Manual						      TMPREAPER(8)

NAME
tmpreaper - removes files which haven't been accessed for a period of time SYNOPSIS
tmpreaper [-htvfmMsaT] [--help] [--test] [--verbose] [--force] [--delay=x] [--runtime=x] [--showdeleted] [--ctime] [--mtime] [--mtime-dir] [--symlinks] [--all] [[--protect '<shell_pattern>']...] <time_spec> <dirs>... DESCRIPTION
tmpreaper recursively searches for and removes files and empty directories which haven't been accessed for a given number of seconds. Nor- mally, it's used to clean up directories which are used for temporary holding space, such as "/tmp". Please read the WARNINGS section of this manual. When changing directories, tmpreaper is very sensitive to possible race condition security exploits[1], 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), never performs chdir(".."), will not switch file systems, and only removes empty directories and regular files. Unless your machine is one with lots of relatively untrusted users, such as an ISP or school, you don't need this program; `find ... -exec rm ...' works just as well when you don't have to be concerned about people trying to exploit the race condition on you. tmpreaper will stop itself after almost one minute with an appropriate warning message, as attempts to keep it running long enough so that it runs in parallel with another instance of itself may also lead to possible vulnerabilities. Normally, tmpreaper won't need that amount of time. If your system is so slow that it does, try to configure things so that this doesn't happen. As a last resort, the --runtime=x option can be used to set the number of seconds after which the timeout occurs; the default setting is 55 seconds. tmpreaper dates files by their atime, not their mtime, unless you select the --mtime option. If files aren't being removed when ls -l implies they should be, use stat(1) or ls --time=access to examine the file's atime and see if that helps to explain the problem. Additionally, tmpreaper can be instructed to also check the ctime (inode change time, which is updated e.g. when the file is created or permissions are changed). This is primarily useful when tmpreaper is used to clean up directories that are accessible as a Samba share; DOS (and Windows) PCs preserve the mtime and the atime when copying to a new file, so that it appears that the newly created file is old. tmpreaper will remove such files is the atime is beyond the removal time, even though they were just created. This is avoided by using the --ctime option. As testing the contents of subdirectories will update those directories' atime, empty directories won't be removed. To circumvent this problem you can use the --mtime-dir option, which will switch on mtime checking for directories only. Using --mtime-dir in addition to --mtime doesn't do anything useful. The <time_spec> parameter defines the age threshold for removing files. If the file has not been accessed for <time_spec>, it becomes eli- gible for removal. The <time_spec> should be a number, defaulting to hours, optionally suffixed by one character: `d' for days, `h' for hours, `m' for minutes, or `s' for seconds. Following the time option, one or more directories must be given for tmpreaper to clean up. On linux ext2/ext3 filesystems, no errors will be given when trying to remove files marked as immutable. A common situation for this (nowa- days) is the ext3 .journal file. However, there may of course be other files marked as such by the system administrator. OPTIONS
<noargs>, -h, --help Print a brief version, copyright, and usage statement on stderr, then exit with error status 1. -t, --test Don't actually remove any files, but go through the motions, checking through the directory, then pretend to remove the eligible files. -v, --verbose Print a verbose display. Two levels of verbosity are available---use this option twice to get the most verbose output. The --test option automaticly sets --verbose once. Higher numbers mean more output (max. is 3). To force normal verbosity after --test, use "--verbose=0". This will generally only show error messages. Use "--test --verbose=0 --showdeleted" to give a shellscript-like list of actions that would have been done (see the --showdeleted description below). --showdeleted Show what files and directories are deleted. The output is in the form of shell commands, i.e. "rm /dir/dir2/file" and "rmdir /dir/dir2". When used together with --test, this option will still cause the "shell commands" to be printed, although nothing is really done. Note that this may show more than without --test, as problems removing the file won't be detected (e.g. immutable files). -f, --force Remove files even if EUID doesn't have write access (akin to rm -f). Normally, files owned by the current EUID, with no write bit set are not removed. --delay=x Delay execution at the start for a random time, up to x seconds; if no value is specified, the default maximum time to delay is 256 seconds. This is an option useful in cron scripts to make the execution of tmpreaper less predictable, thus making things a little harder for those who would attempt to use tmpreaper to thwart security. -T x, --runtime=x Execution of tmpreaper will aborted after x seconds; this is to prevent attacks that create many, many files. By default the time- out is set to 55 seconds. A value of 0 will disable this feature, which is not advised as this feature prevents possible race-con- ditions between different instances of tmpreaper. -m, --mtime Base the decision of whether to remove the file on its mtime, rather than on its atime. -M, --mtime-dir Base the decision of whether to remove the directory on its mtime, rather than on its atime. -c, --ctime Base the decision of whether to remove the file on its ctime, in addition to its atime. Only applicable if the --mtime options is not given! -s, --symlinks Remove symlinks too, not just regular files and directories. -a, --all Remove all file types, not just regular files, symlinks, and directories. --protect '<shell_pattern>' Protect the files that match the <shell_pattern> from deletion. This option may be used more than once. It has no one letter abbreviation, you must spell out the full word "protect". If you do not enclose the <shell_pattern> in single quotes, the shell will perform the expansion before tmpreaper reads its argument array. The program does not support that syntax, so you must use single quotes around the glob pattern. tmpreaper will chdir(2) into each of the directories you've specified for cleanup, and check for files matching the <shell_pattern> there. It then builds a list of them, and uses that to protect them from removal. For example: tmpreaper --test --verbose --protect '.X*-{lock,unix,unix/*}' --protect '.ICE-{unix{/*,}}' 5d /tmp # 5 day grace period TIPS
As long as there are files present inside a subdirectory, it won't get removed. You can use a non-writable, self-owned file, perhaps named ".tmpreaper", or, if you are su, a file that has the ext2fs immutable attribute set, to keep a subdirectory from being deleted. Of course, you could just as easily use use the --protect option to obtain the same result. Because the command line argument processing is implemented with GNU getopt_long(3)[2], you may order the arguments thusly, if it pleases you: tmpreaper --test --verbose 5h --protect './tmp/{blah?,dir{/blah4,}}' ./tmp --protect '/tmp/.X*' /tmp ... Note that if you use --all or --symlinks, it will have global effect. If you only want it turned on for one directory, you must use separate commands. WARNINGS
Please do not ever run tmpreaper on `/'!!! There are no safeguards against this built into the program, because that would make it diffi- cult to use in a chrooted environment. SEE ALSO
chattr(1) chdir(2) chroot(8) cron(1) getopt_long(3) ls(1) lsattr(1) rm(1) stat(1) [1] http://seclists.org/lists/bugtraq/1996/May/0046.html or http://www.security-express.com/archives/bugtraq/1996_2/0054.html http://linuxgazette.net/18/tmp.html (formerly http://www.linuxgazette.com/issue18/tmp.html) http://linuxgazette.net/20/followup.html [2] info:(libc)Long Options AUTHOR
Karl M. Hegbloom <karlheg@debian.org> Mostly based on `tmpwatch-1.2/1.4', by: Erik Troan <ewt@redhat.com> Now being maintained for Debian by: Paul Slootman <paul@debian.org> 4th Berkeley Distribution Mon May 19 2008 TMPREAPER(8)
All times are GMT -4. The time now is 08:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy