Sie sind auf Seite 1von 34

A Quick Introduction to Linux

(A compilation of some useful Linux commands)

Tusharadri Sarkar June 28, 2010


IBM

June 28, 2010

Tusharadri Sarkar

A 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

Tusharadri Sarkar

A Brief History of Linux


The GNU project - Richard Stallman
1984

MINIX - Professor Andrew Tanenbaum Linus Torvalds - inspired by MINIX


April 1991

Linux 0.01 released


September 1991

June 28, 2010

Tusharadri Sarkar

Linux Directory Structure

June 28, 2010

Tusharadri Sarkar

Main Directories
/ bin boot etc dev home lib The parent (root) directory Compiled executable files (ls, grep) Linux Kernel images and configs System configuration files Device files for all system H/Ws Stores home directories of users System library files
June 28, 2010 Tusharadri Sarkar 5

Main Directories
Stores files and executables that all usr users can access var Stores files whose size might vary tmp Stores temporary files Stores executables for system sbin administrator, user can also access Provides Kernel windows to view proc system status (memory, IO etc.)
June 28, 2010 Tusharadri Sarkar 6

Users and Groups


Each user is a member of at least one group in Linux Each user has a home directory What are there in /home and /usr/users? UNIX permissions: both users and groups > ls l : see all permissions
Note: From now on, the > at the beginning of each command represents the command prompt. The command starts after that
June 28, 2010 Tusharadri Sarkar 7

Notes on File Permission


In UNIX/Linux system everything is a file File 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 Tusharadri Sarkar 8

Alias: To make life easy...


Pipes |: to chain multiple commands Alias: 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 Tusharadri Sarkar 9

Directory Navigation
pwd Present Working Directory cd Login (home) dir. of the user cd Users 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
June 28, 2010 Tusharadri Sarkar 10

Creating & 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 Tusharadri Sarkar 11

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*.so Create alias of your own listing commands to make things easy for you again: Example: > alias l=`ls l --color=auto`
June 28, 2010 Tusharadri Sarkar 12

File Searching (find)


find: A very powerful command for file searching operations > find <path> -name *.txt Typical usage with regular expressions > find <path> -type d when you are after the directories!! > find <path> -type f when you are after the regular files!!
June 28, 2010 Tusharadri Sarkar 13

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

Tusharadri Sarkar

14

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 Tusharadri Sarkar 15

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 Tusharadri Sarkar 16

File Operations (Copy Move Delete)


cp mv rm: Straight forward and simple They 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 Tusharadri Sarkar 17

Archives and Compressions


Need to create archive and compress files? Its 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 Tusharadri Sarkar 18

Text Manipulation (sed)


sed: Stands for Stream Editor Powerful but also complex When 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 Tusharadri Sarkar 19

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 Tusharadri Sarkar 20

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 alignments 8. > sed n '1000{p;q}' 9. > sed n '10,20p;20q' Printing with sed
June 28, 2010 Tusharadri Sarkar 21

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 Tusharadri Sarkar 22

Set Operations (sort)


sort: is helpful with text files uniq helps refining you sorting > sort file1 file2 | uniq > sort file1 file2 | uniq d > sort file1 file1 file2 | uniq u > sort file1 file2 | uniq u What are the differences in output?
June 28, 2010 Tusharadri Sarkar 23

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 file2 On unsorted files join works just like concatenating files
June 28, 2010 Tusharadri Sarkar 24

Basic Networking
ifconfig: An equivalent of 'ipconfig' on DOS, but it does much more
> ifconfig > ifconfig < interface>

iwconfig: 'w' for wireless network hostname: When you want to know about the host/ the system you are on
> hostname > hostname i
June 28, 2010 Tusharadri Sarkar 25

Basic 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 Tusharadri Sarkar 26

Basic System Information


> uname a
Which 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 Tusharadri Sarkar 27

Basic 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 Tusharadri Sarkar 28

Disk 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 i
June 28, 2010 Tusharadri Sarkar 29

Basic 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 Tusharadri Sarkar 30

Basic 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 Tusharadri Sarkar 31

Miscellaneous
Get familiar with the time keepers: cal: Calendar, no one can live without it... > cal (current months 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 Tusharadri Sarkar 32

Miscellaneous
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 Tusharadri Sarkar 33

Thank You !!

34

Das könnte Ihnen auch gefallen