linux : the history a.k.a. hang on to your seats or prepare to be bored silly sumit punglia iit...

21
LINUX : The history A.K.A. Hang on to your seats Or Prepare to be bored sill Sumit Punglia IIT Bombay LINUX : User Basics

Upload: barnaby-white

Post on 30-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

LINUX : The history

A.K.A.

Hang on to your seats

Or

Prepare to be bored silly

Sumit Punglia IIT Bombay

LINUX : User Basics

Lets get bit bored :Linux Arises

Linux is a variant of a tried and true operating system called “UNIX”.

Unix was created in the 1960’s at Bell Labs, responding to the need for a stable, multi-user operating system.

Linux arose from an attempt to port Unix to the x86 architecture (the architecture of most computers running Windows).

The idea was to put a stable operating system on cheap hardware. Before Linux, Unix based systems were damn expensive.

Linux arises

In terms of stability, Linux is king; it is not uncommon for systems running Linux to stay up for months without needing to be rebooting. So NEVER REBOOT A SYSTEM in lab.

Linux is free. It is distributed under an “open source” license. That means that the people who code it generally are not paid for their coding efforts. However, companies like Red Hat get paid to provide support for the OS.

The root user

In all of this, keep in mind there is a “root” user who controls the entire machine. Root can read/write/execute any file and can restart the machine, install software, etc.

Root is not to be trifled with, for root is quick to anger and does not suffer fools gladly.

Seriously, usually root is one or a small group of people who really know what they are doing and make sure the machine runs properly.

Okay…Lets Start

Use your user-id and password to login to the system.

Open a terminal because 90% things can’t be done using GUI.

passwd command can be used to change your password.

startx command is used to instantiate GUI (graphical) mode. startx -- :1 can be used to start another GUI

NEVER LOCK SCREEN WITHOUT KEEPING A NOTE ON THE TABLE.

Ctrl +Alt + Fn takes you the various tty available in general Ctrl+Alt+F7 is the X-system.

Setting

Under /home are the home directories, so /home/pungi, for example is a specific directory for a particular user named pungi.

Software is usually stored in /bin, /sbin, and /usr/local/bin as well as /usr/bin. This directory should be included to your path. Echo $PATH gives you info about your path. You can add extra directories to your path by

export $PATH=$PATH:/home/pungi/bin/:./

.bash_profile = add above path command to this file so that it gets auto-executed every time you login.

.bashrc = your default file to keep aliases and stuff alias p=“pine –i”

Don’t keep echo commands in this file. Instead use .bash_profile for those since it creates problem with using scp.

LS , CD and PWD

cd abc/def/ghi takes you to directory “ghi” cd ~/ brings you to your home directory from anywhere within the file

structure. cd - : takes you back to directroy “ghi” pwd : prints your Present Working Directory ls –alSh : Sort files by size in human readable format.

First character is ‘d’ or ‘-’ or ‘l’ d- directory , - normal file , l = link or shortcut

Links or shortcut : ln –s /home/pungi/dcAnalyzer/bin/dcAnalyzerV2.0 dc

Chmod command : chmod –R 711 results/

More commands

More command : Because lists can be long, it gives you a pagebreak.ls –al | more cat filename | more more filename

Less Command : Never use more. Instead use less. In this you can browse the file with up/down keys.

File manipulations The command cp lets you copy a file The mv command lets you move a file from one place to another or rename the

file Rename : Another helpful command for mass renaming. rename .cpp .c *.cpp The rm command can be used (with care!) to remove a file. rm –rf can be used to delete directories. Never use rm –rf Its dangerous. Make a directory called Trash in your home directory. Use mv filename Trash. Instead of using rm. Don’t expect to get your data recovered after you delete it. Back it up for yourself.

#! /bin/bashif [ $# -eq 1 ] ; then mv $1 $HOME/Trashfiif [ $# -eq 2 ]; then mv $2 $HOME/Trashfi

File editing : VIM

If you need to edit or create a text file, there are many ways to do it. My recommendation is to use a program called vim.

Invoke it by typing vim (name of file)Ctrl + P : Can finish keywords. Useful

100== : Will indent 100 lines for you

100dd : Cuts next hundred lines

100yy : Copies 100 lines

p : pastes the lines cut/copied

u : undo operation

ctrl + R : redo operation

w : save

w filename : “save as” filename

q : Quit

sp filename : open files in split mode. Both files can be compared for data.

vsplit filename : Gives vertical split of screen

/keywork : Search for keyword

%s/keyword/newkeyword : Replaces keyword by newkeyword

CTags

Do the following configuration

1) vi ~/.vimrc

2) set tags=$HOME/mycode/tags

3) vi ~/.bashrc

4) alias ctags=‘ctags –recurse –language-force=c++ -extra=+q –fields=+i’

5) cd $HOME/mycode/

6) ctags */*.cpp */*.h

Open your C source code with using vim and by pressing :

Ctrl + ] : ( Control and Square bracket) takes you to definition of the function/class/variable initialization etc..

Ctrl + T : Come back

g + ] : list in which classes the function/variable is defined

Your good friend Man

When you get stuck, try typing man with the name of the command. Man is short for manual. It is the list of help for a given command. Don’t use it.

Instead use “info” which gives more upto date information.

The command apropos will get you a listing of man pages and files that might be appropriate for a specific command.

For example, “apropos mp3 player” will show you “xmms” “aplay” “arecord” as some appropriate command.

SSH & SCP

The command ssh opens a secure, encrypted line of communication into a specific machine. Usage : ssh –l sumitp 10.107.1.2

Ssh –Xl sumitp 10.107.1.2 : Reduces the pain to do export display from machines and doing xhost+ here. Always use ssh -X

The command scp allows for files to be copied from one machine. scp –r directory [email protected]: transfers directory to sharada.

scp -r [email protected]:./directory ./

Copies the directory from sharada to ./ ./directory path is given with respect to your home directory on sharada.

Scripting

Specific commands or sets of commands can be “linked” together to make your life easier.

This isn’t really “programming”; so it’s usually called scripting.

This is helpful for many tasks in your work, where you need to manipulate large numbers of files quickly or perform a specific, repetitive action.

Scripting may be really useful when you want to automate your simulation tasks. Especially when things are not with a GUI.

Text manipulation

The grep command allows you to search for specific patterns within a file.

Usage: grep “#include” *.cpp

Especially sening piped data. This would find and print to the screen all lines with a #include word in them. Grep is one of the most useful commands in unix.

Grep has many more kind of usage. ls -al | grep ^d : Just list all directories

Text manipulation

cut The cut command can be used to work with formatted lists or columns. It can

pick specific columns from a file with a delimiter. For example

– cut -f 2,3,7 -d : <filename> This would grab the second, third and seventh columns from a file where : was

used as a delimiter. It can also be used to grab specific characters from a file.

Perl Perl can be used to edit a file “in place”, meaning it does not copy the file or

require it to be opened in an editor. The basic format of the command is:

– Perl -i -p -e ‘s/<old text>/<new text>/ig’ <filename> This replaces <old text> with <new text> Special formatting characters can be replaced: for example \n is a newline, \r is

a return, \t is a tab. Spaces are treated as real spaces.

G++/ GCC

The Gnu C++ . Its free. Its open source. If your programs are not working. You can changes the source-code of compiler and recompile the compiler.

The G++ options-Wall : Display all kinds of warnings. -W : add this will –Wall to get even more warning. -On : n =1,2,3 Optimized your compiled code. 3 is best but 2 is suggested to used. G++ -Wall –W –Wno-deprecated –O2 filename.cpp –O executable

Linking : If you have code distributed over several files. It useful to compile each one individually and link them together.

G++ -Wall –W –O2 –c file1.cpp –o file1.o G++ -Wall –W –O2 file1.o file2.o file3.o –lm –o executable

-lmcheck : can be used in addition to lm. It gives a meaning to your segmentation faults.

More Commands

Nohup : Used for running processess in background. The output generated by process is appended to file nohup.out

tar –zcvf : Creates a .tar.gz file Usage : tar –zcvf new.tar.gz “list of files or directory”

tar –jcvf : Creates a .tar.bz2 extension tar –jxvf or tar –zcvf can be used to

untar .tar.bz2 files or .tar.gz files

tar –xvf abc.tar : It will untar the files zip –r directory directory : will create directory.zip

switchdesk : Switching between Gnome and Kde when GUI is not available.

Makefile

CPP = g++CFLAGS = -Wall -W -O2 -Wno-deprecated

OBJECTS = \Main.o \Func.o \

all: $(OBJECTS) compile

Main.o : Main.cpp $(CPP) $(CFLAGS) -c Main.cppFunc.o : Func.cpp $(CPP) $(CFLAGS) -c Func.cppclean: rm –f $(OBJECTS) ../dcanaCG

compile: $(CPP) $(CFLAGS) *.o -lm -o ../ab.exe

“make” on command line run the “make all” tag. make clean : run the clean: tag make compile : will run the compile tag

Some utilities

Gaim : mkdir bin && ln –s /home/users/sumit/bin/gaim bin/gaim

After this command execute gaim by writing “gaim &” on your login.

gvim : Graphical interface to vim.

emacs : A better but slightly difficult to use text editor. gv : PS file viewer xpdf : pdf file viewer gnuplot : Plotting utility. Useful for viewing your simulation results. Latex : The best typesetting utility for writing reports.

Some more command Line Utility

factor 100 : Gives factors prime factors N

/sbin/ifconfig : Gives you IP address of machine

bc : Command line calculator

fg : When Job is stopped by Ctrl+Z . Fg return to it

sort : Sort file name

sort filename > newfilename

ps –e : Shows all processes running

top : Shows Process memory and cpu usage.

df –h : Shown disk space free on system.

du –sh “list of files or directory”: Gives size of the files