Friday, July 7, 2017

Copy files using regular expressions

If you need to copy or move only a portion of your files based on some rule for their name, use regular expressions.

For example, these are all fastqc.zip files in a folder:

ls | grep fastqc.zip$
071_2_E7_24h_TAG_fastqc.zip
071_2_E7_72h_TAG_fastqc.zip
071_2_E8_24h_TAG_fastqc.zip
071_2_E8_72h_TAG_fastqc.zip
334_1_E7_24h_TAG_fastqc.zip
334_1_E7_72h_TAG_fastqc.zip
334_1_E8_24h_TAG_fastqc.zip
334_1_E8_72h_TAG_fastqc.zip
756_3_E7_24h_TAG_fastqc.zip
756_3_E7_72h_TAG_fastqc.zip
756_3_E8_24h_TAG_fastqc.zip
756_3_E8_72h_TAG_fastqc.zip
835_1_E7_24h_TAG_fastqc.zip
835_1_E7_72h_TAG_fastqc.zip
835_1_E8_24h_TAG_fastqc.zip
835_1_E8_72h_TAG_fastqc.zip
H1_E7_24h_TAG_fastqc.zip
H1_E7_72h_TAG_fastqc.zip
H1_E8_24h_TAG_fastqc.zip
H1_E8_72h_TAG_fastqc.zip
H7_E7_24h_TAG_fastqc.zip
H7_E7_72h_TAG_fastqc.zip
H7_E8_24h_TAG_fastqc.zip
H7_E8_72h_TAG_fastqc.zip
If you need to find files with specific number, lets say those with _1_ or _2_ in the name of the file, use the following regex:


ls | grep .*_[1-2]_.*_fastqc.zip$
071_2_E7_24h_TAG_fastqc.zip
071_2_E7_72h_TAG_fastqc.zip
071_2_E8_24h_TAG_fastqc.zip
071_2_E8_72h_TAG_fastqc.zip
334_1_E7_24h_TAG_fastqc.zip
334_1_E7_72h_TAG_fastqc.zip
334_1_E8_24h_TAG_fastqc.zip
334_1_E8_72h_TAG_fastqc.zip
835_1_E7_24h_TAG_fastqc.zip
835_1_E7_72h_TAG_fastqc.zip
835_1_E8_24h_TAG_fastqc.zip
835_1_E8_72h_TAG_fastqc.zip
Now simply to copy those files capture the output of the command:

cp $(ls | grep .*_[1-2]_.*_fastqc.zip$) test/
cd test/
ls -ltrh
total 10648
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 835_1_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   439K Jul  7 01:45 835_1_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   438K Jul  7 01:45 334_1_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 334_1_E8_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   446K Jul  7 01:45 334_1_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   436K Jul  7 01:45 334_1_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   441K Jul  7 01:45 071_2_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   441K Jul  7 01:45 071_2_E8_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   444K Jul  7 01:45 071_2_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   438K Jul  7 01:45 071_2_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   447K Jul  7 01:45 835_1_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 835_1_E8_24h_TAG_fastqc.zip

No comments:

Post a Comment