0
A Quick Introduction toA Quick Introduction to
LinuxLinux
(A compilation of some useful Linux commands)(A compilation of s...
A Brief History of UNIXA Brief History of UNIX
Bell Labs : 1970
The C programming language
Support for simultaneous users
...
A Brief History of LinuxA Brief History of Linux
The GNU project - Richard Stallman
1984
MINIX - Professor Andrew Tanenbau...
Linux Directory StructureLinux Directory Structure
June 28, 2010 4Tusharadri Sarkar
Main DirectoriesMain Directories
/ The parent (root) directory
bin Compiled executable files (ls, grep)
boot Linux Kernel ...
Main DirectoriesMain Directories
usr
Stores files and executables that all
users can access
var Stores files whose size mi...
Users and GroupsUsers and Groups
Each “user” is a member of at least one
“group” in Linux
Each user has a home directory
W...
Notes on File PermissionNotes on File Permission
In UNIX/Linux system everything is a file
File has 3 permissions :
read (...
Alias: To make life easy...Alias: To make life easy...
Pipes |: to chain multiple commands
Alias: Write commands once and
...
Directory NavigationDirectory Navigation
June 28, 2010 10Tusharadri Sarkar
pwd Present Working Directory
cd Login (home) d...
Creating & Removing DirectoriesCreating & Removing Directories
Check your permissions carefully:
write permission – to cre...
File Searching (ls)File Searching (ls)
ls: Stands for “List”
> ls –a : Expose the hidden files
> ls –lrt : Very useful whe...
File Searching (find)File Searching (find)
find: A very powerful command for file
searching operations
> find <path> -name...
File Searching (find)File Searching (find)
find is more powerful when chained with
other commands like xgrep or grep
For e...
File Searching (grep)File Searching (grep)
grep: When you are interested in the
contents of the files
Typical usage for se...
File Searching (grep)File Searching (grep)
When you are looking for the line
numbers: > grep –c '<pattern>' <file>
Remembe...
File Operations (Copy Move Delete)File Operations (Copy Move Delete)
cp – mv – rm: Straight forward and simple
They have s...
Archives and CompressionsArchives and Compressions
Need to create archive and compress
files? It’s easy with tar and gzip:...
Text Manipulation (sed)Text Manipulation (sed)
sed: Stands for “Stream Editor”
Powerful but also complex
When it comes to ...
Text Manipulation (sed)Text Manipulation (sed)
9 typical ‘sed’ usage examples
Pattern substitutions:
1. > sed 's/string1/s...
Text Manipulation (sed)Text Manipulation (sed)
5. > sed 's/[ t]*$//'
If you don't like spaces...
6. > sed 's/([`"$])/1/g'
...
Text Manipulation (tr)Text Manipulation (tr)
tr: Stands for “Translate”
> echo 'Test' | tr '[:lower:]' '[:upper:]'
Case co...
Set Operations (sort)Set Operations (sort)
sort: is helpful with text files
uniq helps refining you sorting
> sort file1 f...
Set Operation (join)Set Operation (join)
join: works best on previously sorted
files:
> join –t'0' –a1 –a2 file1 file2
> j...
Basic NetworkingBasic Networking
ifconfig: An equivalent of 'ipconfig' on
DOS, but it does much more
> ifconfig
> ifconfig...
Basic NetworkingBasic Networking
netstat: Working with kernel routing table
> netstat –r
> netstat –i
> netstat –l
> netst...
Basic System InformationBasic System Information
> uname –a
Which OS, Kernel version, hardware and
system architecture am ...
Basic System InformationBasic System Information
> head –n1 /etc/issue
What distribution version and release
am I using?
>...
Disk Space UsageDisk Space Usage
> ls –lSr
'ls' command coming to rescue again!!
du – Stands for “Disc Usage”
> du –s * | ...
Basic Monitoring & DebuggingBasic Monitoring & Debugging
If you want to monitor a file continuously:
> tail –f <filename> ...
Basic Monitoring & DebuggingBasic Monitoring & Debugging
3 Advance commands (for sysadmin)
Every CPU cycle is costly...
> ...
MiscellaneousMiscellaneous
Get familiar with the time keepers:
cal: “Calendar”, no one can live without it...
> cal (curre...
MiscellaneousMiscellaneous
Where are your commands resting?
> which <command>
> whereis <command>
Read a file without open...
Thank You !!Thank You !!
34
Upcoming SlideShare
Loading in...5
×

A Quick Introduction to Linux

549

Published on

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.

Published in: Self Improvement, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
549
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Created at Bell Labs in 1970 Written in the C programming language which was developed at the same time Supports large numbers of simultaneous users Runs with few alterations on many hardware platforms Simple, elegant, and easy to use (at least compared to its predecessors)
  • 1984 - Richard Stallman started the GNU project to create a free operating system Professor Andrew Tanenbaum wrote the operating system MINIX from scratch to teach his students the inner workings of a real operating system April 1991 - Linus Torvalds starts working on a new operating system inspired by MINIX September 1991 - Linux version 0.01 released.
  • Each user has a home directory, which is where personal files and preferences are stored. These are typically in /home or in /usr/users Use the &apos;ls -l&apos; or long listing command to see all the permissions assigned to a file. It is similar to the &apos;dir&apos; command in DOS.
  • Each permission is assigned a different weight r=4, w=2 &amp; x=1 If a file is assigned permissions 755 it means the current user has all permissions while group and others have only read and execute permissions
  • For example: alias jtest=`find . -name *.java | grep &apos;Test&apos; | sed &apos;s/Test//&apos;` The entire command will execute when you run &apos;jtest’ To create permanent aliases add them in the .bashrc file in you home or login directory.
  • pwd: shows the current directory path cd: go to the home directory of the user cd - : go to the previous directory cd ~/&lt;path&gt; : go to any directory &lt;path&gt; under the home directory cd /&lt;path&gt; : go to any directory &lt;path&gt; under root (/). This is the absolute path name cd ../ : go one directory up
  • mkdir &lt;dname&gt; : Creates directory &lt;dname&gt; rmdir &lt;dname&gt; : Removes directory &lt;dname&gt; Remember: In Unix everything is &apos;file&apos;
  • ls : Lists directory contents (The command that you will probably use the most) ls -a : Lists directory contents including hidden files ls -lrt : Lists directory contents by date of modification in reverse order Regular expressions blends efficiently with ls: For example: ls -lrt lib*.so --color Option lists the filenames with different color according to filetypes
  • find &lt;path&gt; -name *.txt : lists all the .txt files under the directory &lt;path&gt; find &lt;path&gt; -type d : lists all the sub-directories under &lt;path&gt; find &lt;path&gt; -type f: lists all the regular files under &lt;path&gt; You can get the same result of the above commands by using &apos;ls&apos; with some tricks, but &apos;find&apos; is far more powerful and easy to use
  • find &lt;path&gt; -name &quot;*.*&quot; -exec grep &quot;&lt;pattern&gt;&quot; &apos;{}&apos; \; -print 2&gt;/dev/null The above example will look for every instance of &lt;pattern&gt; even inside file contents under &lt;path&gt; recursively
  • grep &apos;&lt;pattern&gt;&apos; *.* : Searches for &lt;pattern&gt; inside all regulare files in current directory grep -a/-b N &apos;&lt;pattern&gt;&apos; *.* : Same as above but prints N lines including the matching lines either after or before it grep -C N &apos;&lt;pattern&gt;&apos; *.* : Same as above but prints N lines including the matching lines both before and after it
  • grep -c &apos;&lt;pattern&gt;‘ *.* Searches for &lt;pattern&gt; inside all the regular files in the current directory and prints the file names and the line numbers at which &lt;pattern&gt; is present
  • cp &lt;source&gt; &lt;destination&gt; : To copy a file &lt;source&gt; to &lt;destination&gt; move &lt;source&gt; &lt;destination&gt; : To move a file &lt;source&gt; to &lt;destination&gt;. This also doubles as the command to rename the file rm &lt;fname&gt; : To remove / delete file &lt;fname&gt;
  • tar -cf demo.tar &lt;files&gt;: Create an archive demo.tar from &lt;files&gt; tar -xvf demo.tar : Extracts the files from archive demo.tar (-v gives verbose output) gzip &amp; gunzip : To Zip and Unzip a file bzip2 &amp; bunzip2 : Same as above with a different compression algorithm tar -czvf tushar.tar.gz *.sh and tar -xzvf tushar.tar.gz (-z refers to zipping)
  • General format of &apos;sed&apos; for substitution operations: sed &apos;s/&lt;pat1&gt;/&lt;pat2&gt;/&lt;switch&gt;&apos; file1 &gt; file2 It means, in file file1 replace occurrences of &lt;pat1&gt; with &lt;pat2&gt; depending on &lt;switch&gt; and put the outcome in file2 &lt;switch&gt; = ‘g’ or ‘c’ which stands for ‘global’ and ‘conditional’ substitution
  • sed &apos;s/string1/string2/g&apos; : Replace all occurrence of string1 with string2 sed &apos;s/\(.*\)1/\12/g&apos; : Modify anystring1 to anystring2. &apos;\&apos; is used to escape special chars sed &apos;/ *#/d; /^ *$/d&apos; : Remove comments and blank lines. &apos;;&apos; joins two sed operations sed &apos;:a; /\\$/N; s/\\\n//; ta&apos; : Concatenate lines with trailing \
  • sed &apos;s/[ \t]*$//&apos; : Remove trailing spaces from lines sed &apos;s/\([`&quot;$\]\)/\\\1/g&apos; : Escape shell meta characters active within double quotes seq 10 | sed &apos;s/^/ /; s/ *\(.\{7,\}\)/\1/&apos; : Right align numbers (Not applicable for all patterns) sed -n &apos;1000{p;q}&apos; : Print 1000th line sed -n &apos;10,20p;20q&apos; : Print lines 10 to 20
  • echo &apos;Test&apos; | tr &apos;[:lower:]&apos; &apos;[:upper:]&apos; : Convert uppercase letters to lower case letters tr -dc &apos;[:print:]&apos; &lt; /dev/urandom : Filter non printable characters tr -s &apos;[:blank:]&apos; &apos;\t&apos; &lt; /proc/diskstats | cut -f4 : cut fields separated by blanks
  • sort file1 file2 | uniq : Shows union of unsorted files sort file1 file2 | uniq -d : Shows intersection of unsorted files sort file1 file1 file2 | uniq -u : Shows difference of unsorted files sort file1 file2 | uniq -u : Shows symmetric Difference of unsorted files
  • join -t&apos;\0&apos; -a1 -a2 file1 file2 : Shows union of sorted files join -t&apos;\0&apos; file1 file2 : Shows intersection of sorted files join -t&apos;\0&apos; -v2 file1 file2 : Shows the difference of sorted files join -t&apos;\0&apos; -v1 -v2 file1 file2 : Shows the symmetric Difference of sorted files
  • ifconfig : Lists details of all the available network interfaces of your system ifconfig -&lt;IFACE&gt; : Shows details about interface &lt;IFACE&gt; iwconfig : Same as &apos;ifconfig&apos; but lists only the wireless network interfaces hostname : Shows the system host name hostname -i : Shows host IP address
  • netstat -r : Lists the routing table of the kernel netstat -i : List interface details netstat -l: Lists all the binded listening ports netstat -tup : Lists all active connections route add : To add a new route to the table route delete : To delete a route from the table
  • uname -a : Shows kernel version and the system architecture information To get specific information use following options: -r (release) -n (distribution) -m (Hardware ) -v (version) -s (OS name) -o (OS category) who : Lists all the users currently logged in into the system whoami : Gives the current user name who am i : Lists current user login info and time
  • head -n1 /etc/issue : Shows the name and the version of distribution cat /proc/partitions : Lists all the partitions registered on the system grep &apos;MemTotal&apos; /proc/meminfo : Shows the total RAM registered to the system Mount | column -t : Lists all the mounted file systems on the system (Aligns the output)
  • ls -lSr : Show files by size, biggest last du -s * | sort -k1,1rn | head : Show top disk users in current directory du -h : Shows the file sizes in easy to interpret disk usage (-h stands for Human Readable) df -h : Show free space on mounted filesystems df -i : Show free inodes on mounted filesystems
  • tail -f &lt;fname&gt; : Continuously monitors file &lt;fname&gt; until terminated by Ctrl+C ps -ef : Lists all the processes in the system ps -p id1,id2,... : Lists information for particular process IDs id1,id2,… ps -e -o pid,args --forest : List processes and displays them arranged in a hierarchy free -m : Shows amount of free memory in MB last reboot : Show system reboot history
  • ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed &apos;/^ 0.0 /d&apos; : Lists processes by % cpu usage ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS : Lists processes by mem (KB) usage. See also ps_mem.py ps -C &lt;process&gt; -L -o pid,tid,pcpu,state : Lists all threads for a particular &lt;process&gt;
  • Transcript of "A Quick Introduction to Linux"

    1. 1. A Quick Introduction toA 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
    2. 2. 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
    3. 3. A Brief History of LinuxA 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 3Tusharadri Sarkar
    4. 4. Linux Directory StructureLinux Directory Structure June 28, 2010 4Tusharadri Sarkar
    5. 5. 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
    6. 6. Main DirectoriesMain Directories usr Stores files and executables that all users can access var Stores files whose size might vary tmp Stores temporary files sbin Stores executables for system administrator, user can also access proc Provides Kernel windows to view system status (memory, IO etc.) June 28, 2010 6Tusharadri Sarkar
    7. 7. Users and GroupsUsers 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 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
    8. 8. Notes on File PermissionNotes 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 8Tusharadri Sarkar
    9. 9. Alias: To make life easy...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 9Tusharadri Sarkar
    10. 10. 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
    11. 11. 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
    12. 12. 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*.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 12Tusharadri Sarkar
    13. 13. File Searching (find)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 13Tusharadri Sarkar
    14. 14. 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
    15. 15. 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
    16. 16. 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
    17. 17. File Operations (Copy Move Delete)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 17Tusharadri Sarkar
    18. 18. 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
    19. 19. Text Manipulation (sed)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 19Tusharadri Sarkar
    20. 20. 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
    21. 21. 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 alignments 8. > sed –n '1000{p;q}' 9. > sed –n '10,20p;20q' Printing with sed June 28, 2010 21Tusharadri Sarkar
    22. 22. 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
    23. 23. Set Operations (sort)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 23Tusharadri Sarkar
    24. 24. 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 file2 On unsorted files join works just like concatenating files June 28, 2010 24Tusharadri Sarkar
    25. 25. Basic NetworkingBasic 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 25Tusharadri Sarkar
    26. 26. 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
    27. 27. Basic System InformationBasic 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 27Tusharadri Sarkar
    28. 28. 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
    29. 29. 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 –i June 28, 2010 29Tusharadri Sarkar
    30. 30. 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
    31. 31. 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
    32. 32. 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
    33. 33. 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
    34. 34. Thank You !!Thank You !! 34
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×