Sample Header Ad - 728x90

How to move every three day file based on the filename to respective folders in bash

1 vote
2 answers
40 views
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.
Asked by Joana Carvalho (37 rep)
Mar 21, 2023, 12:31 PM
Last activity: Mar 22, 2023, 05:24 PM