Register
It is currently Thu Aug 21, 2014 4:09 am

Organize files by date into year/month/day folders.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Jan 09, 2012 12:59 pm   

Joined: Mon Jan 09, 2012 12:52 pm
Posts: 5
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!


Top
 Profile  
 PostPosted: Tue Jan 10, 2012 6:31 pm   

Joined: Mon Jan 09, 2012 12:52 pm
Posts: 5
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


Top
 Profile  
 PostPosted: Tue Jan 10, 2012 9:40 pm   

Joined: Mon Jan 09, 2012 12:52 pm
Posts: 5
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


Top
 Profile  
 PostPosted: Tue Jan 10, 2012 11:00 pm   

Joined: Mon Jan 09, 2012 12:52 pm
Posts: 5
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


Top
 Profile  
 PostPosted: Tue Jan 10, 2012 11:33 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
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


Top
 Profile  
 PostPosted: Wed Jan 11, 2012 2:22 pm   

Joined: Mon Jan 09, 2012 12:52 pm
Posts: 5
If anyone is interested I put the updated script on my blog here: http://g00t.com/?p=709


Top
 Profile  
 PostPosted: Tue Jan 17, 2012 10:04 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
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?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP