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

  • 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

Edit | Attach | Print version | History: r6 < r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions...

 Barrick Lab  >  WebLeftBar  >  ComputationList  >  ProtocolsMassRename

Topic revision: r5 - 09 Sep 2014 - 13:45:24 - Main.DanielDeatherage
 
This site is powered by the TWiki collaboration platformCopyright ©2017 Barrick Lab contributing authors. Ideas, requests, problems? Send feedback