Portál AbcLinuxu, 21. července 2025 06:42
touch -t `date -d '30 minutes ago' '+%m%d%H%M'` ~/tmp/ts
(b) použít podmínky -amin
, -mmin
, -cmin
#!/usr/bin/env python
import os, sys, stat, time, re, pwd
dir = '/tmpsamba'
file_age = 10 * 24 * 3600 #Delete files older than this number of seconds
epochsec = time.time() #Current time
def is_file_old_enough(path, anewer):
if not os.path.isfile(path):
print "Error, " + path + " is not a file"
return False
time_threshold = epochsec - anewer
if time_threshold < os.stat(path).st_atime:
# print 'File ' + fullpath + ' was recently accessed'
return False
else:
return True
def is_path_old_enough(path, anewer):
if os.path.islink(path): return True # We can always consider links to be old
# enough to be deleted
if not os.path.isdir(path): #If the tested path is not a directory, ...
return is_file_old_enough(path, anewer)
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
fullpath = os.path.join(root, name)
if os.path.islink(fullpath): continue #This should skip links
if not is_file_old_enough(fullpath, anewer):
return False
return True
def rm_rf_path(path_to_rm):
if not re.search('^/tmpsamba/', path_to_rm): # Just to make sure we are not
# deteting something valuable
print "Refused to delete " + path_to_rm
return
stat_result = os.lstat(path_to_rm) #do not follow symbolic links
uid = stat_result[stat.ST_UID]
name = pwd.getpwuid(uid).pw_name
print "Deleting " + path_to_rm, '('+name+')'
os.system("rm -rf '" + path_to_rm + "'")
return
items = os.listdir(dir)
for i in items:
fullpath = os.path.join(dir, i)
if is_path_old_enough(fullpath, file_age):
rm_rf_path(fullpath)
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.