Saturday, October 30, 2010

Linux Lessons part 1 Lesson 2

In the previous lesson, we discussed how to create folders with a common name, but with different number, e.g. a year number.
Our Case was to create folders, that our photos would sort out by their year.
E.g the photos taken in 2009 will go to the photo2009 folder.
Now, the first thing to consider is the folder reference. We won't speak about folders. In windows you call them folders. In Linux you call them directories. So you have 11 directories from 2000 until 2010. Is that ok? (If you have from 2001 until 2010 see again the previous lesson because there was a mistake in the code)
Now, in another directory, or in the same, you have 1000 photos or more, which have taken from 2000 until now.
Let's see one by one, our new tools we will need to sort them out.


The program date, outputs the current date and time, if no arguments are given.
If you write in your terminal: man date , you will have a manual, or help which explains the arguments date can take.
There is an argument -r, which says

So, if we write date -r filename the output will be the date of the last time we modified that file.

But we only want the year taken (in our case).
How do we do that?
If you look in the man date again, you will notice a FORMAT part.

So, if we add a Format argument, the %G we'll get only the year.
Notice the syntax of date:

That means, write date, then the options (or arguments), the file and then the format after a + symbol.
In conclusion we write this:
date -r filename +%G

So this is the date instruction!

A variable is a random name we use to put things in it.
E.g name=Vasilis

name, mynumber and myage are variables.
Vasilis, 23 and 20 are the contents of each variable!
In lesson 1 we used in the for statement a variable! Can you guess its name?
Answer: Yes, its name was 'i'

To put an output of a program or command in a variable we use ``.
e.g. dateofphoto=`date -r filename`

The mv instructions moves a file from a place to another.

change directory

So, now we have our tools, we also have our folders.
So in your terminal write each line below followed by enter.

cd /path_where_the_photos_are
for pics in *;do date_taken=`date -r $pics +%G`; mv $pics photos$date_taken; done

For any inquires or errors you notice let as know by:
email, facebook wall, or commnent.

No comments:

Post a Comment