Introduction to Linux

Why Linux?

Package Managers

  • Linux operating systems enable download of third party software through package managers unlike Windows.
  • No need to face setup / dependency hassles while installing software.
  • Ubuntu uses the "apt-get" utility as a package manager.
  • Typing "sudo apt-get install" is all you need to do :D

The Terminal $

  • Linux's Terminal is much more powerful when compared to Windows' Command Prompt

  • The Terminal can practically do anything. Delete everything on your computer too.
  • Make it your best friend as soon as possible.

The "Developers" Feel

  • As a Linux user, one would need to spend a lot of time on the Terminal. "Typing" feels a lot more like a programmer when compared to "Clicking"

  • Linux has several flavors, each having its strength and weakness. You can choose the distribution to suit your needs.

Getting Ubuntu

Dual Booting

  • Dual Booting is a complicated procedure. It highly depends on your Laptop Manufacturer
  • BACK UP all your sensitive data before starting.
  • You must have a bootable disk/pendrive with Ubuntu installed.
  • Here is a great blog post which works for most laptops 

http://ranveeraggarwal.com/blog/ubuntu-windows-dual-boot

 

Ditch Windows for Good :P

Essential Software

Text Editors / IDEs

  • Sublime Text
  • Vim
  • Android Studio
  • Code::Blocks

 

 

Essential Tools/Packages

  • Gimp (Image Editing)
  • LAMP (Linux - Apache - MySQL - PHP)
  • Arduino IDE (Electronics)
  • Laravel (Server Programming)
  • OpenCV (Image Processing)
  • Geogebra (Technical Drawing)
  • Processing (Graphical computations)
  • LinuxDC++

 

 

Sublime Text 2

Type the following commands one by one :-

 

 

 

The following commands are used to open files and folders respectively. Make sure the file exists in that directory. Sublime Text will create it otherwise.

 

sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text
subl filename.py
subl .

Vim

Type the following command to install it and get started :-

 

 

It should be installed by default. Vim is rather daunting at first. Do look at this guide to get started :-

http://www.engadget.com/2012/07/10/vim-how-to/

 

 

sudo apt-get install vim
vim filename.py

Android Studio

Use the following guide to install Android Studio :-

https://developer.android.com/sdk/installing/index.html?pkg=studio

You might have to make studio.sh an executable and then run it. Do the following for the same :-

 

 

 

sudo chmod 777 studio.sh
./studio.sh

Essential Tool Installation

LAMP Server :- http://www.prateekchandan.me/how-to-setup-localhost

Arduino :- 

Laravel :- https://laracasts.com/series/laravel-5-fundamentals/episodes/1

Install PHP-mcrypt too 

Gimp / Geogebra :- 

 

sudo apt-get update 
sudo apt-get install arduino arduino-core
sudo apt-get install mcrypt
sudo apt-get install gimp
sudo apt-get install geogebra

(Continued)

Basic Commands

The Terminal

 

~ represents the home directory.

ls  List all files in a directory.

cd Downloads  Change directory to Downloads

cd .. Go one directory behind

/  Root folder

Press Tab to auto-complete the directory / file name.

 

 

kalpesh@kalpesh-Inspiron-3542:~$ ls

Colors in Terminal

  1. Blue :- Represents a folder
  2. Green :- An executable file
  3. Pink :- Graphical file
  4. Red :- Archive file

Installing Software

 

 

sudo "super do", makes you master in short.

apt-get The package manager of Ubuntu

update Listing of changes in repositories. It doesn't actually download the repositories.

 

 

kalpesh@kalpesh-Inspiron-3542:~$ sudo apt-get update
kalpesh@kalpesh-Inspiron-3542:~$ sudo apt-get install geogebra

Basic File Commands

 

 

 

 

 

 

mv - Move file

rm - Remove file

cp - Copy file

kalpesh@kalpesh-Inspiron-3542:~$ mkdir sample
kalpesh@kalpesh-Inspiron-3542:~$ cp test.cpp sample/test.cpp
kalpesh@kalpesh-Inspiron-3542:~$ cd sample
kalpesh@kalpesh-Inspiron-3542:~/sample$ ls
test.cpp
kalpesh@kalpesh-Inspiron-3542:~/sample$ mv test.cpp xyz.cpp
kalpesh@kalpesh-Inspiron-3542:~/sample$ ls
xyz.cpp
kalpesh@kalpesh-Inspiron-3542:~/sample$ rm test.cpp
rm: cannot remove ‘test.cpp’: No such file or directory
kalpesh@kalpesh-Inspiron-3542:~/sample$ rm xyz.cpp
kalpesh@kalpesh-Inspiron-3542:~/sample$ ls
kalpesh@kalpesh-Inspiron-3542:~/sample$ 

Manual

man <command> - A good way to learn about Unix commands, especially to get details on flags and parameters.

Alternatively, you could learn about commands with the --help flag, or by simply typing the command.

 

You may use a -v or --version flag to obtain the version for the utility.

 

 

File Permissions

Command

 

 

 

 

 

chmod Command to change file permissions

777 New file permissions

./run.sh Execute file run.sh

kalpesh@kalpesh-Inspiron-3542:~/sample$ subl run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$ ./run.sh
bash: ./run.sh: Permission denied
kalpesh@kalpesh-Inspiron-3542:~/sample$ sudo chmod 777 run.sh
[sudo] password for kalpesh: 
kalpesh@kalpesh-Inspiron-3542:~/sample$ ls
run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$ ./run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$

What is 777?

chmod follows the following number code :-

  • 4 represents permission to read
  • 2 represents permission to write
  • 1 represents permission to execute 
  • The permission we desire would be the sum of these numbers. 7=4+2+1 which allows us to read,write,execute the file.

Three 7's?

The three 7's represent the following :-

  • First 7 - Permission given to user
  • Second 7 - Permission given to group
  • Third 7 - Permission given to others

Alternative Command

 

 

 

 

 

- user

g - group

o - other

r - read, w-write, x-execute

kalpesh@kalpesh-Inspiron-3542:~/sample$ subl run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$ ./run.sh
bash: ./run.sh: Permission denied
kalpesh@kalpesh-Inspiron-3542:~/sample$ sudo chmod u=rwx,g=rwx,o=rwx run.sh
[sudo] password for kalpesh: 
kalpesh@kalpesh-Inspiron-3542:~/sample$ ls
run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$ ./run.sh
kalpesh@kalpesh-Inspiron-3542:~/sample$

Downloading Files

Command

 

 

 

wget Simply downloads the file off the network.

kalpesh@kalpesh-Inspiron-3542:~/sample$ sudo apt-get install wget
.
.
kalpesh@kalpesh-Inspiron-3542:~/sample$ wget files.explosm.net/comics/Rob/feel-great2.png

Essential Folders

Programmer's playground

All paths start from the root folder.

  • usr/include/ All your header files come here.
  • usr/lib/ The corresponding library files.
  • usr/bin/ The home of all executable commands. The commands in this folder can be executed from anywhere on the PC
  • etc/NetworkManager/ Configure your networks here

Using these folders

Editing, adding files here are essential for any programmer. You might often get a "Permission Denied" error while doing so.

  • To counter this, we must use the super user mode.

 

 

 

su Super user

# represents su mode

exit A command within su to exit super user mode.

kalpesh@kalpesh-Inspiron-3542:~$ sudo su
[sudo] password for kalpesh: 
root@kalpesh-Inspiron-3542:/home/kalpesh# exit
exit
kalpesh@kalpesh-Inspiron-3542:~$ 

Random Commands

Useful stuff

  • Use Ctrl+C or Ctrl+Z whenever you wish to end/stop a program running in the terminal.
  • Use Ctrl+Shift+C to copy off the terminal.
  • ifconfig Get details about your current network status and IP.
  • find Used to locate files on our PC. Some examples :-

 

 

root@kalpesh-Inspiron-3542:/home/kalpesh# find -name "CS101*"
./CS101 Project
./CS101 Videos
root@kalpesh-Inspiron-3542:/home/kalpesh#
root@kalpesh-Inspiron-3542:/home/kalpesh# find -name "*.ogv"
.
.
./CS101 Videos/2.ogv
./CS101 Videos/video1-6.ogv
./CS101 Videos/1.ogv

Other Great Commands

  • grep
  • top
  • df
  • Piping |
  • fg bg
  • killall <process_name>
  • kill <process>
  • cat
  • which
  • diff

 

 

top

top displays the list of processes running on the PC.

kalpesh@kalpesh-Inspiron-3542:~$ top

top - 17:55:01 up 3 days, 23:06,  2 users,  load average: 0.22, 0.42, 0.54
Tasks: 241 total,   3 running, 238 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.8 us,  0.8 sy,  0.2 ni, 93.8 id,  1.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   3950144 total,  3643452 used,   306692 free,    50056 buffers
KiB Swap:  9764860 total,   549344 used,  9215516 free.   899724 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
 1449 root      20   0  423968  45196  31648 S   3.3  1.1  91:34.25 Xorg        
 1944 kalpesh   20   0 1585324 102952  20916 S   2.7  2.6 105:28.78 compiz      
 3437 kalpesh   20   0  692800  22368  12372 S   2.7  0.6   0:04.20 gnome-term+ 
30971 kalpesh   20   0 1058944 248412  23964 S   2.7  6.3  14:34.22 chrome      
 2257 kalpesh   20   0 1787144 191548  40752 S   2.3  4.8 140:50.06 chrome      
12174 kalpesh   20   0 1115032 286936  23920 S   1.7  7.3   3:37.80 chrome      
18430 kalpesh   20   0 1142680 320316  25092 S   1.7  8.1   0:40.95 chrome      
  368 root     -51   0       0      0      0 S   1.0  0.0   6:50.69 irq/39-DLL+ 
18846 kalpesh   20   0  825392  91484  23408 S   1.0  2.3   0:03.51 chrome      
 2374 kalpesh   20   0  842412  92908   9112 S   0.7  2.4  19:21.30 chrome      
  458 root      20   0       0      0      0 S   0.3  0.0   2:34.79 rts5139-po+ 
 2055 kalpesh   20   0   20496    352    232 S   0.3  0.0   1:04.38 syndaemon   
17889 kalpesh   20   0 1206188 254540  23992 S   0.3  6.4  41:49.72 chrome      
18010 root      20   0       0      0      0 S   0.3  0.0   0:02.93 kworker/u8+ 
    1 root      20   0   33904   2112    768 S   0.0  0.1   0:04.22 init        
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.09 kthreadd    
    3 root      20   0       0      0      0 S   0.0  0.0   0:17.40 ksoftirqd/0 

kill

  • Use kill -9 <process_ID> to kill the process displayed in top.
  • You may use killall -9 <process_name> to kill the processes having this name.

 

 

 

cat

 

 

 

cat swag.cpp
cat >swag.cpp

diff

  • Diff helps compare two files. Often used in git to compare current revision with last commited revision.
  • It essentially gives to instructions to change the first file into the second.
  • Often used with "contextual" output, as 
  • diff -c <old_file> <new_file>

 

 

 

less, tail, sort

  • A number of linux commands could be used with or piping, which is used to redirect output of one command to another.
  • One may test less, tail, sort by redirecting ls output to them as ls | less or ls | tail or ls | sort

 

 

 

grep

  • grep is essentially used to find snippets of text from a set of files.
  • One usage of grep is grep <regex> <file1> <file2>

 

 

 

Shell Scripts

Programming the commands?

What will happen if we get if, for, switch and all the CS101 stuff into commands?

Magic.

The answer is Shell Scripting.

  • Scripts must be made executable to run.
  • Scripts are run in the terminal as follows :-

 

 

kalpesh@kalpesh-Inspiron-3542:~/sample$ ./run.sh

Shell Script - 1

Let's make a small shell script using the things we have learnt.

Type the following in a text editor :-

 

 

 

 

The script automates the download of 24 gif images off MIT OCW. We could do the same for the videos by replacing ".gif" with ".mp4"

#!/bin/bash
COUNT=24
while [ $COUNT -gt 0 ]; do
  wget http://ia902706.us.archive.org/6/items/MIT6.006F11/MIT6_006F11_lec"$COUNT"_300k.gif
  let COUNT=COUNT-1
done 

Shell Script - 2

This script is a little more advanced.

 

 

 

 

 

 

Here we are taking a number input as a command line argument. We could get the same via voice/image processing.

var=$(gsettings get com.canonical.Unity.Launcher favorites)
let counter=-1
declare -a ARRAY
const1=0
for i in $var; do
	if [ ${#i} -gt 23 ]; then
		let counter=counter+1
		var2=${#i}
		let var2=var2-17
		#echo $var2
		if [ $counter -eq $const1 ]; then
			let var2=var2-1
			ARRAY[$counter]=${i:16:var2}
		else
			ARRAY[$counter]=${i:15:var2}
		fi
	else
		sleep 0;
	fi	
done 
cd /
file_to_open=$1
let file_to_open=file_to_open-1
echo ${ARRAY[$file_to_open]}
`grep '^Exec' /usr/share/applications/${ARRAY[$file_to_open]} | tail -1 | sed 's/^Exec=//' | sed 's/%.//'` & 

Thank You !

deck

By Kalpesh Krishna