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

2 comments:

  1. i dont know what i could have done if i did not get to know this simpler way. i am sure cheap professional cv writing it is going to save me a lot of time. thank you for posting with this

    ReplyDelete
  2. Thanks, in my opinion, sooner or later each user comes across this problem, as like
    unpacking of ZIP files and avoid recommendation how to open zip file https://wikiext.com/zip until receiving an error directly. You will know in detail what to do in such cases, and from the list of decisions you'll necessarily find out what will work in your particular case. Exactly, I advise you to save in bookmarks, or you'll have to look for the source again, as I did before.

    ReplyDelete