+ All Categories
Home > Education > The linux command line for total beginners

The linux command line for total beginners

Date post: 27-May-2015
Category:
Upload: corrie-watt
View: 314 times
Download: 10 times
Share this document with a friend
Description:
A first short look at the linux command line: the power and the glory
Popular Tags:
25
Introduc)on to the Linux Command Line Interface Prepared for the Nova Sco0a Linux Users Group by Corrie Wa= (h=p://www.corriewa=.com) 2014
Transcript
Page 1: The linux command line  for total beginners

Introduc)on*to*the*Linux*Command*Line*Interface

Prepared'for'the'Nova'Sco0a'Linux'Users'Group'by'Corrie'Wa='(h=p://www.corriewa=.com)

2014

Page 2: The linux command line  for total beginners

The$Linux$CLI$(I$am$using$the$Bash$shell)CLI!=!Command!Line!Interface

“Graphical)user)interfaces)make)easy)tasks)easy,)while)command)line)interfaces)make)difficult)

tasks)possible.”

(original)source)unknown,)quoted)in)Sho6s,)W.)E.)(2012).)The)Linux)Command)Line.)

Page 3: The linux command line  for total beginners

What%is%“The%Shell”?

The$shell$is$a$program$in$your$linux$distro$which$takes$keyboard$commands$and$passes$them$to$the$opera8ng$system$to$perform.In$the$good$old$days,$it$was$the$only$user$interface$available.

Now$we$have$GUI's$and$mice.$Good$(intui7ve),$but$not$be;er.

For$instance,$imagine$a$directory$with$200$images$of$large$(but$unknown)$size$that$must$be$put$in$a$gallery$page:$large$size$of$512$x$512,$with$thumbnails$of$256$x$256.

How$would$you$do$this$quickly?...Use%the%CLI!

Page 4: The linux command line  for total beginners

Open%a%terminal%window%and%enter:

!~$ mkdir 512 256~$ for i in *.jpg ; do convert -resize 256x256 "$i" "256/$i" ; done~$ for i in *.jpg ; do convert -resize 512x512 "$i" "512/$i" ; done...Faster(than(using(a(GUI(by(several(orders(of(magnitude!

How$can$you$get$comfortable$doing$this?!Play!with!your!CLI:!make!test!files!and!muck!around!with!them.!Move!them,!copy!them,!put!them!in!another!directory,!delete!them,!list!them,!etc....Learn'more'at'h"p://linuxcommand.org/index.php

Page 5: The linux command line  for total beginners

Ge#ng&Started

1. Start'the'terminal'emulator'(this'program'opens'a'window'to'let'you'interact'with'the'shell).'A'shell'prompt'will'appear:me@linuxbox: ~$

2. Type'in'some'stuff,'press'Enter:me@linuxbox: ~$ WTFbash:WTF: command not foundme@linuxbox: ~$'...A#new#command#prompt#is#returned#to#you

Your%shell%could%not%find%the%command%'WTF'%because%it%does%not%exist.%No%surprise%here.%Let%us%do%something%more%useful.

Page 6: The linux command line  for total beginners

Ge#ng&Around

Use$the$up)arrow$to$show$previous$commands$you$used$(command$history).

Use$the$arrow$keys$to$move$the$cursor$(to$go$forward$or$backward$in$a$line$of$text).$Your$mouse$will$not$work$for$this!

Copy%Paste+with+your+mouse+usually+works+(but+do+not+copy+a+blank+space+at+the+end+of+your+text+string%%the+'Enter'+key+will+be+assumed+to+have+been+pressed).

...Hint:'File'names'in'Linux'are'case3sensi4ve.

Page 7: The linux command line  for total beginners

Safe%Commands%to%Explore

Format'of'a'CLI'command:!command -option filename

Display(current(/me(and(date(or(this(month's(calendar:me@linuxbox: ~$ dateme@linuxbox: ~$ calSee(the(current(amount(of(free(space(on(your(disk(drives:me@linuxbox: ~$ dfSee(amount(of(free(memory:me@linuxbox: ~$ freeEnding(a(terminal(sesssion:me@linuxbox ~$ exit!...Or$type$in$'Control/D'

Page 8: The linux command line  for total beginners

Naviga&ng(the(File(System

Files!in!Linux!are!hierarchical!(i.e.!in!an!upside2down!tree!structure)A!file!may!contain!data!in!some!format!(with!or!without!a!file!extension!label)!or!it!may!contain!other!files!2!making!it!a!directory.Directories)are)files!that!have!files!in!them:!sub2directories!and!data!files!(eg.!Downloads,!home,!Recipes).We!need!to!know!what!directory!we!are!currently!in,!so!that!we!can!change!our!working!directory!to!where!we!want!be:me@linuxbox: ~$ pwd!...Print/my/current/working/directory/home/me!...This/is/your/current/working/directoryme@linuxbox: ~$!...As/usual,/a/new/command/prompt/follows

Page 9: The linux command line  for total beginners

Working(with(Directories

me@linuxbox: ~$ ls!...List&the&files&in&the&current&directory08pdfs work.tgz Desktop project5 recipes!...&files&in&your&home&directory

Change'the'working'directory'using'Absolute'Pathnames:me@linuxbox: ~$ cd /usr/binme@linuxbox:/usr/bin$!...You%are%now%in%directory%'bin'

Change'the'working'directory'using'Rela5ve'Pathnames:me@linuxbox:/usr/bin$ cd ..!...go$UP$one$level$in$the$file$treeme@linuxbox:/usr$

Page 10: The linux command line  for total beginners

Learn&more&about&traversing&the&linux&directory&tree&at:!h"p://linuxcommand.org/index.php

Naviga&on)Shortcutscd!...Change(to(your(own(home(directorycd)2!...Change(to(the(previous(working(directorycd)~jane!...Change(working(directory(to(user(Jane's(home(directorycd)..!...Change(to(directory(one(level(higher(up(in(the(file(tree

me@linuxbox:,~$,ls,0l!...Get%more%informa-on%by%using%the%op-on%5ldrwxr-xr-x 2 jane users 4096 May 2 2011 my_c_scripts-rw-r--r-- 1 jane users 2313 Sep 21 2012 vidlist.html

Page 11: The linux command line  for total beginners

What%does%it%all%mean?

drwxr-xr-x 2 jane users 4096 May 2 2011 c_scriptsdrwxr$xr$x%...d#says#this#is#a#file#is#a#directory#(-#means#a#plain#file)drwxr$xr$x%...rwx#says#you#have#permission#to#read,#write#and#executedrwxr%xr$x%...r:x#says#your#group#has#read#and#execute#permissionsdrwxr$xr%x%...#This#r:x#means#the#'world'#read#and#execute#permissionsdrwxr$xr$x%2%...#number#of#hard#links#to#the#filedrwxr$xr$x%2%jane+users%...Owner,#Group#name4096+May+2+2011%...size#of#file#in#bytes,#date#last#modifiedc_scripts%...name#of#directory#(filename)

Page 12: The linux command line  for total beginners

Less$is$More

Page 13: The linux command line  for total beginners

Using&the&less&program

The$less$program$is$an$improved$replacement$of$the$earlier$Unix$program$called$more.$Many$Linux$files$have$human<readable$text$content,$and$less$lets$you$look$at$them.It$is$a$paging&program$useful$for$reading$the$actual$programs$(scripts)$the$system$uses$and$for$checking$out$what$is$in$the$system$configuraDon$files.$It$allows$you$to$navigate$page$by$page,$backwards$and$forwards.

Try$this$at$home:!me@linuxbox:/etc$ less hosts.allow

Page 14: The linux command line  for total beginners

Sample'less'commands

PAGE%UP%or%b%...Scroll'back'one'pagePAGE%DOWN%or%spacebar%...Scroll'forward'one'pageUp%Arrow;%Down%Arrow%...Scroll'up'one'line,'scroll'down'one'lineG%...Go'to'end'of'text'fileg%...Go'to'the'beginning'of'the'text'file/characters%...Search'forward'to'the'next'occurrence'of'charactersn%...Search'for'the'next'occurrence'of'the'previous'searchH%...Display'help'screenq%...Quit'less

Page 15: The linux command line  for total beginners

The$Linux$File$System

Directories,*data*files,*devices*and*peripherals*such*as*a*USB*devices,*etc.,*are*treated*as*files*in*Linux.

See#this#link#for#a#simple#explana2on:#h4p://www.cyberci2.biz/2ps/understanding?unixlinux?file?system?part?i.html

The$Linux$and$Unix$file$systems$are$very$much$alike.$This$is$a$good$beginner's$tutorial:h=p://www.ee.surrey.ac.uk/Teaching/Unix/

Page 16: The linux command line  for total beginners

Doing&Things&with&Files

Copy%files%and%directories!(many!'op*ons'!available!with!cp)$ cp -i myfile1 myfile2!...Make&a&copy&of&one&file

`$ cp myfile1 myfile2 myfile3 dest-directory...Copy&several&files&into&the&dest3directory

$ cp -u *.mp3 dest_directory!...Simple'in'the'CLI,#not#so#in#a#graphical#file#manager!This#command#only#copies#up#the#.mp3#files#that#do#not#exist#in#the#des9na9on#directory#or#are#newer#than#the#exis9ng#version#in#the#directory.#Totally#cool.

Page 17: The linux command line  for total beginners

Move/rename*files*and*directories$ mv file1 file2!...Rename'file1'to'file2;'file1'is'now'gone$ mv file1 dest-directory!...Move'file1'to'the'dest7directory

Create&new&directories$ mkdir my-directory1!...Make&a&new&directory$ mkdir my-directory1 my-directory2!...Make&several&new&directories

Remove&(delete)&files&and&directories$ rm -i thisfile$ rm -r directory$ rm -i *.jpg

Page 18: The linux command line  for total beginners

The$-i$Command$Op-on$and$a$Cau-on

-i!makes!a!command!interac.ve;!you!will!be!prompted!for!consent!before&execu*ng!the!command!for!each!of!the!files!in!turn.!USE!IT!

Fun$Commands!(Do!NOT!try!at!home!or!on!a!friends!machine!)$ rm -r * ~!...OOPS!&What&did&it&just&do???$ rm -rf /!...This&command&will&recursively&(-r)&and&forcefully&(-f)&delete&all&the&files&inside&the&root&directory.&Oy,&all&gone!~$ mv /home/yourhomedirectory/* /dev/null!...dev/null&is&a&special&file.&This&command&will&move&all&the&files&in&your&home&directory&to&a&black&hole,&never&to&be&seen&again.

Page 19: The linux command line  for total beginners

About&Shell&Commands

A"shell"command"can"be:1.#An"executable"program#(like#the#files#in#/usr/bin).#It#can#be#a#shellscript,#a#compiled#program#(in#C#or#C++),#or#a#script#in#Perl,#Python,#Ruby,#etc.2.#A"shell"func6on#(miniature#shell#scripts#that#are#part#of#the#environment)3.#A"command"built"into"the"shell#(ls,#cd,#cp,#mkdir#...)4.#An"alias#–#a#command#we#can#define#in#a#special#file,#which#I#will#not#cover#here.

Page 20: The linux command line  for total beginners

What%type%of%command%am%I%using?

Use$this$shell$built+in$to$find$out$what$type$your$command$is:me@linuxbox: ~$ type typetype is a shell builtin

me@linuxbox: ~$ type cpcp is /bin/cp

me@linuxbox: ~$type lsls is aliased to 'ls --color=tty'

Page 21: The linux command line  for total beginners

Echo,&a&sound&command

Echo%is%a%shell%command%that%displays%a%line%of%text.@linuxbox: ~$ echo Hello World!Hello Worldme@linuxbox: ~$ echo *Documents Downloads medusa1.php xplanet-gnome-output.txt mybooksSince!the!wildcard!character!*!means!“match%any%characters%in%a%file%name”,!the!shell!expands!the!*!into!something!else!before!the!echo!command!is!executed.!Here!we!see!a!display!of!the!contents!of!our!current!working!directory.me@linuxbox: ~$ echo D*Desktop Documents Downloads

Page 22: The linux command line  for total beginners

The$Help$Files

Bash%has%built+in%help%for%each%shell%buil3n.%Type%in%'help'%plus%the%command:

me@linuxbox: ~$ help cdcd: cd [-L|[-P [-e]]] [dir]Change'the'shell'working'directory.Change'the'current'directory'to'DIR.'The'default'DIR'is'the'value'of'the'HOME'shell'variable.```'...and%it%goes%on,%with%a%list%of%command%op4ons%and%path%informa4on.Op4onal%items%(op4ons)%are%in%square%brackets;%a%ver4cal%bar%indicates%mutually%exclusive%op4ons.

Page 23: The linux command line  for total beginners

More%Help

Executable*programs*that*are*usable*from*the*command*line,*such*as*mkdir*or*chmod,*usually*have*a*manual*(“man$page”)*available.*The*program*man*will*display*the*man*page,*which*is*broken*into*secAons*1C8,*where*secAon*1*contains*the*user*commands.*It*lets*you*scroll*through*the*whole*length*of*it.*(Usually,*man*uses*less*to*display*the*man*pages).

Try$it:Type%in%man mkdir%...type&h&for&Help&or&q&to&quit&(h&will&open&a&help&file&with&a&nice&summary&of&all&the&*less&commands!)

Page 24: The linux command line  for total beginners

Links

Useful'Websites'for'lists'of'commands,'examples'or'explana6ons:h"p://ss64.com/bash//...An$A%Z$Index$of$the$Bash$command$line$for$Linuxh"p://linuxcommand.org/lc3_learning_the_shell.php/...A$shell$intro$for$beginners

Excellent(Funh"ps://www.digitalocean.com/community/tutorials/top7107linux7easter7eggs;...ENJOY!

Page 25: The linux command line  for total beginners

Part%2%will%cover%some%shell%buil3ns%and%command%op3ons

Contact:(h*p://www.info@corriewa*.com


Recommended