ThingsIdoAllTheTime

Capture a part of a filename

Capture the begining of a filename

Study_Patient_Assay_Date.txt

> echo Study_Patient_Assay_Date.txt | cut -c-13
Study_Patient

Rename Files using Part of Current filename

This can be done multiple ways but I use two commonly depending on the format of the files

1) Files without complete standardization

Patient001_20130102.txt

Patient002_19990203_run2.txt

#Loop through all files in current directory ending with .txt
for line in `ls *.txt`
do
    #Capture the patient name from each file as a variable
    PATIENT_NAME=`echo ${line} | cut -c-10`
    #Rename files
    mv ${line} ${PATIENT_NAME}.txt
    #Message
    echo
    echo "---------------------------------"
    echo "Parsing file: ${line}"
    echo Old Name=${line}
    echo New Name=${PATIENT_NAME}.txt
done

2) Files with standardized endings

Patient001_2013_rc_ir_dm.bam

Patient002_20120118_rc_ir_dm.bam

#Loop through all files in current directory ending with .bam
for line in `ls *.bam`
do
    #Capture the non-standard portion of the filename as a variable, using the unix basename function
    BASE_NAME=`basename ${line} "_rc_ir_dm.bam"`
    #Rename files
    mv ${line} ${BASE_NAME}.bam
    #Message
    echo
    echo "---------------------------------"
    echo "Parsing file: ${line}"
    echo Old Name=${line}
    echo New Name=${BASE_NAME}.bam
done

Replace a portion of a file name with something else (ie. Find X and Replace with Y)

#Loop through all files in current directory ending with .md5

for line in `ls *.md5`

do

#Rename files using move (mv) command

#The substitution function ${line//foo/bar} has two slashes to replace every occurrence of foo with bar

mv $line ${line//KBS5U/KBS5X}

    #Message
    echo
    echo "---------------------------------"
    echo "Parsing file: ${line}"
    echo Old Name=${line}
    echo New Name=${line//KBS5U/KBS5X}
done

Bottom