2

I have files named

patient_sample_L001_R1_001, patient_sample_L002_R1_001, patient_sample_L003_R1_001, patient_sample_L004_R1_001

another set named

patient_sample_L001_R1_002, patient_sample_L002_R1_002, patient_sample_L003_R1_002, patient_sample_L004_R1_002,

and so on and so forth. For each set of 4 files (with different end numbers 001,002,003) i need to move it into an appropriate directory labelled patient0001, or patient 0002, and so on and so forth. I have around 200 of these sets. Is there a command I can do that scans for files with a different end number and puts it into the appropriate patient folder?

i.e patient_sample_L001_R1_001, patient_sample_L002_R1_001, patient_sample_L003_R1_001, patient_sample_L004_R1_001, goes into "patient0001 folder and etc.

Each of these files are in different directories. For example main directory has a directory called run40 in it there is the patient1 data. Then in run41 there is the patient 2 data and etc.. back in the main directory there is another directory called "total_patient_samples"that is where you can find all the directories that i want to move the files from the "run40, run41" directories into. In that file there is patient0001, patient 0002, patient 0003. I would like the aforementioned data files to go into there.

  • Is renaming f.ex. run40 to patient0001 a possibility? – serv-inc Oct 19 '15 at 15:20
  • 1
    Please edit your question and show us the output of tree in run41, run41 and total_patient_samples. It is hard to understand your description. Are the directiories in total_patient_samples actually named patient0001 or patient 0001 or is it patient001 as in the files? – terdon Oct 19 '15 at 16:54
1

How about

for i in `seq -f '%03g' 1 200`; do
    FOLDER=patient0$i
    mkdir -p total_patient_samples/$FOLDER    # creates folder if missing
    find . -regex ".*patient_sample_L00._R._${i}$" -type f -exec mv -i {} total_patient_samples/$FOLDER/ \; # moves patient data if not already there, else prompts
done

This cycles i from 001 to 200 (change the 200 if necessary), creates the patient-folder (remove the mkdir ... line if not needed), finds all patients of the structure you specified, and moves them into the folder.

1

You can do something like:

cd total_patient_samples
find ../ -name patient_sample_\* | while read -r i; do
    pnum="${i##*_}"
    mkdir -p "patient$pnum"
    mv -v "$i" "patient$pnum"
done

This depends sensitively on the exact names of the files, so it's good to be careful.

EDIT: Updated code according to comment below. This will grab any file under the main directory that matches patient_sample_*, so make sure there aren't any you don't want to move.

0

You can do it something like this:

for file in patient*; do
 PATIENT_NO=$(echo $file | sed 's/patient_sample_.*R1_\(.*\)/\1/')
 PATIENT_DIR=patient$PATIENT_NO
 mkdir -p $PATIENT_DIR
 mv $file $PATIENT_DIR
done

At first we cut with sed the patient number, then construct directory name and move file into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.