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.

1)date:

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
Voila:


So this is the date instruction!

2)variables
A variable is a random name we use to put things in it.
E.g name=Vasilis
mynumber=23
myage=20

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'

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

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

5)cd
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
enter
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.

The power of Linux, Lessons for Dummies part 1

Here is the beginning of a big Linux tutorial.
In most of the lessons we'll use bash.
Bash is the common Linux Shell, and stands for Bourne again shell!
The tutorials will have 2 parts: The case, that's the problem we face, a solution with common Windows knowledge, and a solution with Bash Script.
Vaslabs trials have made in an Ubuntu 10.04 Lucid Linux Machine, but there is no difference at all in every distribution you have. If you are new to Linux, Ubuntu is the best choice for you.

The Case:
We want to create 11 folders, each one will have the name photos and then a year number starting from 2000. In these folders we will put later some photos according the year they have been taken.

Solution:
1) In windows, or in Linux using file browser:
Go to the place you want to create the folders, right click, create folder, give it a name, e.g. photos2000, and keep doing that until you reach photos2010.
2)With a bash script:
So we want to make 11 folders in a specific location. It is not necessary to go to that location, so we won't change directory. Since you are new to Linux we won't even use a for, but will suggest that best solution later.
Even without a for the solution is faster.
So for that job will need:
1)a terminal (to write the commands)
2)the command mkdir which means make directory
3)the up arrow(it's on the keyboard)
4)and the path we want to make our folders.

So a simple solution is:
open the terminal.
write: mkdir /path/photos2000
press enter.
A folder named photos2000 has been created in /path.
then press the up arrow
The mkdir /path/photos2000 will show again.
Press backspace one time and then press 1.
now you will have mkdir /path/photos2001
press enter. etc...

But that is a very simple, and stupid solution. This is file browser thinking.
We can automate the above procedure by adding a for command.
So we'll need a couple of extra tools:
1) the for command
2)the let command (let allows mathematical operations)

So write that script instead.
for ((i=0;i<=10;i++)); do let number=2000+$i; mkdir "photos"$number; done
And press enter. The folders shall be created successfully. If you have any problems, don't
hesitate, write your problem on our facebook wall, or here as a comment.