I am working on a script to sort my downloads folder by the date the files were created. This is the script I am using:
#!/bin/bash
cd ~/Downloads
for FILENAME in *
do
if [[ -f $FILENAME ]]; then
date="$(ls -l $FILENAME | tr -s ' ' | cut -d ' ' -f 6 | tr -d '.')"
mkdir -vp "$date"
find . -maxdepth 1 -type f -name $FILENAME -exec mv {} "$date" \;
fi
done
cd -
The name of the folder created by this script is just Jun which leads me to believe there is some sort of conversion going on somewhere.
If I do just a
ls -l test0 | tr -s ' ' | cut -d ' ' -f 6 | tr -d '.'
the date shows 19062014 correctly.
This is the result of a bash -x of the script:
sigurd@Goliath ~ -> bash -x clean
+ cd /home/sigurd/Downloads
+ for FILENAME in '*'
+ [[ -f test0 ]]
++ ls -l test0
++ tr -s ' '
++ cut -d ' ' -f 6
++ tr -d .
+ date=Jun
+ mkdir -vp Jun
mkdir: created directory 'Jun'
+ find . -maxdepth 1 -type f -name test0 -exec mv '{}' Jun ';'
+ for FILENAME in '*'
+ [[ -f test1 ]]
++ ls -l test1
++ tr -s ' '
++ cut -d ' ' -f 6
++ tr -d .
+ date=Jun
+ mkdir -vp Jun
+ find . -maxdepth 1 -type f -name test1 -exec mv '{}' Jun ';'
+ cd -
/home/sigurd
The functionality I'm looking for is that the script should move files into folders named 19_06_2014, 20_06_2014 etc. based on when the file was created.