Home > OS >  moving files to equivalent folders in bash shell
moving files to equivalent folders in bash shell

Time:11-08

Im sorry for the very basic question but I am frankly extremely new at bash and can't seem to work out the below. Any help would be appreciated.

In my working directory '/test' I have a number of files named :

  • mm(a 10 digit code)_Pool_1_text.csv
  • mm(same 10 digit code)_Pool_2_text.csv
  • mm(same 10 digit code)_Pool_3_text.csv

how can I write a loop that would take the first file and put it in a folder at :

/this/that/Pool_1/

the second file at :

/this/that/Pool_2/

etc

Thank you :)

CodePudding user response:

Using awk you many not need to create an explicit loop:

awk 'FNR==1 {match(FILENAME,/Pool_[[:digit:]] /);system("mv " FILENAME " /this/that/" substr(FILENAME, RSTART, RLENGTH) "/")}' mm*_Pool_*.text.csv
  • the shell glob selects the files (we could use extglob, but I wanted to keep it simple)
  • awk gets the filenames
  • we match pool and digit
  • we move the file using the match to extract the pool name
  • Related