LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Archiving files using a time stamp. (https://www.linuxquestions.org/questions/programming-9/archiving-files-using-a-time-stamp-559163/)

HunterColt22 06-04-2007 05:36 PM

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.

ErV 06-04-2007 07:57 PM

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...

HunterColt22 06-10-2007 12:41 AM

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.

ErV 06-11-2007 01:29 AM

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?


All times are GMT -5. The time now is 11:18 AM.