0

I'm trying to create a directory structure based on characters in the file name (the file name includes dates and times the file started being written to). I'm up to the point of having figured out how to read certain "columns" in my file name and print them. But now I can't figure out how to store them as integers. Here is my code that I'm trying to add to:

#!/bin/bash
FILES=samplefilenames*
for file in ${FILES}; do
    echo "${file}" | awk -F'[-_]' '{print $8}'
done
exit $ERRORS

My filenames have '-' and '_' in them, hence the delimiters. I can make this successfully print the values I'm looking for. But I cannot get it to assign those values as variables. Literally something around the lines of

tempNum=echo "${file}" | awk -F'[-_]' '{print $8}'

Any help would be appreciated.

EDIT:

Source filename example - samplefilenames_2017-10-31_12-23-13.csv (filenames all have date/time stamps in them in this format). Directory structure I'm looking to do is add code to the existing file that places them here, and instead of having hundreds of files sitting in one location, I want them organized in directories by date. At this first level, folders of January, February, March.... (column 2) Then within each of those, subdirectories of each of the days of the month (column 3), then within each of those, one for each hour of that day column 4) Also, the column 8 in my code is just from my code, since my filenames are actually longer than my example is. I'm aware that the value needs to change accordingly.

2
  • Can you give us an example of the source filename and the corresponding directory structure you'd like to see? Oct 31, 2017 at 17:38
  • Source filename example - samplefilenames_2017-10-31_12-23-13.csv (filenames all have date/time stamps in them in this format). Directory structure I'm looking to do is add code to the existing file that places them here, and instead of having hundreds of files sitting in one location, I want them organized in directories by date. At this first level, folders of January, February, March.... (column 2) Then within each of those, subdirectories of each of the days of the month (column 3), then within each of those, one for each hour of that day column 4).
    – TechSavvy
    Oct 31, 2017 at 17:39

2 Answers 2

1

Try something like this:

#!/bin/bash
for filename in *.csv; do
    dateTime=($(basename "${filename}" .csv | awk -F_ '{ print $2, $3 }'))
    date="${dateTime[0]}"
    time="$(echo "${dateTime[1]}" | sed -e 's/-/:/g')"

    dir="$(date -d "${date} ${time}" +"%Y/%B/%d/%H")"

    mkdir -p "${dir}"
    mv "${filename}" "${dir}"
done

Basically, I parse off the date and time, turn in into something that the date command can consume, use date to produce a string representation of the directory structure you're looking for, create that directory, and move the file into that directory.

The directory format is:

year/month/date/hour

If you do not want the year to be included, you can omit the %Y/ in the parameter to the date command.

Please note that I haven't heavily tested this, YMMV.

1
  • Awesome! This worked exactly as written in my test directory, and also worked flawlessly on the first shot when modified for my actual real-world use. Thanks!
    – TechSavvy
    Nov 1, 2017 at 0:03
1
(IFS=_-.; for f in *.csv; do set -- $f; echo $6; done)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .