I'm using list.files(path, pattern, full.names = TRUE)
to get a list of files in a specific directory.
The files are, by default, sorted alphabetically. Is there any way in R to keep them sorted by date?
Click the sort option in the top right of the Files area and select Date from the dropdown. Once you have Date selected, you will see an option to switch between descending and ascending order.
To sort files in a different order, click one of the column headings in the file manager. For example, click Type to sort by file type. Click the column heading again to sort in the reverse order. In list view, you can show columns with more attributes and sort on those columns.
ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.
How to list Unix files in date order? In order to ls by date or list Unix files in last modifed date order use the -t flag which is for 'time last modified'. or to ls by date in reverse date order use the -t flag as before but this time with the -r flag which is for 'reverse'.
You can use the file.info
function to obtain details on your files. Once you have those details, you can sort the files accordingly. For example,
details = file.info(list.files(pattern="*.csv"))
gives a data frame containing, inter alia, modification and creation times. You can sort that data frame however you want. Here I sort according to modification time, mtime
:
details = details[with(details, order(as.POSIXct(mtime))), ] files = rownames(details)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With