0

I have a following list of files;

260704       96 -rw-r--r--    1 MacStudio      staff               47131 Jul 28 20:18 ./GSM7673474_30_CC0022.barcodes.tsv.gz
260705     6264 -rw-r--r--    1 MacStudio      staff             3206621 Jul 28 20:18 ./GSM7673474_30_CC0022.features.tsv.gz
260706   592096 -rw-r--r--    1 MacStudio      staff            303149616 Jul 28 20:18 ./GSM7673474_30_CC0022.matrix.mtx.gz
260707       88 -rw-r--r--    1 MacStudio      staff               44397 Jul 28 20:18 ./GSM7673475_31_CC0022.barcodes.tsv.gz
260708     5800 -rw-r--r--    1 MacStudio      staff             2967665 Jul 28 20:18 ./GSM7673475_31_CC0022.features.tsv.gz
260709   481960 -rw-r--r--    1 MacStudio      staff            246761628 Jul 28 20:18 ./GSM7673475_31_CC0022.matrix.mtx.gz
260710       80 -rw-r--r--    1 MacStudio      staff               38322 Jul 28 20:18 ./GSM7673476_32_CC0022.barcodes.tsv.gz
260711     6104 -rw-r--r--    1 MacStudio      staff             3123868 Jul 28 20:18 ./GSM7673476_32_CC0022.features.tsv.gz
260712   430808 -rw-r--r--    1 MacStudio      staff            220569913 Jul 28 20:19 ./GSM7673476_32_CC0022.matrix.mtx.gz
260713       80 -rw-r--r--    1 MacStudio      staff               38152 Jul 28 20:19 ./GSM7673477_33_CC0053.barcodes.tsv.gz
260714     6160 -rw-r--r--    1 MacStudio      staff             3152423 Jul 28 20:19 ./GSM7673477_33_CC0053.features.tsv.gz
260715   404888 -rw-r--r--    1 MacStudio      staff            207300291 Jul 28 20:19 ./GSM7673477_33_CC0053.matrix.mtx.gz

I want to move these files to name matching directories. I created directories matching "GSM7673474_3x" as follows;

drwxr-xr-x@ 2 MacStudio  staff  64 Nov 10 18:36 GSM7673474_30/
drwxr-xr-x@ 2 MacStudio  staff  64 Nov 10 18:37 GSM7673475_31/
drwxr-xr-x@ 2 MacStudio  staff  64 Nov 10 18:37 GSM7673476_32/
drwxr-xr-x@ 2 MacStudio  staff  64 Nov 10 18:38 GSM7673477_33/

Based on an answer by Kusalananda in Move files according to pattern matching I came up with a following script to move files to name matching directories.

find . -maxdepth 1 -type d |rg -o "GSM\d+.*" > dir.name

for i in $(cat dir.name) ; do echo $i;for filename in ./*"$i"* ;do mv -i "$filename"/ $i ;done ;done

This works fine with a mv warning but I am looking for a way to mv each of 3 files to directories in order. For example, these files,

GSM7673474_30_CC0022.barcodes.tsv.gz
GSM7673474_30_CC0022.features.tsv.gz
GSM7673474_30_CC0022.matrix.mtx.gz

will be in the GSM7673474_30 directory and next 3 sets of files;

GSM7673474_31_CC0022.barcodes.tsv.gz
GSM7673474_31_CC0022.features.tsv.gz
GSM7673474_31_CC0022.matrix.mtx.gz

to the next directory, GSM7673474_31 and so forth.

Since I am seriously getting stuck figuring out, I'd really any inputs in this.

My system;

                  'c.          MacStudio.local
                 ,xNMM.          -----------------------------
               .OMMMMo           OS: macOS 13.6 22G120 x86_64
               OMMM0,            Host: Mac13,2
     .;loddo:' loolloddol;.      Kernel: 22.6.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 10 hours, 53 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 240 (brew)
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: zsh 5.9
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 2048x858
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Quartz Compositor
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    WM Theme: Blue (Light)
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   Terminal: iTerm2
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   Terminal Font: Monaco 10
    kMMMMMMMMMMMMMMMMMMMMMMd     CPU: Apple M1 Ultra
     ;KMMMMMMMWXXWMMMMMMMk.      GPU: Apple M1 Ultra
       .cooc,.    .,coo:.        Memory: 3475MiB / 131072MiB
3
  • Do what the bash tag you used instructs "For questions about Unix shells in general, use the /shell tag instead. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at shellcheck.net) before posting here." then post your corrected script and let us know if you still have a problem.
    – Ed Morton
    Nov 11, 2023 at 13:05
  • If you do still have a problem then try the parts of your script one step at a time. Does mv -i "$filename"/ $i do what you expect for 1 value of i? If so, does for filename in ./*"$i"* ;do mv -i "$filename"/ $i ;done do what you expect? And so on.
    – Ed Morton
    Nov 11, 2023 at 13:09
  • Ed, Thanks for a suggestion. Next time I will post a script question with a script tag
    – akh22
    Nov 13, 2023 at 20:48

1 Answer 1

2

Sounds like just a matter of

autoload -Uz zmv
zmv '(GSM<->_<->)_*' '$1/$f'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .