Saturday, May 28, 2016

10 Simple ways to work with the `ls` command in Linux

So, today we'll work with the `ls` command in Linux
& see how it can help us to know about directories & files on our system.

`ls` helps us list the contents of the directory/folder that we are currently in, or that we specify as a parameter to `ls`, and lets us understand about other details of the files/directories like, the permissions on them, owners/groups they belong to, 
last modified times on them, their size with some other functionalities.

Listing names of the contents in the current folder

 1.txt  b.b  bench  bench_apache  bench_nginx

Listing contents with summary (Name and size in Kilobytes) 

ls -s
total 20
0 1.txt  4 b.b  4 bench  4 bench_apache  4 bench_nginx  4

Listing contents with long list format (Name and size in Kilobytes) (It is kilobytes)

ls -l
total 20
-rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
drwxrwxr-x 2 ash ash 4096 May 29 00:03 b.b
drwxrwxr-x 3 ash ash 4096 May 22 19:30 bench
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_nginx
-rwx------ 2 ash ash  243 Mar 10  2014

Listing in long listing & human readable format

ls -lh
-rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
drwxrwxr-x 2 ash ash 4.0K May 29 00:03 b.b
drwxrwxr-x 3 ash ash 4.0K May 22 19:30 bench
-rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_nginx
-rwx------ 2 ash ash  243 Mar 10  2014

Listing in long listing & human readable format

ls -lsh
total 20K
   0 -rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
4.0K drwxrwxr-x 2 ash ash 4.0K May 29 00:03 b.b
4.0K drwxrwxr-x 3 ash ash 4.0K May 22 19:30 bench
4.0K -rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_apache
4.0K -rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_nginx
4.0K -rwx------ 2 ash ash  243 Mar 10  2014

Ignoring files & only listing directories

(Caveat: This will ignore any file/directory which contains a .(dot) in its name. Sometime, directories contain a dot in their name so they might get ignored as well so watch out, and sometimes, files do not contain a dot in their name)
ls -lI "*.*"
drwxrwxr-x 3 ash ash 4096 May 22 19:30 bench
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_nginx

Listing files and folders in the current directory {Having a dot (.) in their names}

ls *.*


List the files & folders in the current directory's, & further files/folders inside the folders in current directory (to Depth 1)

ls *
1.txt  bench_apache  bench_nginx


b  b.txt

List all files along with hidden files

ls -a
.  ..  1.txt  b.b  bench  bench_apache  bench_nginx  .hidden

List Files & Folder contents starting with a Name/Pattern/Alphabet

Eg. List all the files starting with the alphabet 'b' & folder contents starting with the alphabet 'b'
ls b*
bench_apache  bench_nginx


b  b.txt

Thursday, May 12, 2016

A few things to do with screen command and screen session in Linux

Today we'll try to see a few things we can do with the screen command.
If you wish to have multiple bash terminals running in the same terminal window 
without creating multiple terminal windows or terminal tabs, 
screen is the command you would want to aim at.

So let's see what options does screen command gives us:
screen --help

And, we get the following details about the screen command.

Currently running screen sessions

screen -ls

Attaching a Detached screen sessions

If a screen session was detached from a bash terminal window, 
and you wish your current bash window, to attach the that screen session process, 
you can just type the following.
screen -r <process_id.your_screen_session_name>

You could also use only the your_screen_session_name without the process_id if the multiple screen sessions are named differently, like this:
screen -r <your_screen_session_name>

Detaching & Attaching an attached screen sessions

If you were disconnected from a screen session somehow, and the screen session never got detached,
you can detach it from the bash terminal where it is attached to, & attach it to your current bash terminal window, 
and start working with it.
screen -D -r <process_id.your_screen_session_name>

Killing the whole screen session

To kill the whole screen session, type:
Ctrl-a \

To Kill a window in a screen session

Let's start a screen session with total 3 windows created
screen -S Local_Server -t my_local_server_1
Type the following 2 times, to create 2 more screen windows:
Ctrl-a c
Now to kill a window in screen,  go to that window
Since you created 2 more screen window in the same bash terminal, by typing the above command 2 times.
You will now have total 3 windows listed, starting from the number 0 to 2, inside screen type:
Ctrl-a Shift-"
We can see 3 Windows like this

To scroll up the window in screen

If a lot of output has been printed on your terminal while in a screen session,
and you would want to scroll up & see what got printed, you can type:
Ctrl-a Esc k
Ctrl-a: The escape character
Esc: The Escape key, to enter into scroll mode
k: To scroll up
Others you can use:
j: scroll up
h: move cursor towards left
l: move cursor towards right
You could also use the arrow keys instead of, j,k,h,l.
Type Esc once key to exit scrolling.

Changing the escape character in screen from Ctrl-a to other

Open the ~/.screenrc file that you should be having in your home folder (/home/your_username/.screenrc), if you don't then you can just create it.
Let's change the escape character from Ctrl-a to Ctrl-z
Enter the following in your ~/.screenrc and save it.
escape ^zz
Now start screen & your escape key has been changed to Ctrl-z from Ctrl-a

Monday, April 25, 2016

Working with Python Virtual environment on Ubuntu

Virtual environment (virtualenv) helps us have different python versions & different python libraries and their versions installed on our system in isolated environment (environment in this case is basically a folder/directory) without affecting our global system python version & its libraries.

Installing python virtual environment is not too difficult on your system.
Just open the terminal ( Press ctrl+alt+t in Ubuntu. You may have to check the key combination for your distro) and fire the command

pip install virtualenv

this will install virtual environment on your system, and provide you with a `virtualenv` command

Now we can create a folder for your python projects,
which will give you an environment to work on,
isolated from the system.
Type this on your terminal

virtualenv my_virtual_env_folder_name

You can also provide a python version to use in your project like this:

virtualenv -p /usr/bin/python2.7 my_virtual_env_folder_name

If you check this folder, it should have a few directories like these:
(Note: `my_virtual_env_folder_name` in my case is `test`)
ls -lsh my_virtual_env_folder_name/

Enter into the folder, 

cd my_virtual_env_folder_name

And activate the virtual environment.

source bin/activate

This will change your current python environment & set it to this folder `my_virtual_env_folder_name`

So now that we have activated the virtual environment, we can see it on the terminal.
This shows you that a virtual environment has been activated in the terminal,
by showing the name of the virtual environment, like this:

And now everything you install with `pip` or `easy_install` should be installed, in the 
my_virtual_env_folder_name/lib/pythonX.X/site-packages folder
{X.X denotes a version of python, like, 2.7, 3.4 etc}
For eg.
pip install requests
pip install lxml

And not the global site-packages and dist-packages folder, which can be found at these locations

So that's how you work on isolated python environments.

To come out of the virtual environment, you can type deactivate on the terminal and exit it, like this

This will deactivate your python environment & set it to global default.

Tuesday, March 15, 2016

Git aliases: Shortening Git commands

So today we'll see, how we can shorten the long git commands, 
which can help programmers save time and a few keystrokes while working with git.

So lets work with git a bit. (Git a bit. Lol :p, a rhyming scheme. Does that help me qualify to be a poet, song writer?, umm... a rapper maybe? Maybe i should write a rap on this, yo mayne...! )

We'll see 2 ways of adding aliases to our git commands.

How to add aliases to git commands: 

Way 1: Using the `git config` command

Use the git config command to do the same.

So lets add an alias to the `status` command of git as `s`

git config --global alias.s status

Lets add another for `add`

git config --global alias.a add

Another for `commit`

git config --global commit

And another

git config --global alias.cim "commit -m"

Now, we can just fire the short commands instead of the long ones and see the result of the command

for eg. git status, we can write,

git s

Lets break down the above command & take a look at the options we passed:

config: This option tells git, that everything following should be treated as the configuration for git, and can be added in the .gitconfig (or .git/config file depending on other options) file.

--global: The `--global` option tells git to add the alias in your '.gitconfig' which is in your home folder,

which will be treated as your global settings for all git repos on your system, 
unless you have another .git/config file inside a particular repo, overriding the global .gitconfig. 
Try using the `--global` option since it will help you keep the same aliases across all repositories on your system, if you have a number of them.
Without the `--global` option git will consider the local repository by default.
Caveat: If you already have the <short_name> as an alias to some other previsouly added command in the config, the --global option will replace that previous one.

alias.<short_name> <git_command_name>: Tells git that <git_command_name> particular command will have <short_name> as its alias form.

Way 2: Using the .gitconfig file directly

If you see in the ~/.gitconfig (~ = your_home_folder which is, /home/<your_login_name>/.gitconfig ) file,
you will notice that the shortened commands have gotten added under the [alias] section
Like this:

So instead of firing a `git config` command, lets try adding, the aliases directly into this file.

Lets open the file,

vim ~/.gitconfig

go under the [alias] section & type 

rl = reflog

& save it & the file now looks like this.

And now we can check the `reflog` command too, by typing
git rl

So, there we have it.

Let us know, if the above post was of any help to you.
Until later.

Oh ya! The complimentary rap i mentioned.


Rap starts...

Hey yo! Let me tell you a few things about git,

but i'll teach you them, uh! bit by bit.
aye! but, bit by bit, is a 1 bit. (Suddenly, the rapper gets into Math/CS/Binary mode & talks about, Division/Binary division, as, bit by bit, bit_divided_by_bit, is 1bit/1bit is 0b0001/0b0001 = 0b0001 = 1),
oh my gosh!!! what is this sh*t??? (And.. the audience go...). 



Monday, March 7, 2016

Working with Postgresql helper commands

Hi Friends,

So today we'll try to learn a few things on Postgresql and
see what exactly Postgresql 'Helper commands' are.

Apart from the good old sql queries that you can certainly use,
Postgresql also provides us with Helper commands which you can use
to fetch information from the Postgrsql DB.

You can fire the Postgresql Helper commands from the Postgresql command prompt.

So lets login into the Postgresql command prompt.
`psql` is the command line utility you would need for this, so lets begin.

Open Terminal (`ctrl+alt+t` in Ubuntu, check for you particular distro)

psql -h <host_name> -p <port_number> -d <database_name> -U <db_user_name> -W

Type in your db_password in the password prompt.

So now since we have connected to out database, lets see a few commands.

Check for All the Databases on the server

Check for size and access privileges of a particular Database
\l+ <database_name>

List Tables in a DB

List Tables in a DB with Size

Details about a particular table
\dt+ <table_name>

Describe a Table in a DB
\d+ <table_name>

Describe table with size and description (with pg_catalog schema)
\dtS+ <table_name>

Change/connect/move to a different Database.

\c <new_database_name>

Will try to come up with more blog post where we can share our information with each other.

Please let us know if you have any comments about the blog in the comments section below or if there are any corrections you may have spotted and you would like us to make those corrections.

Thanks a lot for taking your time & stopping over at the blogpost.
Have a good day!

Saturday, October 25, 2008

Wat's the Price of that Mango ???

Artificial creation of Demand.

Sometimes i wonder,
How come the Mangoes in the summer season last up until ... the rainy seasons???

Mangoes start arriving in summer which is the Feb-March month(according to mumbai's weather situations) & they last up a long time, almost till the rains arrival(July).

Is there no one in the City buying Mangoes ?

According to a lot of Interviews that i have read of different people in the papers, according to different articles, surveys, studies, etc --- every Tom, Dick & Harry
seems to be loving Mangoes.
(i too am one of them, Hehee :D) (mind you i said 'Loving' not just 'Liking').

Mangoes are on the top of the list of any & everybody in the City.
we call Mango, the King of Fruits.
we all just Love mangoes(the form doesn't matter either).
Mangoes will be the first choice for the major population in the city, if given a choice.

So according to these studies, surveys etc,
Mango is the fruit everybody likes to eat, we all buy it.

So then, how come the Mangoes aren't finishing a little early, like in the end of summer itself, how come they last till the first month of rainy seasons ?

The rains have set in, its almost July, but we still see Bhaiyaji's selling mangoes with their Trademark Blue colored plastic material(which has got Holes in it, in order for some rain water to enter inside the tent, dunno why???) over their head to save their Mangoes from the Rain lashes.

so then :
What happens ? How come ?

May be this is what happens :

There is News everywhere in all the papers, on all the channels ...
Everywhere there are Headlines , stating that :

Due to unseasonal rains, a large amount of Mango crop was destroyed ...
Due to Drought in a lot of places,
There was less Water,
Water supply wasn't readily available, proper Irrigation also was an issue,
due to which Mango has not been produced in sufficient quantity,
like it was produced in the Last year, & this time its lesser than the last year.

all these News forces people to believe there is less Mango supply.
(the above line in 'economics' jargon will be termed as, Supply < Demand )
& this is immediately followed by, another consequence,
Mango prices suddenly shooting up, sky rocketing Mango prices.

1 dozen Mangoes (12 Mangoes) are costing around Rs400-Rs450 bracket!!!

A lot of people are still buying it, though they don't like that price, but as it the King of all fruits, we need to get a Taste of it, atleast once a year.

According to above situations,
the Mango crop being destroyed & other Bad News etc etc ,
the Mango crop has to be less in supply,
which means it should have been finished pretty early,
sometime around in the end of Summer,
but we still see Mangoes being sold till end of July - the Rainy seasons.

And now, this time around, if you go to buy the Mango(in July) ,

you suddenly find yourself paying Rs200 per dozen
(the Bhaiyaji says, 'thik hain memsaab 200 rupay mein dozen le jao')

the same Mangoes selling for Rs450 at the start of the season, are being sold for Rs200 in July.
Only, because there is an Excess stock of mangoes, which the Bhaiyaji's are finding hard to finish, they then start selling it for lower prices, as there is a Huge stock still left to be sold.

All This makes me feel,
Was there a shortage of Supply in the First place itself ???

there wasn't any Shortage of Supply.

all that bad news (of Mango crop destroying) was just created by people related to the business(most probably the 'Netas'), in order to Push the Mango Prices up.
so as to get double, thrice, four or even five times of the prices of the same Mangoes , which will be sold for Half the price in the End of the season.

All this was just done to
Artificially create Demand(by reducing supply-which never happened in reality) & push prices Upwards.

Now, that is some cool Economics concepts under work here.