Loop to compare numbers within file name


So I’ve written a code to compare if a certain number within the file name is bigger than 11 and if it is than it should make a directory.

      -->Huistaak1-HelloWorld_Jonas.De Preter.s.ua_poging_2019-11-12

The code needs to get the day of the provided date
and if it’s above 11 it creates a directory "late_inzending"
So it should look like this

      -->Huistaak1-HelloWorld_Jonas.De Preter.s.ua_poging_2019-11-12

My code doesn’t seem to work

for dir in */
    cut1=$(echo "$dir" | cut -f4 -d '_')
    cut2=$(echo "$cut1" | cut -f3 -d '-')
    declare -i x="$cut2"
    declare -i y=11
    if (( x > y))
        mkdir late_inzending


Something like.

#!/usr/bin/env bash

for d in ./*/*/; do                   #: From main plus one level down
  [[ ! -d "$d" ]] && continue         #: If it is not a directory, skip it.
  end="${d##*-}"                      #: To remain only the last 2 strings and a /, e.g. 12/
  (( ${end%/} > 11 )) &&              #: Remove the trailing `/`, to remain only 12 and compare.
  echo mkdir -p "$d"late_inzending    #: Append the desired string to the directory and create it.

  • Execute from within main

  • Remove the echo if you’re ok with the ouput.

Resources for the answer above

Answered By – Jetchisel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published