-1

I have files having below formatting:

2017-05-01-1500-00S.SRO___001_BH_Z
2017-05-01-1543-04S.SRO___001_BH_E
2017-05-01-1543-04S.SRO___001_BH_N
2017-05-01-1543-04S.SRO___001_BH_Z
2017-05-01-1600-00S.MOG___001_SP_E
2017-05-01-1600-00S.MOG___001_SP_N
2017-05-01-1600-00S.MOG___001_SP_Z
2017-05-01-1600-00S.MYA___001_SP_E
2017-05-01-1600-00S.MYA___001_SP_N
2017-05-01-1600-00S.MYA___001_SP_Z
2017-05-01-1600-00S.SRO___001_BH_E
2017-05-01-1600-00S.SRO___001_BH_N
2017-05-01-1600-00S.SRO___001_BH_Z

I have made a bash scripts which have some variables listed here:

st = ls -1 2* | awk -F "[.__]" '{print $2}' | sort | uniq

cmp = ls -1 2* | awk -F "[__]" '{print $5$6}' |sort | uniq

day = ls -1 2* | awk -F "-" '{print $3}' | sort | uniq

I want to make some folder with below formatting:

st/cmp/day

and then copy The corresponding values to its folder.

for example files are listed below:

2017-05-01-1500-00S.SRO___001_BH_Z
2017-05-01-1543-04S.SRO___001_BH_E
2017-05-01-1543-04S.SRO___001_BH_N
2017-05-01-1543-04S.SRO___001_BH_Z

st=SRO, cmp=BHZ, cmp=BHE, cmp=BHN, and day=01

so I need to make the following directory:

first : SRO/BHE/01

second: SRO/BHN/01

third : SRO/BHZ/01

and then copy files containing SRO, BHE, 01 (which is day value) to its corresponding directory.

I hope, I have mentioned the details.

Lots of Thanks.

0

So, you have a list of files in a give directory, like the ones in your question. You want to create directories based on certain portions of those filenames, and then move those files into their appropriate directory, correct?

If so, how about this?

#!/bin/bash

# Get the list of states(?)
st=$(ls -1 2* | awk -F "[._]" '{print $2}')

# Get the list of comparisons(?)
cmp=$(ls -1 2* | awk -F_ '{print $5$6}')

# Get the day
day=$(ls -1 2* | awk -F- '{print $3}')

# Loop over all of the states(?)
for i in ${st}; do

    # Loop over all of the comparisons(?)
    for j in ${cmp}; do

        # Break te comparisons into 2 fields.  The filenames
        # have an underscore between the two fields.  The mv
        # command fails if that underscore is not included.
        cmp1=$(echo ${j} | cut -c1,2)
        cmp2=$(echo ${j} | cut -c3)

        # Loop over the days
        for k in ${day}; do
            # echo "${i}/${j}/${k}"

            # Define the sub-directory, and make it.
            sd="./${i}/${j}/${k}"
            mkdir -p "${sd}"

            # Move all files found into the appropriate sub-directory
            # Ignore any error messages since some of the string
            # combinations do not exists as files.
            mv -v 2*${k}*${i}*${cmp1}_${cmp2}* ${sd} 2>/dev/null
        done
    done
done

exit 0
  • cmp1=$(echo ${j} | cut -c1,2), cmp2=$(echo ${j} | cut -c3) these piping was really a mistake so I removed them and every thing is fine. the directories are made fine but moving is not working at all – Nima Dolatabadi Sep 23 '18 at 19:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.