Solved: See updates in comments.
I'm trying to sort files with a date in the filename into folders based on that date. The files used to have a space in the filename, like this. There are a few older files, so I need to sort those too.
Dispatch Fax_20180521121901.pdf
We're now getting the files with no space in the filename, so it's just Dispatch_20180521124202.pdf
The date is the 20180521 and the rest of it i think are message IDs, but we don't need that. Just the date.
This attempt at a bash script (ubuntu 18.04 box) ended up creating a single folder called 2018-05-21 one directory level up from where the files lived, and just put all of the pdf files in that folder.
for x in /home/tb/temp/*.pdf
do
d=$(date -r "$x" +%Y-%m-%d)
mkdir -p "$d"
mv -- "$x" "$d/"
done
This ended up moving into a folder called "e" somehow.
for x in /home/tb/temp/*.pdf
do
d="${x:4:4}-${x:8:2}-${x:10:2}"
mkdir -p "$d"
mv -- "$x" "$d/"
done
This attempt? I ended up with a folder for each PDF.
/home/tb/20180521124202.pdf/Dispatch Fax_20180521124202.pdf /home/tb/20180521121901.pdf/Dispatch Fax_20180521121901.pdf
for x in /home/tb/temp/*.pdf
do
d=$(echo "$x" | awk -F _ '{print $2}')
mkdir -p "$d"
mv -- "$x" "$d/"
done
The scripts are from previous posts here and here and those worked for them, but I'm not having the same luck even though the goal is the same.
My goal is to eventually populate a temp directory from rsync (pulling these files from another server) and then via cron, run this script to move things into folders based on date, and serve that up via Apache with "Options +Indexes" enabled. It's an intranet only page, and this particular machine has no outside access. Directory structure is simple. Eventually something like /var/www/html/2018/5/21 etc, following the YYYY/MM/DD format.
I'm just at a loss on how to get this working and my knowledge of bash scripting is very rusty. I have looked at a couple other examples like this one but then I see that it won't work with files that have spaces in the filenames.
I'm open to ideas!