Overview
This is an example command for renaming multiple files at once. Ideally it is presented as a method for shortening file names by removing common elements in the name. It could similarly be used to create backup copies of a file to play with by changing the "mv" command to a "cp" command. This command works as a single line command on linux/unix, and should be entered directly into the command prompt.
Renaming multiple fastq files with command line variables
This appears much simpler and better than messing with sed, even if you may need to do 2x one for left side, one for right side.
This allows you to quickly rename files without the overhead of extra variables, but may have slightly less control? It is a good idea to make sure the command makes sense by using "echo" before actually moving things (i.e. add
echo
between
do mv
).
- Assume you have downloaded multiple fastq files which are of the format Lib-###-S##-L##-R#-001.fastq ... The following command will rename all files to Lib-###.
DO NOT DO THIS TO RAW SEQUENCING FILES SUCH AS THOSE ON CORRAL
-
for name in *.fastq; do mv "$name" "${name/-S*.fastq}";done
- if instead you would like to keep the .fastq on the end of the file name the following command will work:
-
for name in *.fastq; do mv "$name" "${name/-S*.fastq/.fastq}";done
Renaming multiple fastq files with sed
This gives you a bit more control using sed to generate new variable names and a single move command rather than a simple replace command.
- Assume you have downloaded multiple fastq files which are of format: Samplename-Lane-runID-etc-etc.fastq ... the following command will rename all files to Samplename.fastq:
- for f in *.fastq;do new_name=$(echo $file|sed 's/-*/.fastq/'); mv -i $file $new_name;done
- Generic explanation in parts:
- for f in *.fastq;
- "for" is the start of a for loop which allows you to do the same thing to multiple files sequentially
- generate a generic variable named "f" which is a list of all files in the current directory that end in ".fastq"
- ; end the existing for loop generation
- do new_name=$(echo $file|
- do is what you want to do with each of the entries in the for loop
- generate a new variable named "new name" with the starting point of the old file name, change the name based on the following sed command.
- sed 's/-*/.fastq/');
- "sed" is the name of the command to be called. it is effectively just a find and replace command. It has 3 parts separated by / marks, as follows:
- "s" tells the command you are dealing with a string
- "-*" says match everything after and including the first hyphen, change this according to the particular sample so you are sure that it is only matching what you want it to
- ".fastq" says what you want to replace the matched text with
- ; break apart the next command as if it were being executed on its own line
- mv -i $file $new_name
- "mv -i" is being used as a rename command here, with the -i flag meaning not to overwrite existing file name. This is important if your replacement string is wrong and you are generating multiple final names that are identical.
- $file $new_name this uses the previously created in line variables to reference the existing file as well as the new name you want to name the file.
- ;done
- all for loops end with a "done" command
-- Main.DanielDeatherage - 21 Aug 2014
Topic revision: r6 - 2016-04-12 - 21:07:13 - Main.DanielDeatherage