a quick introduction to linux

34
A Quick Introduction A Quick Introduction to Linux to Linux (A compilation of some useful Linux (A compilation of some useful Linux commands) commands) Tusharadri Sarkar June 28, 2010 IBM June 28, 2010 1 Tusharadri Sarkar

Upload: tusharadri-sarkar

Post on 15-Jun-2015

511 views

Category:

Self Improvement


1 download

DESCRIPTION

A beginners guide to get familiar with the Linux and learn the most commonly and frequently used commands. Most of the commands are expected to run across all well known Linux distributions. Plus, there are a few commands for the advanced users as well.

TRANSCRIPT

Page 1: A Quick Introduction to Linux

A Quick Introduction to A Quick Introduction to LinuxLinux(A compilation of some useful Linux commands)(A compilation of some useful Linux commands)

Tusharadri Sarkar

June 28, 2010

IBM

June 28, 2010 1Tusharadri Sarkar

Page 2: A Quick Introduction to Linux

A Brief History of UNIXA Brief History of UNIX

Bell Labs : 1970

The C programming language

Support for simultaneous users

Minimum effort for platform compatibility

Simple, elegant, and easy* to use

* Of course, we are talking about the CLI folks !!

June 28, 2010 2Tusharadri Sarkar

Page 3: A Quick Introduction to Linux

A Brief History of LinuxA Brief History of Linux

The GNU project - Richard Stallman 1984

MINIX - Professor Andrew TanenbaumLinus Torvalds - inspired by MINIX

April 1991

Linux 0.01 releasedSeptember 1991

June 28, 2010 3Tusharadri Sarkar

Page 4: A Quick Introduction to Linux

Linux Directory StructureLinux Directory Structure

June 28, 2010 4Tusharadri Sarkar

Page 5: A Quick Introduction to Linux

Main DirectoriesMain Directories

/ The parent (root) directory

bin Compiled executable files (ls, grep)

boot Linux Kernel images and configs

etc System configuration files

dev Device files for all system H/Ws

home Stores home directories of users

lib System library files

June 28, 2010 5Tusharadri Sarkar

Page 6: A Quick Introduction to Linux

Main DirectoriesMain Directories

usr Stores files and executables that all users can access

var Stores files whose size might vary

tmp Stores temporary files

sbinStores executables for system administrator, user can also access

procProvides Kernel windows to view system status (memory, IO etc.)

June 28, 2010 6Tusharadri Sarkar

Page 7: A Quick Introduction to Linux

Users and GroupsUsers and Groups

Each “user” is a member of at least one “group” in Linux

Each user has a home directoryWhat are there in /home and /usr/users?UNIX permissions: both users and groups

> ls –l : see all permissions

June 28, 2010 7Tusharadri Sarkar

Note: From now on, the > at the beginning of each command represents the command prompt. The command starts after that

Page 8: A Quick Introduction to Linux

Notes on File PermissionNotes on File Permission

In UNIX/Linux system everything is a fileFile has 3 permissions : read (r), write (w) and execute (x)

File has 3 sets of permissions: user – group – others

For each, permissions have weights: r = 4 w = 2 x = 1

So, what does permissions 755 means ?

June 28, 2010 8Tusharadri Sarkar

Page 9: A Quick Introduction to Linux

Alias: To make life easy...Alias: To make life easy...

Pipes |: to chain multiple commandsAlias: Write commands once and execute them whenever you need

An example:> alias jtest=`find <JAVA_TEST_PATH> -name *.java |

grep 'Test' | sed 's/Test/TestCase/g'`

What does it do?The .bashrc file: Your own set of aliases

June 28, 2010 9Tusharadri Sarkar

Page 10: A Quick Introduction to Linux

Directory NavigationDirectory Navigation

June 28, 2010 10Tusharadri Sarkar

pwd Present Working Directory

cd Login (home) dir. of the user

cd – User’s last (previous) dir.

cd ~/<path> Here, ~ stands for ‘home’

cd /<path> Using absolute path

cd .. One dir. level up

'.' (dot) and '..' (dot dot) indicate current and parent directories, just like Windows

Page 11: A Quick Introduction to Linux

Creating & Removing DirectoriesCreating & Removing Directories

Check your permissions carefully:write permission – to create and remove

directories

Check your permissions again:not only user but group permissions

also matters

> mkdir <dname> : Create directory> rmdir <dname> : Remove directory

June 28, 2010 11Tusharadri Sarkar

Page 12: A Quick Introduction to Linux

File Searching (ls)File Searching (ls)

ls: Stands for “List”> ls –a : Expose the hidden files> ls –lrt : Very useful when searching by last modification dates

Use regular expressions: ls –lrt lib*.soCreate alias of your own listing commands to make things easy for you again:

Example: > alias l=`ls –l --color=auto`

June 28, 2010 12Tusharadri Sarkar

Page 13: A Quick Introduction to Linux

File Searching (find)File Searching (find)

find: A very powerful command for file searching operations

> find <path> -name *.txtTypical usage with regular expressions

> find <path> -type dwhen you are after the directories!!

> find <path> -type fwhen you are after the regular files!!

June 28, 2010 13Tusharadri Sarkar

Page 14: A Quick Introduction to Linux

File Searching (find)File Searching (find)

find is more powerful when chained with other commands like xgrep or grep

For example:

> find <path> -name "*.*" -exec grep "<pattern>" '{}' \; -print 2>/dev/null

What does the above command do?

June 28, 2010 14Tusharadri Sarkar

Page 15: A Quick Introduction to Linux

File Searching (grep)File Searching (grep)

grep: When you are interested in the contents of the files

Typical usage for search operations:

> grep '<pattern>' <files>When the pattern position also matters:

> grep –a N '<pattern>' <file>

> grep –b N '<pattern>' <file>

> grep –C N '<pattern>' <file>

June 28, 2010 15Tusharadri Sarkar

Page 16: A Quick Introduction to Linux

File Searching (grep)File Searching (grep)

When you are looking for the line numbers: > grep –c '<pattern>' <file>

Remember: C and c are different options for 'grep'

The --color option: Highlight <pattern>Enhance power of 'grep' : 'find' and 'sed'Extremely useful if you are interested in Shell scripting

June 28, 2010 16Tusharadri Sarkar

Page 17: A Quick Introduction to Linux

File Operations (Copy Move Delete)File Operations (Copy Move Delete)

cp – mv – rm: Straight forward and simpleThey have similar syntax and options:

> cp <source> <destination>

> mv <source> <destination>

> rm <filename>-i & -r : Do it 'interactively' or 'recursively'Be careful while using rm and move. Use > alias rm=`rm –i`; alias mv=`mv –i`

June 28, 2010 17Tusharadri Sarkar

Page 18: A Quick Introduction to Linux

Archives and CompressionsArchives and Compressions

Need to create archive and compress files? It’s easy with tar and gzip:

> tar –cf demo.tar <file1> <file2> …> tar –xvf demo.tar - like verbose?

Zip and Unzip a file:gzip & gunzip :: bzip2 & bunzip2

Combine archiving and zipping together:> tar –czvf myfiles.tar.gz *.sh> tar –xzvf myfiles.tar.gz

June 28, 2010 18Tusharadri Sarkar

Page 19: A Quick Introduction to Linux

Text Manipulation (sed)Text Manipulation (sed)

sed: Stands for “Stream Editor”

Powerful but also complexWhen it comes to text manipulation:

Combine powers of 'sed' and 'grep'General format of 'sed' for substitution:> sed 's/<pat1>/<pat2>/<switch>' file1 > file2

How does the <switch> influence the output?

June 28, 2010 19Tusharadri Sarkar

Page 20: A Quick Introduction to Linux

Text Manipulation (sed)Text Manipulation (sed)

9 typical ‘sed’ usage examples

Pattern substitutions:

1. > sed 's/string1/string2/g' – very basic

2. > sed 's/\(.*\)1/\12/g' – a little tricky !

3. > sed '/ *#/d; /^ *$/d' – trickier !!4. > sed ':a; /\\$/N; s/\\\n//; ta' – too cryptic !!!

What are all those '\' required for?

June 28, 2010 20Tusharadri Sarkar

Page 21: A Quick Introduction to Linux

Text Manipulation (sed)Text Manipulation (sed)

5. > sed 's/[ \t]*$//'

If you don't like spaces...6. > sed 's/\([`"$\]\)/\\\1/g'

Use escape judiciously with meta-characters 7. > sed 10 | sed 's/^/ /; s/ *\(.\{7,\}\)/\1/'

Handy for number alignments8. > sed –n '1000{p;q}'9. > sed –n '10,20p;20q'

Printing with sed

June 28, 2010 21Tusharadri Sarkar

Page 22: A Quick Introduction to Linux

Text Manipulation (tr)Text Manipulation (tr)

tr: Stands for “Translate”> echo 'Test' | tr '[:lower:]' '[:upper:]'

Case conversion> tr –dc '[:print:]' < /dev/urandom

Handle non printable characters> tr –s '[:blank:]' '\t' < /proc/diskstats | cut –f4

Combine with 'cut' to manipulate fields

June 28, 2010 22Tusharadri Sarkar

Page 23: A Quick Introduction to Linux

Set Operations (sort)Set Operations (sort)

sort: is helpful with text filesuniq helps refining you sorting

> sort file1 file2 | uniq

> sort file1 file2 | uniq –d

> sort file1 file1 file2 | uniq –u

> sort file1 file2 | uniq –uWhat are the differences in output?

June 28, 2010 23Tusharadri Sarkar

Page 24: A Quick Introduction to Linux

Set Operation (join)Set Operation (join)

join: works best on previously sorted files:

> join –t'\0' –a1 –a2 file1 file2

> join –t'\0' file1 file2

> join –t'\0' –v2 file1 file2

> join –t'\0' –v1 –v2 file1 file2On unsorted files join works just like concatenating files

June 28, 2010 24Tusharadri Sarkar

Page 25: A Quick Introduction to Linux

Basic NetworkingBasic Networking

ifconfig: An equivalent of 'ipconfig' on DOS, but it does much more

> ifconfig

> ifconfig –< interface>

iwconfig: 'w' for wireless networkhostname: When you want to know about the host/ the system you are on

> hostname

> hostname –i June 28, 2010 25Tusharadri Sarkar

Page 26: A Quick Introduction to Linux

Basic NetworkingBasic Networking

netstat: Working with kernel routing table> netstat –r> netstat –i> netstat –l> netstat –tup

route: Modifying the kernel routing table> route add [route addr] [interface]> route delete [route addr] [interface]

June 28, 2010 26Tusharadri Sarkar

Page 27: A Quick Introduction to Linux

Basic System InformationBasic System Information

> uname –aWhich OS, Kernel version, hardware and

system architecture am I running on? For specific information use the following:

-s, -o, -r, -v, -m, or -n

Know who all are working with you:> w> who> whoami

> who am i (Yes, the output is different!!) June 28, 2010 27Tusharadri Sarkar

Page 28: A Quick Introduction to Linux

Basic System InformationBasic System Information> head –n1 /etc/issue

What distribution version and release am I using?

> cat /proc/partitions

What about my partitions?> grep 'MemTotal' /proc/meminfo

Am I running out of RAM?> mount | column –t

What file systems is currently in use?June 28, 2010 28Tusharadri Sarkar

Page 29: A Quick Introduction to Linux

Disk Space UsageDisk Space Usage

> ls –lSr

'ls' command coming to rescue again!!du – Stands for “Disc Usage”

> du –s * | sort –k1,1rn | head

> du –hk .

> du –h <file>

df – Stands for “Disc Free”> df –h

> df –iJune 28, 2010 29Tusharadri Sarkar

Page 30: A Quick Introduction to Linux

Basic Monitoring & DebuggingBasic Monitoring & Debugging

If you want to monitor a file continuously:> tail –f <filename> You will need Ctrl+C too!!

When you want to deal with processes:> ps –ef <processname>

> ps –p pid1, pid2,... Be sure of PID & PPID !!

> ps –e –o pid, args --forest Hierarchical list

Some miscellaneous commands:> free –m> last reboot

June 28, 2010 30Tusharadri Sarkar

Page 31: A Quick Introduction to Linux

Basic Monitoring & DebuggingBasic Monitoring & Debugging

3 Advance commands (for sysadmin)

Every CPU cycle is costly...> ps –e –o pcpu, cpu, nice, state, cputime,

args --sort pcpu | sed '/^ 0.0 /d'

And every byte of memory too...> ps –e –orss=, args= | sort –b –k1, 1n | pr –

TW$COLUMNS

Locked threads can cause troubles...> ps –C <process> -L –o pid, tid, pcpu, state

June 28, 2010 31Tusharadri Sarkar

Page 32: A Quick Introduction to Linux

MiscellaneousMiscellaneous

Get familiar with the time keepers:

cal: “Calendar”, no one can live without it...

> cal (current month’s calendar)

> cal <mm> <yyyy> (for specific month)date: Of course you want to know this...

> date (Date with IST)

> date “%d/%m%y %H:%M:%S”

(A more familiar date format)June 28, 2010 32Tusharadri Sarkar

Page 33: A Quick Introduction to Linux

MiscellaneousMiscellaneous

Where are your commands resting?

> which <command>

> whereis <command>Read a file without opening it:

> cat <filename>Finally, the grand “Manual” of Linux:

> man <item>

The <item> list is really large!! Try it...June 28, 2010 33Tusharadri Sarkar

Page 34: A Quick Introduction to Linux

Thank You !!Thank You !!

34