- #1
- 3,759
- 4,199
- TL;DR Summary
- I'd like help writing a bash script to rename a list of files.
I'd like to rename a bunch of files in a directory based on data from a tab-delimited text file. I know how to do this in R:
Where the fileNames.txt file looks something like:
In an effort to improve my knowledge of unix shell scripting, I'd like to know how one would approach writing a bash script that does this.
Code:
dir <- "~/user/folder/"
fileNames <- read.table(paste0(dir,"fileNames.txt"),sep="\t",header=T)
for (i in c(1:nrow(fileNames))){
oriFile <- paste0(dir,fileNames[i,"Sample"],"_S",i,sprintf("_L%03d",fileNames[i,"Lane"]),"_R1_001.fastq.gz")
newFile <- paste0(dir,fileNames[i,"ID"],".fastq.gz")
system(paste("mv",oriFile,newFile))
}
Where the fileNames.txt file looks something like:
Code:
Lane Sample ID
1 xxx-xx-32S-pl1-J01 WT1.IN
1 xxx-xx-32S-pl1-J02 WT2.IN
1 xxx-xx-32S-pl1-J03 WT3.IN
In an effort to improve my knowledge of unix shell scripting, I'd like to know how one would approach writing a bash script that does this.