-1

I have a folder contain daily rainfall data in geotiff format from 1981-2019 with naming convention chirps-v2.0.yyyymmdd.1days.tif

I would like to arrange all the files based on MONTH information, and move into a new folder, ie all files with Month = January will move to Month01 folder.

Is there any one-liner solution for that, I am using terminal on macos.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user97103
  • 235
  • 1
  • 7

2 Answers2

1

This should do it:

for i in $(seq -f "%02g" 1 12); do mkdir -p "Month$i"; mv chirps-v2.0.????$i*.tif "Month$i"; done

Explanation:

  • For each number in the range 1, 12 (padded with 0 if necessary)...
  • Make the directories Month01, Month02, etc. If the directory already exists, continue.
  • Move all files that include the current month number in the relevant part of the filename to the appropriate folder. The question marks in chirps-v2.0.????$i*.tif represent single-character wildcards.

Note: If there is any chance there will be spaces in your .tif filenames, you can use "chirps-v2.0."????"$i"*".tif" instead.

jdaz
  • 5,964
  • 2
  • 22
  • 34
0

I don't think there is a simple way to do this. You can, however, do a "one-liner" solution if you use pipes and for loops, things like that:

for file in $(ls *.tif); do sed -r 's/(.*\.[0-9]{4})([0-9]{2})(.*)/\1 \2 \3/' <<< "$file" | awk '{print "mkdir -p dstDir/Month" $2 "; cp", $1 $2 $3, "dstDir/Month" $2}' | bash ; done

Formatting this a bit:

for file in $(ls *.tif); do \
    sed -r 's/(.*\.[0-9]{4})([0-9]{2})(.*)/\1 \2 \3/' <<< "$file" \
    | awk '{print "mkdir -p dstDir/Month" $2 "; cp", $1 $2 $3, "dstDir/Month" $2}' \
    | bash 
done

This needs to be executed from the directory containing your files (see "ls *.tif). You will also need to replace "dstDir" with the name of the parent directory where "Month01" will be created.

This may not be perfect, but you can edit it, if required. Also, if you don't have bash, only zsh, replace the "bash" bit by "zsh" should still work.

Marcio Lucca
  • 370
  • 2
  • 10
  • I got an error ```zsh: argument list too long: ls```, Is it because too many files in a folder? I have 14,406 files (daily files from 1 Jan 1981 to 10 Jun 2020. – user97103 Aug 04 '20 at 06:11