Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created February 8, 2018 05:05
Show Gist options
  • Save tmasjc/7c2913960d9d40594d01ad7e00b69331 to your computer and use it in GitHub Desktop.
Save tmasjc/7c2913960d9d40594d01ad7e00b69331 to your computer and use it in GitHub Desktop.
Rename multiple files at once using regex. #bash
#!/usr/bin/env Rscript --vanilla
args <- commandArgs(trailingOnly = TRUE)
# List all files
items <- list.files(path = ".")
# See which matches argument (regex)
old <- items[grep(items, pattern = args[1])]
# What to replace to?
new <- gsub(pattern = args[1], replacement = args[2], x = old)
# Replace those matches
file.rename(old, new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment