Skip to main content
3 of 5
Edited: Create soft links instead of move

Extract sub-directory name based on pattern

I have a list of paths stored in a variable tmp for example:

/abc/bcd/def/ZRT834/ZRT834_9/5678/S1_L001_R1.tar

/abc/bcd/def/ZRT834/ZRT834_9/5678/S2_L001_I1.tar

/abc/bcd/def/ZRT834/ZRT834_9/5678/S1_L001_I2.tar

/abc/bcd/def/asd/hrt/ZRT207/ZRT207_1/5678/S1_L001_R1.tar

/abc/bcd/def/asd/agf/ZRT207/ZRT207_1/5678/S1_L001_R2.tar

/abc/bcd/def/asd/sdc/ZRT207/ZRT207_1/5678/S1_L001_I2.tar

I want to create new directories based on the matching patterns from the paths. In the above example, I want to create directoriesZRT834_9 and ZRT207_1 and create soft links for the tar files into their corresponding directories.

My output should be something like: ZRT834_9 directory having S1_L001_R1.tar, S2_L001_I1.tar, and S1_L001_I2.tar

How do I achieve this?