Sunday, July 30, 2017

Linux Basics To Break out of Windows Part 1

  1. Introdcution to Linux --- why Linux

  2. Linux Architecture  with Introduction to shell

  3. Linux Commandos- yes Commandos

  4. Redirecting I/O and Pipes

  5. Files and Their Permissions

    1. Octal Way

    2. The chmod

  6. Copying around

    1. cp , mv, ln

  7. Regular Expression for  Shell

    1. Star Lord and Its Crew (* , ? , [] )

    2. The Escape Plan

  8. Dealing with Files

    1. ls , cat

    2. grep

    3. find

  9. Shell Revised

    1. A bit of shell script

  10. To Compress or Not to Compress is the question

    1. tar

    2. zip

  11. man command


Introduction

As much as some want to stay comfortable with windows and explore less of the machines they use, the power of Linux in the era disruptive technologies is undeniable. Thus with out further  ado lets break out of windows. But why?
If you are a person who likes to share your intellectual property for betterment of humanity
fan of open source projects
Believes  collaborations brings excellence
Want to work on your imaginative ideas 
but know less about linux
You want to stay fit  and extra efficient in your coding cardio, if you are a developer.
And anyone else who feels  like breaking out of windows just for the thrill of it. This tutorial is for all of us. Black, white, asian, or even Martian.

DISCLAIMER

The writer of this article will not be hold accountable for any property damage, including windows and gates by people literally inspired by the blog, nor is he in any way in dispute with Microsoft Inc. products and services.

Architecture

All Unix and its pre/desceor  are one of the most programmer-friendly platforms ever created by man kind.
GNU/Linux is Free and Open source. Meaning you can have it for free plus you can tap into the source code when ever you feel like it.

 Linux Environment and Its Editor(s)
The operating system is independent of the operating system commands, which are command to display files, show contents of directories and many others. Thus “Kernel” is left with the necessary functionality of the o.s. The rest are executables on top of kernel, that can be changed, enhanced or even replaced. One of which is the command processor named shell.
Shell  is a program that takes command- line input , and picks what program(s) you are asking for and then runs those programs. Basically it's a dude disguised as kernel right hand. It is the user-interface.

Redirecting I/O and Pipes

 Redirecting I/O : Means it shift the inputs and outputs to your desired source and destination
 it's based on standard I/O
⦁    Any Linux process has Standard in, out and error : named as the three musketeers of open file descriptors.
Standard In: Source of input for the process e.g input from keyboard, file
Standard Out: Destination of the process output e.g output to Monitor, file
Standard Error : Destination for error messages e.g output to Monitor, file

this can bring great flexibility to your tasks.
Redirecting I/O is done by “<” for input and “>” for output
  example command line and type ls

aelaf@localhost:~$ ls
bin  Documents  Downloads  Music  Pictures  Public  Templates

aelaf@localhost:~$ ls >output_file
aelaf@localhost:~$

in the first command the output is displayed on the  screen you and you and I can see it(de facto location of standrard out), but on the second command you are redirecting the output to a file named “output_file”. Open the file to witness the magic.  As for error, to redirect standard error use “2>”

~$ ls ET_file 2> error_file
~$

exercise: the command 'sort' sorts by the first character of each line, try to sort contents from a file and redirect the result to another file, can you sort the file itself ? Why not?
   

Pipes: 

Its similar to Block chain production
 The output from one command given as an input to another command.
$ ls | wc > wc.fields
$ java MyJava < data.file | grep -i total > out.put

the first example runs ls then pipes its output to → wc(word count) program in return output is  redirected to the file wc.fields

the second example runs java with class file MyJava with argument input from data.file and the output will be piped into → grep in return output into out.put

Morale Lesson: Modularization of functions into small, reusable units. And can be interconnected with other commands to do more.

Files and Their Permissions


FileNames
Stay with alphanumeric, period and underscore,as puncutations have speacial meaning to the shell. Or you can escape them but its tedious.
 Filenames are case sensitive JackSparrow.pdf and jacksparrow.pdf are different pirate files.
Avoid using spaces, as shell uses it to delineate between arguments. Or you have to put the name in quotes when using it in shell.
Unlike windows period or “dot” in linux has no special meaning.
In a file name Mummy.avi  has .avi are just the last four characters.

Permissions

There are three categories: The owner(creator), the group(buddies), and the others(neither the creator nor buddy). Any file has a single owner and, simultaneously, to a single group.
It has separate Read/Write/Execute permission for its owner,group and  others.

If you are owner of a file, as well a member of the group that owns it, owner persmission  is valid to you. If you are not owner,but member of the group, then group permission is valid.
Other get the “other” permissions. They are the outcasts afterall.

Octal way of Permission

Read/Write/Execute as three bits of a binary number,
the most significant bit represents read,
the middle bit represents write,
and the least significant bit represents execute.
Taking the three categories, User/Group/Others as three digits, permissions of a file can be expressed as three octal digits.
22    21    20    Bit
4    2    1    Numeric
Read    Write    Execute    4+2+1 = 7

Example:The permission of 750 octal digit is as follows
R/W/X (4+2+1)for the user, R/-/X (4+1)for the group and -/-/- for others.

The chmod way:


 uses letters to represent categories and permissions.
'u'-for user, 'g'-for group, and 'o'-for others, as for permissions 'r'-for read, 'w'-for write, and 'x'-for execute.For both categories and permission a represent 'all'.

To add permission use plus sign(+)
to remove permission use minus sign(-)
example: g+a means: add all permissions to the group categories
a+r means: add read permissions to all categories
what does those mean? - a+a, u+a, g+rx

File Copying --- mv – cp – ln


mv[move]-move file around directories and rename file name

    $ mv Dart.java DartX.java  <-- rename a file
   
    $ mv MyApp.java ./Document/App.java  <--move to sub-directory      Document and rename a file if same file name exist rename it

    $ mv One.java Two.java ..   <--move the files up one directory
    '.' points to the current and '..' points to the parent directory

    $ mv /usr/oldproject/*.java . <--moves all java files from the directory “oldProject” to the current directory

cp[copy]- similar to mv but the original file is left untouched

ln[linked copy]- vaguely similar to cp, the change in the original file is reflected to the copied file.a.k.a shortcut in windows

    ~$ ln chuck ./Document/chuck2
     change copy the reference to another file named chuck2  any change to chuck, and you will see it from the face of chuck2


Star Lord and Its Crew



No, its not about saving the Galaxy, its rather more interesting ;-) → It is used in shell pattern matching

$ mv /Documents/Projects/*.java .
 The star is a shorthand to match any character with in comination to the .java, will match any file  in the /Documents/Projects. try to finish the rest  of the explanation on the command.

 Two things to note here!

First, the Star and rest of the crew “? and []” don't mean identically same as the regex in vi or other programing languages.Although similar.

Second, the pattern matching is done by the command interpreter before the arguments are given off to the specific command. Any text with those characters is replaced by the shell with one or more filenames that match the pattern.This means, all the Linux commands(be it mv,ln,ls...)only receives the result from shell.

Exercise: Try to explain the advantage of shell doing the regex job,then handling it over to the commands.

?:will match any single character
[…] or Bracket Match: It can matches any of the individual character inside
    example: [abc] matches any of a or b or c
    example2: Dart[123].java would match Dart1.java,Dart2.java,Dart3.java but not Dart12.java

exercise: what would those match to: Dart*.java and Dart?.java
Bracket Match can match a range of charaters like [a-q] or [0-5].If first character is '^' or '!', it means the meaning is reversed. Dart[^0-9].java will match non-numeric alphabet following Dart,like DartX.java but not Dart1.java.If you want to match “-” or “!” or “^”   Don't put it first,and you are clear to take off.

Short hand for common ones : [:name:]  where name could be one of the the following: alnum(alphanumeric),alpha(alphabetic),digit,punct,upper...
example [:alpha:] and [:punct:]

The Escape Plan


Yes there are times,where you don't want to live by the rules, and probably  time travelled and make a great deal with Incas before the spaniards make it to the shore.But this is not about it.
If you want the aforementioned and Lenios Travos knows the rest special character to be just character, that mean nothing to the shell.
Either precede it with a backslash or enclose it in single quotes.
Example: $rm MyApp\$1.java aremoves a file named MyApp$1.java a in the eyes of Shell $ is a variable or
    $rm 'MyApp$1.java' the special character have escaped extreme vetting from shell, for now.
Note: if filename contains extra alphanumeric,underscores,or periods, better to gear it up with the escape plans.

Dealing with Files


$ls . // displays files and directories in top bottom and left right order

$ls -l // which is long listing displays additional permissions, links,owner group size and date of modification
aelaf@localhost:~$ ls -l
total 48
drwxr-xr-x 2 aelaf aelaf 4096 Jul 26 19:17 bin
-rw-r--r-- 2 aelaf aelaf  200 Jul 27 10:41 chuck
-rw-r--r-- 2 aelaf aelaf  200 Jul 27 10:41 chuck2
drwxr-xr-x 3 aelaf aelaf 4096 Jul 27 10:25 Documents
drwxr-xr-x 2 aelaf aelaf 4096 Jul 26 19:17 Downloads
-rw-r--r-- 1 aelaf aelaf    0 Jul 27 05:50 error_log

To study the inside of a file use file command, which takes as arguments a list of files, single or not. It does look at the data blocks of the file


 Do examine the following result
aelaf@localhost:~/Pictures$ ls
Screenshot - 07272017 - 04:52:04 AM.png  screenshots  wallpapers
aelaf@localhost:~/Pictures$ file *
Screenshot - 07272017 - 04:52:04 AM.png: PNG image data, 673 x 173, 8-bit/color RGBA, non-interlaced
screenshots:                             directory


it tells a great deal about the image.




Cat : display file content if file has many lines of codes better to use more  or less .  And head command to check the top few lines of a file. The tail command displays the last few lines of a file, with a special parameter -f. -f tells the tail to display result and wait and try again. Excellent way to handle log files on the run. ^c(Contrl-C) will stop the tail command
$ tail -f error.log
displays the last few lines of a 
Do experiement on those commands on your favorite file

The grep


Relax it's not a Genetically Modified grape you must eat or die.It's Generalized RegEx Processor, a tool for searching through the contents of a file, a great tool that you really must know and its a killer

$grep mContext *.java
will search and display all lines from all *.java file(s)that has a string mContext. Remeber *.java is handled by the shell NOT grep. The    first non-option parameter which in this case is  mContext is considered a regular expression(it can have all the stars and it's crews)

$grep println MyApp.java | grep -v System.out

Search for println on every line, excluding with the ones with System.out and displays to standard output.

$ cat MyApp.java
class MyApp{
    File file = null;
    public static void main(String[] args){
        System.out.println("My App working");
        file.println("file working");
        println("just printing");
        }
    }


$ grep println MyApp.java
        System.out.println("My App working");
        file.println("file working");
        println("just printing");

matches the lines that contains println and displays

$ grep println MyApp.java | grep -v System.out
        file.println("file working");
        println("just printing");

matches the lines that contains println but excluding those System.out and displays the result

use grep --help for more actions?!?!?!?

The find Command


It has predicates – logical expressions that causes actions,and have boolean values that determines if the rest of the expression to execute or not to execute.

~$ find -name '*chuck*'
./chuck2
./chuck
./tmp/chuck2
./tmp/chuck
Looks for files with substring chuck in the current directory and descends into all subdirectories. Not excited, ok... windows can do that? well not the next one

~$ find /over/there . /tmp/here -name '*Activity*.java' -print

It looks for a java file with a name that has Activity by first searching into three underlined  directories.

-name is the predicate,it takes a regular expression as an argument. Any file that matches that regular expression pattern result true, thus the control passes onto the next predicate e.g -print
Explain the following  find predicates
Options    Explanations
-type d    True if file is directory
-mtime -5    True if file is less than 5 days since last modified; + is older than
-atime -5    True if file is less than 5 days since last accessed
-newer App.java    True if file is newer than App.java
-size +10k    True if file is greater than 10k in size


Exercise:Explain the following command
~$ find . -name '*.java' -mtime +90 -atime +30 -print

Shell Revised


Shell- the command interpreter – yes the one you handle all your commands to, can be considered a programming language in its own merit. There are an entire Books about Shell Programming.
For this tutorial we are going to use the spirit of shell scripting,working with shell variables.
By convention shell variables use uppercase, it clears possible ambugity between commands that uses lowercase.

$FILE=./chuck  <--assign variable with the file content of chuck-->
$ export FILE <--variables passed on to other environments-->
$ cat $FILE <–- display the variable content, use $-->

you can use shell variables already exported,you can alter them(their  contents) not their states obviously(no need for export keyword)

PATH(FAMED SHELL VARIABLE): It defines the directories in the filesystem where the shell will look for programs to execute,programs or commands like ls, cat, javac
$echo $PATH
//home/aelaf/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Each directory in the PATH variable is examined by the shell for the executable you want to run. Note each directory is separated by a colon':'. If you are struck by lightning “command not found error”, it means it's not on your PATH. Usual struggle when you are setting java path for first time, and run it from command line.

You can search where commands are located by typing 'which <command_name>'
$ which ls
/bin/ls

If you have a command, and want to execute from command line, you either type the command's full path or you can set your PATH variable to include the location of the command's executable.
More practicality on that topic in next lessons under setting up environment for Java.

Just... A  Bit about Shell Script

When shell boot up, it reads initializations from files, Those files are read and executed by shell as if someone is typing. One of their actions is setting PATH. Shell Scripts are shell commands stored in a file for efficiency and other benefits

Example: create a file name myscript with content “ls -l” then, change the mode by typing chmod a+rx myscript on command line  to allow read and execute for all. Then call the script from the command line. And the script will run like it has never run before.

The tar and zip Commands


used for packing data into archive and extract it back.They provide loosless data compression, no wonder they are everywhere.

The tar action specifiers alphabets are:
c:Create Archive | x:Extract from Archive | t:Get a table contents | f: the next parameter is the file name of the archive | v:more verbose output

Try to explain the following commands
$ tar tvf packedup.tar
$ tar xvf packedup.tar
$ tar cvf packedup.tar myTar

As for zip you can easily tell the process
$ unzip -l packedup.zip <---gives table of contents of the archive file-->
$ unzip packedup.zip <---extracts all files from the zip file-->
$ unzip -r packedup.zip <---create zip archive, while recurrsivelly go down zipping subdirectories if there are any →

Example: Explain the Command
$ find . -name '*.java' -print | zip src_file_zip -@

tip: -@ let you read from standard in rather than expecting from argument list

The man Command:


Short for manual.existed ever since Dinos roamed with  Unix in 70's. A great way to easily learn about command
$ man <command_name>
$ man find