1

I have a folder with thousands of files respecting the following naming scheme:

'institute_model_scenario_river.txt'

Each components of the files names (institute, model, scenario, river) are variables.

Then, I would like to sort and classify those files by the name of each variables exept 'scenario' and 'river'.

Therefore, each institute name should be used to create a corresponding directory. These directories should be composed of sub-directories named accordingly with different 'model' names.

To be more accurate I would like to provide a small example, considering the following files:

wbm_gfdl_rcp4_mississippi.txt
matsiro_gfdl_rcp8_amazon.txt
wbm_miroc_rcp8_niger.txt

I would like to create a 'wbm' directory having the sub-directories 'gfdl' (containing the following file 'wbm_gfdl_rcp4_mississippi.txt') and 'miroc' (containing the following file 'wbm_miroc_rcp8_niger.txt'). And a 'matsiro' directory having just a 'gfdl' sub-directory (containing 'matsiro_gfdl_rcp8_amazon.txt').

Considering the huge amount of files, I don't really know from where to start!

1 Answer 1

1

In bash, you can split the file name to an array:

#!/bin/bash
for file in *_*_*_*.txt ; do
    IFS=_ arr=($file)
    dir=${arr[0]}/${arr[1]}
    [[ -d $dir ]] || mkdir -p "$dir"
    cp "$file" "$dir"/
done

If you want to move files instead of copy them, use mv instead of cp.

0

You must log in to answer this question.

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