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/bashcd ~/Downloadsfor 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 \;    fidonecd -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 Junmkdir: 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/sigurdThe 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.
Strange formatting of date in bash script
bash;shell script;ls;date;timestamps
Wild guess: you have an alias for ls that's being used when you try the command interactively, and that alias isn't used in the script.  Something like this, perhaps?alias ls=ls --full-timeNote that conventional wisdom says that trying to parse ls is a bad idea.  Assuming you're on a Linux machine, something like this might be preferable:stat $FILENAME | awk '/^Modify: /{print $2}'EDIT: Or, as Stphane points out below, using find with -printf is even better.  The output of stat is slightly easier to predict than ls, but -printf allows for an output format that's entirely controlled by you.