linux - Parse folder name in bash -
i have folders naming convention:
yearmonthday_jobcode_descriptor
ie 20101007_gr1234_whatsit
i'm attempting write script few things:
- move folders date older $x ./old
- move folders date in future ./old
- move folders dont conform convention ./old
- delete folders in ./old date older $x + $y
here current loop, can see bash scripting pretty mediocre.
#!/bin/bash file in $1/*; if [ -d $file ]; echo $(echo $file | grep -oe "[0-9]{8}_[a-z]*[0-9]*_?.*") fi done
i can handle looping, main concern @ moment how extract folder names variables can compared dates. current regex above checks conformity convention.
you can use cut command tokenize string (-d specifies delimiter, -f specifies field want keep) -
echo 20101007_gr1234_whatsit | cut -d'_' -f1
gives
20101007
from there, can use date command parse date-
foo=`date -d 20101007 +%s`
will convert date string epoch time (seconds since jan 1, 1970) can compared.
if don't want mess epoch time, can call date multiple times parse different parts -
day=`date -d 20101007 +%d` month=`date -d 20101007 +%m` year=`date -d 20101007 +%y`
but make comparisons little more complicated.
Comments
Post a Comment