So you want to play a pRank in R…?

December 18, 2018

(This article was first published on R – Open Source Automation, and kindly contributed to R-bloggers)


So…you want to play a pRank with R? This short post will give you a fun function you can use in R to help you out!

How to change a file’s modified time with R

Let’s say we have a file, test.txt. What if we want to change the last modified date of the file (let’s suppose the file’s not that important)? Let’s say, for instance, we want to make a file have a last modified date back in the 1980’s. We can do that with one line of code.

First, let’s use to check the current modified date of some file called test.txt."test.txt")

We can see above by looking at mtime that this file was last modified December 4th, 2018.

Now, we can use a function called Sys.setFileTime to change the modified date to any date including or after January 1, 1970.

Sys.setFileTime("test.txt", "1980-12-04")

The base R function above takes the file name as the first parameter, and the date for the second parameter. If we use on this file, we can see the newly changed last modified date – set back in 1980!

how to change modified time of a file in r

If we want to change the modified dates for all the files in a directory, we could do this:

sapply(list.files("/path/to/some/directory", full.names = TRUE), 
                  function(FILE) Sys.setFileTime(FILE, "1975-01-01"))

The above code will change the last modified time of every file in the directory specified to be January 1, 1975.

You can also, to an extent, make the last modified time some date in the future (up to 2038 as of this writing).

sapply(list.files("/path/to/some/directory", full.names = TRUE), 
                  function(FILE) Sys.setFileTime(FILE, "2038-01-18"))

Please check out my other R posts here, or subscribe via the right side of the page to receive updates about new posts! To learn more about file manipulation in R, please click here.

The post So you want to play a pRank in R…? appeared first on Open Source Automation.

To leave a comment for the author, please follow the link and comment on their blog: R – Open Source Automation. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)