BashScripts.org
http://bashscripts.org/forum/

Organize files by date into year/month/day folders.
http://bashscripts.org/forum/viewtopic.php?f=8&t=1528
Page 1 of 1

Author:  afink [ Mon Jan 09, 2012 12:59 pm ]
Post subject:  Organize files by date into year/month/day folders.

I am looking for a script to simply organize .wav files in a directory into year/month/day folders to make searching through recordings easier. I have found a few examples on the Internet but most are too complicated for my needs. Thank you for your help!

Author:  afink [ Tue Jan 10, 2012 6:31 pm ]
Post subject:  Re: Organize files by date into year/month/day folders.

So here is what I have so far. All it does it print out the date of the files but I'll call that progress for now. Now I am trying to figure out how to store the different parts of the date into variables. How can I store the output of the state command into a variable?

I tried date_time = stat -c %y "$x" | awk '{printf $1 "\n"}' and it didn't work.

Code:
#!/bin/bash
for x in /home/afink/*
do
        set `stat -c %y "$x" | awk '{printf $1 "\n"}'`
        echo ${1:0:4}    #Worked to get the year however I am not sure how to go about getting the month next
done

Author:  afink [ Tue Jan 10, 2012 9:40 pm ]
Post subject:  Re: Organize files by date into year/month/day folders.

Got the vars on to the next

Code:
#!/bin/bash

for x in /home/afink/*
do
        date_time=$(stat -c %y "$x" | awk '{printf $1 "\n"}')
        month=${date_time:5:2}
        year=${date_time:0:4}
        day=${date_time:8:2}
        echo TimeStamp: $date_time Month:$month Day:$day Year:$year     
done

Author:  afink [ Tue Jan 10, 2012 11:00 pm ]
Post subject:  Re: Organize files by date into year/month/day folders.

w00t. I think I got it. Let me know if you see any problems.

Code:
#!/bin/bash

directory=/home/afink

for x in $directory/*
do
        date_time=$(stat -c %y "$x" | awk '{printf $1 "\n"}')
        month=${date_time:5:2}
        year=${date_time:0:4}
        day=${date_time:8:2}
        echo TimeStamp: $date_time Month:$month Day:$day Year:$year
        if [ -d $directory/$year ]; then
                if [ -d $directory/$year/$month ]; then
                        if [ -d $directory/$year/$month/$day ]; then
                                echo Copying "$x" to /home/afink/$year/$month/$day
                                cp --preserve "$x" $directory/$year/$month/$day
                        else
                                mkdir $directory/$year/$month/$day
                                cp --preserve "$x" $directory/$year/$month/$day
                        fi
                else
                        mkdir $directory/$year/$month
                fi
        else
                mkdir $directory/$year
                if [ -d $directory/$year/$month ]; then
                        if [ -d $directory/$year/$month/$day ]; then
                                cp --preserve "$x" $directory/$year/$month/$day
                        else
                                mkdir $directory/$year/$month/$day
                                cp --preserve "$x" $directory/$year/$month/$day
                        fi
                else
                        mkdir $directory/$year/$month
                        if [ -d $directory/$year/$month/$day ]; then
                                cp --preserve "$x" $directory/$year/$month/$day
                        else
                                mkdir $directory/$year/$month/$day
                                cp --preserve "$x" $directory/$year/$month/$day
                        fi
                fi
        fi
done

Author:  Watael [ Tue Jan 10, 2012 11:33 pm ]
Post subject:  Re: Organize files by date into year/month/day folders.

hi,

I see some problems
- you don't need awk, a simple cut will do it.
- as `cd --preserve' is in every test, it is not test dependant, only one should be out of them

Author:  afink [ Wed Jan 11, 2012 2:22 pm ]
Post subject:  Re: Organize files by date into year/month/day folders.

If anyone is interested I put the updated script on my blog here: http://g00t.com/?p=709

Author:  choroba [ Tue Jan 17, 2012 10:04 am ]
Post subject:  Re: Organize files by date into year/month/day folders.

Ever heard of the -p switch to mkdir? It can save you 90% of the code...
Code:
mkdir -p $directory/$year/$month/$day
cp --preserve "$x" $directory/$year/$month/$day

Any reason you do not copy the file if directory/year exists but month does not?

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com