|
This is rtrash.R, a simple script that deletes old files from libtrash's trash bin. Please let me know if you find it useful or find bugs! #!/usr/bin/Rscript --vanilla ### rtrash.R (version 2011-05-20) ### 2011, Sven Hartenstein <mail@svenhartenstein.de> ### ### This program is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ### ### This script is a poor man's way of deleting from libtrash's trash ### bin. It is an alternative to strash which comes with libtrash 3.2 ### and which is not working on my computer (and according to the www ### not on the computer of some other people; my guess is that under ### some circumstances access times are not set reliably). rtrash.R is ### written in the R language and R must be installed to use it. (It ### would surely be more elegant to use a different programming ### language, but nobody tought me python yet.) You can find R at ### http://www.r-project.org. ### ### rtrash.R deletes files from trash which are in the trash longer ### than X days. While strash is much more intelligent and uses the ### access times (or so) for evaluating the files' duration of stay in ### the trash, rtrash.R records the arrival in the trash itself. ### Therefore, rtrash.R works even if access times are not reliable. ### The disadvantage of rtrash.R is that it does not know when files ### in the trash were actually put there and thus must be run ### regularly to check whether new files are in the trash and record ### their filenames for future deletion. If the trash holds many ### files, the record file (a simple CSV file) may get large. rtrash.R ### should usually be run regularly and automatically, e.g. by cron. I ### run it automatically whenever I log in. ### ### If you find this script useful, please let me know! ### ### Usage: ### rtrash.R [option...] ### ### Options: (You can NOT combine like '-nd', use '-n -d' instead!) ### -d NUM Delete remembered files in trash older than NUM days. ### Caution, '-d 0' will delete all files in the trash! ### -n Do nothing, just report. Implies -v. ### -s Remember new files in trash for future deletion. ### -v Verbose. ### ### Examples: ### rtrash.R -s -d 30 ### Record new files in trash and delete files recorded 30 or ### more days ago, without any output. ### rtrash.R -n -d 7 ### Print one line for each file that has been recorded 7 or ### more days ago, without actually deleting. ### Configuration trashDir <- "~/.Trash" recordFile <- "~/.rtrash.csv" ### End of configuration. args <- commandArgs(trailingOnly=TRUE) if (!("-d" %in% args | "-s" %in% args)) stop("Neither argument -d nor -s given. I do nothing.") is.wholenumber <- function(x, tol=.Machine$double.eps^0.5) abs(x - round(x)) < tol ## Set trash (the actual list of files in trash) and record (the ## recorded filenames with date) trash <- list.files(path=trashDir, all.files=TRUE, full.names=TRUE, recursive=TRUE) if (file.exists(recordFile)) { record <- read.csv(recordFile, colClasses=c("Date", "character")) ## Delete entries in record that are (for some reason) not in trash: record <- record[record[["filename"]] %in% trash, ] } else { record <- data.frame() } ## Settings for argument -n (dry run) if ("-n" %in% args) { dryrun <- TRUE drystr <- " WOULD:" args <- append(args, "-v") } else { dryrun <- FALSE drystr <- "" } ## Deletion if ("-d" %in% args && nrow(record) >= 1) { days <- as.numeric(args[match("-d", args)+1]) if (!is.wholenumber(days)) stop("Argument -d is not a valid number!") del <- (as.integer(Sys.Date())-as.integer(as.Date(record[["rememberdate"]]))) >= days for (file in record[["filename"]][del]) { ## We do not use file.remove() because the file could end up in ## the trash again. if ("-v" %in% args) cat(drystr, "Deleting", file, "\n") if (!dryrun) system(paste("TRASH_OFF=YES rm \"", file, "\"", sep="")) } if (!dryrun && any(del)) { ## Delete empty directories: system(paste("find ",path.expand(trashDir)," -mindepth 1 -type d -empty -delete", sep="")) } ## Update trash trash <- trash[ !(trash %in% record[["filename"]][del]) ] ## Update record record <- record[!del, ] } ## Recording if ("-s" %in% args) { filenames <- trash[!(trash %in% record[["filename"]])] newRecord <- data.frame(rememberdate=rep(Sys.Date(), times=length(filenames)), filename=filenames) if ("-v" %in% args) cat(drystr, "Remembering", length(filenames), "new files in trash.\n") record <- rbind(record, newRecord) } ## Update record file if (!dryrun) write.csv(record, file=recordFile, row.names=FALSE) Changelog
|