command line interface (16_04)-2

Upload: haley-hamill

Post on 01-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Command Line Interface (16_04)-2

    1/8

    This is an introduction to the commandinterface.The command line interface is a way ofworking with files and foldersthat involves typing a command as opposedto pointing and clicking with a mouse.It's incredibly useful for data scienceand will be used both when you programmingin R and when you're using other tools inthe data scientist tool box.So, what is the command line interface?Almost every computer that's shipped todaycomeswith some form of a command lineinterface.For this class, the command lineinterfaces we'll be using, for Windows,it'sGit Bash and you'll see more about that inthe "Introduction to Git".And for Mac and Linux, it's the terminal.[SOUND] So what can the Command LineInterface do?Well, you can use it to navigate between

    folders.You can use it to create files, folders,and programs, andthen you can use it to edit those filesand folders.Finally you can use it to run computerprograms which is one ofthe main uses we'll be having for it in adata science program.So first we need to understand the basicsof directories.So directory is just another name forfolder, and it's the

    common name that you'll be hearing about alot in this class.So being in a directory and moving to thedirectory.Directories on your computer are organizedlike a tree.So there's one directory will be inside ofanother directory.And so we can navigate betweenthose directories using the command lineinterface.So, here's an example, we have a musicdirectory and there

    are three sub directories, one for eachcomposer here, and so youcan imagine that the Debussy directory isinside of the musicdirectory and so when we think about thestructure of this directory.We think of being either inside of adirectory or being up from a directory.So, one directory up from the Debussydirectory is the music directory.

  • 8/9/2019 Command Line Interface (16_04)-2

    2/8

    In general, the directory that's up fromthe directory that you'rethinking of is the directory that containsthe directory you're thinking of.[SOUND] So the directory structure on yourcomputer looks something like this.You have sort of a tree structurewhere each sub, each directory has subdirectories.And so a couple of special cases that youmight want, that need to be considered.First it's the root directory.So this is the root directory up here atthe top of the tree.And it's indicated at the top because itcontains all of the other directories.And it's usually the shorthand for the newdirectory is a slash.So a slash indicates the new directory.[SOUND] The other main directory that youmight wantto be paying attention to is your homedirectory.So that's down here.

    So, the home directory is where you log inwhen you log in your computer and mostof your personal stuff, your photos, yourmusic, yourR function will all be in your personaldirectory.And so that would be a sub directory ofthe root directory again.[SOUND] So if you want to navigate betweenthose directories using thecommand line interface, first you have tohave a command line interface.So, for Windows users you should open the

    Start menu,search for git bash, and then open gitbash the application.If you're a Mac user, you open Spotlight,search for terminal, and then open theterminal application.When you open the terminal or GitBash,you'llsee something that looks a lot like this.So this is basically application where youmostly see blank space.And so what will see is what's called theprompt.

    Which is your username.Here in this case it's Sean followed by[INAUDIBLE].And then so when you open this up, whenyou openup the command line interface, you startin your home directory.An important term that we'll be using alot is called your working directory.So that's whatever directory you're in at

  • 8/9/2019 Command Line Interface (16_04)-2

    3/8

    the moment.So when you open you're command lineinterface, you're working directory isyour home directory.If you move into a sub directory calledmusic, then your working directory will bemusic.[SOUND] So another thing to keep in mindis thepath, so, people think about the path inthe directory structure.And so we'll be moving around along thepath.And so, suppose that you're down here,you're downin your home directory which is signifiedby the tilde.And so you might want to know what are thedirectors that are, directories that areup from your home directory.So the first directory that's up is theusers directory.And then the next directory up is the rootdirectory.

    So you can imagine the path is the set ofdirectories youhave to follow in order to get back up tothe root directory.[SOUND] So if you type PWD when you're inthe command line interface.So here we are at that prompt.You can see the username, the dollar sign.Then if you type PWD and hit return, whatyou'll see is the path.So, this is the path to the workingdirectory that you're in right now.So in this case, this person is in the

    root directory, then users, and underneaththat they're in Sean.So, that's the path to the workingdirectory that they're in.So, PWD stands for print workingdirectory.[SOUND] If you use PWD, you can use thatat any time in your directory,it will always print the directory thatyou're in, that you're looking at rightthen.So, there's always a recipe that goes withusing command line interfaces.

    So what you'll do is you'll use what'scalled the command.So in the previous case it was called PWD.Then you'll use something called flags.These are like parameters or things thatyou'll pass tothe command that tell it what kind ofbehavior to take.And then arguments can be what command isgoing to modify

  • 8/9/2019 Command Line Interface (16_04)-2

    4/8

    so what files it'll look at and thingslike that.Depending on the command, you may or maynot need to use flags or arguments.So for example when you were using PWD, itwas a command and it didn't require anysort of options, it didn't require anyflagsor arguments in order to change what washappening.[SOUND] So again, what we can use is wecan use pwd to print the current workingdirectory.So, if I'm a different user then, when igo to myhome directory and I type PWD, I'llactually get a different path.It's the path, users followed by Jeffbecause now I'm a different user.And so I have a different current workingdirectory.Clear will clear all the commands in yourcurrent command line interface.So for example, suppose you typed a bunch

    of commands.In this case, we typed PWD, and we havesomething here in users Jeff,and we don't want to have that clutteringup the space in our interface anymore.Then what we can do is type just theword clear, and that will clear outeverything inthe command line interface, and you'll beleft withjust a prompt at the top of the screenagain.And a very important command is LS.

    So, LS lists files and folders in thecurrent directory.So, if you type LS, for example, indirec, [INAUDIBLE] directory, you mightget all the subfolders.So for example, you might get the desktop,the photos, and music.And last, dash A lists both hidden andunhidden files.And so for example, the desktop, photos,and music were all unhidden, but thenthereare these files, the trash files and on a

    Mac and .DS underscore store file.Those files are start, start with a dotand so they're hidden when you type LS.They'll also be hidden if you look at in afolder structure on the computer.But LS dash A reveals those folders.LS dash A lists some details for thosefolders.So for example it lists information aboutthe

  • 8/9/2019 Command Line Interface (16_04)-2

    5/8

    size of the folders and stuff like that.An important note is that both dash A anddash L are flags, sothey're prec, preceded by a dash and oftena flag is preceded by a dash.You can combine them together into oneflag byjust catnating the two letters togetherafter the dash.[SOUND]CD is another command.It stands for change directory.It takes as an argument the directory thatyou want to go visit.So if you're in a particular directory andyou want to move to another directory,you can pass to CD a particular path andit will take you to that directory.So if you type CD with no argument atall it'll just take you straight to yourhome directory.And then an important one is if you CD.What that'll do is it'll changedirectories up one level, so

    you'll just move up one level if you do CDdot dot.So for example if I'm in my home directoryand I type CD Music/Debussy.Then, I print what directory I've movedto.I will have moved to that, sub directoryby using the CD command.Then if I wanted to move up just onedirectory to the music folderwhat I could is I could type CD dot dot,and what that woulddo is it would just move me up one

    directory, and so if Iprint working directory now I move downinto the, or up into the music directory.[SOUND] If I just type CD and I don't typeanything after it like thisright here then what I end up with is justgoing back to my home directory.Mkdir or M K D I R stands for makedirectory so,it's basically just like what you'd do ifyou say right click create new folder.And so, what you need to do is you givenan argument that's the name of the

    directory you're creating.So, for example, suppose I'm here again inmy home directory, Jeff.And I type MKDIR, space, documents.What that will do is it will create a newfolder called documents.So if I type LS, the list out all thedirectories that I see.One of the directories will now bedocuments

  • 8/9/2019 Command Line Interface (16_04)-2

    6/8

    that I've just created with this MKDIRcommand.So now I can actually move into thatdocument's directory, and then print mycurrent working directoryand you'll see that I'm now moved intothis new documents directory that Icreated with MKDIR.So touch creates an empty file.So if I type, here again, touch, testfile, what that will dois that it will actually just create afile called test underscore file.And so if I then list out everythingthat's in my home directory,you'll see that now one of the fileslisted is test underscore file.So if you need to create a file you usetouch.CP stands for copy.So suppose you want to take a file andcopy it into another file.What you do is pass copy two arguments.First is the file that you want to copy

    andthen the place that you want it to becopied to.So for example if I type CP and then thetest file that I've created.And then a directory, documents.What it will do is it will take the testfile and it will move it into thedocuments directory.So if I copy that file there and then Imoveinto the documents directory by typing, CDdocuments, and I list

    out list out the files, you'll see thatone of thefiles that is in that directory now is thetest file.You can then CD dot dot, and that will putyou back into your home directory.[SOUND] So CP can, CP can also be used forcopying the contents of directories.So suppose you want to move one directoryinto another directory, youcan do that but you need to use the dash Rflag.So the dash R is a flag that stands for

    recursive.And so if you have the line CP.Dash R, so that's CP with a flag dash R,documents to more documents, it willcopy the entire folder, or the entiredirectorydocuments into a new directory called moredocuments.So for example, what we can do here is wecan use MKDIR to create more docs.

  • 8/9/2019 Command Line Interface (16_04)-2

    7/8

    And then what we can do is we can usethis CP dash R command to move documentsinto more documents.Then if we move in to the more docscommand usingC or directory using CD and we loop, usethe iscommand to list everything in thatdirectory, we see that there'sa test file in that directory because it'sbeen copied over.RN stands for remove.So, RN is sort of like delete.You basically use it to delete files thatyou don't want.So for example if we list out the files inmyhome directory, we now have all these newfiles that we've created.Suppose we want to remove this test file.Then I just type RM space test underscorefile and it will remove that file.So, if I list things out again you'll seethat the test file is now missing.

    You can use RM to remove entiredirectoriesand their contents by using the dash Rflag.So, in other words you can recursivelymove all of the files in a directory.But you should be very careful when you dothis becausethere is no undo so if you delete it, it'sgone.So be very careful when you're using RMdash R.So if I use in my home directory RM dash R

    more docs.It will delete the more docs directory andall of the files that were inside it.And so you can see, again, if I LS nowafter doing that, all of the more docs hasbeen deleted.MV stands for move, so with MV you canmove files between directories.So again, suppose I crate this new filewith touch.So I created that file.And suppose I want to move it intodocuments, then I would type MV space the

    nameof the file, new underscore file, and thenthe directory I wanted to move it todocuments.And so what I would end up with if I typedls now is that there wouldn't be a file inmy main directory, but if I CD, I move tothe documents directory.And I list out everything that's in there.I end up seeing that I've moved the new

  • 8/9/2019 Command Line Interface (16_04)-2

    8/8

    file into that directory.[SOUND] You can also use MV to renamefiles.So if you move a file to another file witha new name it will just change the name.So for example here if I list the files ofthedirectory I'm in then there's two files,Test File and New File.And I want to change the name of new file.I can type MV, space, new underscore file.That's the file name I was, it waspreviously.And I want to create it a renamed file.So I'm moving, I'm changing the name ofthis file to renamed file.And then if I list out all the files thatexist now, now there's test file andrenamed file, which had the contents thatwere previously in new file.[SOUND] And so echo will print whateverarguments you provide.So for example, if you to echo, new, helloworld!,

    like this, what it will do is just printout hello world!Echo is very useful for printing outthe contents of particular variables thathavebeen stored that we'll talk about later.Date will predict pre, pre, print thedate.So if you type date like this you'll getthe datethat this actually in this case that theslides were created.[SOUND] So, summary of commands are PWD,

    which is printing the working directory.It's telling you what directory you're in.Clear, which clears the screen.LS, which lists the files in the currentworking directory.CD, which allows you to change workingdirectories.MKDIR, which creates a new directory,touch, which creates a newfile, CP, which create, copies a file, RM,which removes afile or directory if you use the dash Rcommand, or

    flag sorry, and MV means moving a file orrenaming a file.And then date and echo are ways to lookat the date or to echo out a particularcommand.