LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2007, 05:36 PM   #1
HunterColt22
LQ Newbie
 
Registered: May 2007
Posts: 4

Rep: Reputation: 0
Archiving files using a time stamp.


Hey everyone. I have a quick question, in a class I am currently taking right now, one of our projects for the end of it is to write a script that is able to archive all of our files in the home directory based upon their time stamp for the month and year they were created/last modifided. Now, my teacher has suggested an array to do this but the thing is, I have no idea how to even begin writing this script. So I was wondering if you guys could give me some help and pointers in the right direction, because as of right now, I am completely lost on this one and this is the only script I have left to write for it. I know I need to start off the script by searching for all the files in my current directory by giving the ls command, but after that, I am not sure how to tell the script if it sees a file with a certain time script to place it into a certain folder. If you could help me answer this it would be great, thanks again. Oh, almost forgot, the language we are using to write these in is bash.
 
Old 06-04-2007, 07:57 PM   #2
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
An array in bash script is declared like thie
Code:
A[0]="123"
A[1]="234"
A[2]="345"
and it's elements are accessed using indexes:
Code:
I=${A[0]}
You should read "info bash" for more information.
Or you can take a look here, this can be a bit complex, but it uses arrays, loops, etc. If you'll skip everything related to iptables in that post, You can find definition and use of arrays.

But I recommend you to use "info bash" first...
 
Old 06-10-2007, 12:41 AM   #3
HunterColt22
LQ Newbie
 
Registered: May 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, I have and got a little farther, and once again stuck. This time though, it is more on the wording and logic of using an if statment to add an element into my array. Here is what I have so far for my script.

#!/bin/bash
#Colt Whitaker
#May 29, 2007

#This shell script is supposed to be able to archive/ back up all my files on my home directory
#Into new and different directories dependant upon there date stamp.
#I am trying to see if an array is my best option
for filename in $(ls)
do
ls --full-time $filename
done

ls --full-time $filename | sed's/.*\([0-9]\{4\} - [0-9]\{2\}\).*/\1/'

stamp = ls --full-time $filename | sed's/.*\([0-9]\{4\} - [0-9]\{2\}\).*/\1/'

delcare -a dates
declare -i i

for(( i=0; i <${dates[@]} ; i++ ))
do
if [[ $stamp == ${dates[i]} ]]
then
#Dont do anything
else
#Add the element into the arry

This is where I am stuck since I am unsure of how the else statment should look since I am not sure if I should say; "else ++1" Or something along those lines. Help would be appriciated again. thanks in advance.
 
Old 06-11-2007, 01:29 AM   #4
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by HunterColt22
This is where I am stuck since I am unsure of how the else statement should look since I am not sure if I should say; "else ++1" Or something along those lines. Help would be appreciated again. thanks in advance.
1) To add element to array just use an index bigger than any indexes used before.
2) "i++" in bash looks like
Code:
i=$(($i+1))
Maybe there are nicer-looking way to do this, but it works and works faster than using "expr", etc.

There were loops in the script i've provided link to. Example:
Code:
while [ "${PAID[$i]}" != "" ]
do
#...
    i=$(($i+1))
done
This is standart "while" loop, but in this example empty string marks an end of array (so I could add new entries easily), and array size isn't explicitly specified.

Did that answer you question?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Time stamp on uploaded files incorrect. Helfrick Linux - Networking 5 10-22-2006 11:44 AM
Incorrect time stamp on uploaded files atif-1 Linux - General 1 12-21-2005 05:01 PM
Time stamp Kalyani1 Linux - Software 0 11-07-2005 02:58 PM
Time stamp in Samba is 11 hours behind time stamp in Linux Linh Linux - General 3 09-04-2003 12:44 PM
Remove files by time stamp evione Linux - Newbie 1 07-15-2003 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration