1

I have several files like these:

2020.001.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.001.03.04.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.002.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.003.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.004.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.004.05.06.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.005.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
2020.006.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
...
2020.366.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac

The files are year.day.hour1.hour2... The important parameters are the year and day. The day goes from 001 to 366 (or 365).

What I want is to organize my files like this (folder and files). So create the folder and move the respective files into it:

2020.001.003 - 2020.001.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.001.03.04.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.002.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.003.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac

2020.004.006 - 2020.004.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.004.05.06.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.005.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
               2020.006.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac

and so on until finishing the files in day 366

What I did (which does not work as I want):

for file in *.sac
do
year=`echo "$file" | awk -F"." '{print $1}'`
day=`echo "$file" | awk -F"." '{print $2}'`
dayi=$day
dayf="366"
#
# Moving every three day files
delta=2
x1=$dayi
x2=$(echo "$x1+$delta" | bc)
if [ $x1 -lt $x2 ]
then
echo $x1 $x2
dir=$(echo "$year"."$x1"."$x2")
mkdir $dir
x1=$(( x1+3))
x2=$(( x1+delta))
fi
done

As a result the code is creating folders like:

2020.001.3
2020.002.4
2020.003.5
...

Basically it is creating folders that I do not need. Also I still do not know how to move the files into the folders.

Thanks for your help.

2 Answers 2

2

Everything can be done much simpler if using bash's abilities:

#!/bin/bash
for file in *.sac
do
    year=${file:0:4}
    day=${file:5:3}
    
    range_start=$(( (($day-1)/3)*3+1 ))
    range_end=$(( $range_start+2 ))
    
    ndir=$(printf "%04d.%03d.%03d" $year $range_start $range_end)

    mkdir -p archive/$ndir
    mv $file archive/$ndir/
done
2
  • White Owl, thanks a lot for your help. This works perfectly. Thanks again. Commented Mar 21, 2023 at 13:46
  • Just let me add, in case anyone need this code, because I have a 008 day and this is an octal number, a slightly change needs to be made in the range_start: range_start=$(( ((10#$day-1)/3)*3+1 )). Commented Mar 21, 2023 at 15:04
2

In zsh, you could do it using its zmv batch renamer.

autoload -Uz zmv
mkmv() { mkdir -p -- $2:h && mv -- "$@"; }
zmv -n -P mkmv '(<->.)(<1-366>).*.sac' \
               '$1${(l[3][0])$(( start = ($2 - 1) / 3 * 3 + 1 ))}.${(l[3][0])$(( start + 2 ))}/$f'

(remove the -n (dry-run) if happy).

That gives:

mkmv 2020.001.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.001.003/2020.001.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.001.03.04.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.001.003/2020.001.03.04.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.002.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.001.003/2020.002.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.003.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.001.003/2020.003.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.004.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.004.006/2020.004.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.004.05.06.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.004.006/2020.004.05.06.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.005.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.004.006/2020.005.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.006.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.004.006/2020.006.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac
mkmv 2020.366.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac 2020.364.366/2020.366.00.01.pcc1_TBTZ_TBTZ_1.0-3.0.sac

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.