ConqueringTheCommandLineForBeginners

Open Week -- Conquering the Command Line for Beginners -- mhall119 -- Wed, May 4

   1 [17:02] <ClassBot> Logs for this session will be available at http://irclogs.ubuntu.com/2011/05/04/%23ubuntu-classroom.html following the conclusion of the session.
   2 [17:03] <mhall119> Hello everybody
   3 [17:04] <mhall119> welcome to my session on using the command line
   4 [17:04] <mhall119> now, there's a lot of old stereo-types about Linux, that you have to put all kinds of cryptic stuff into the command line to get things done
   5 [17:05] <mhall119> but in Ubuntu, you can do pretty much everything in the GUI with the mouse, and you'll never ever *need* the command line
   6 [17:05] <mhall119> but, power users still use it all the time
   7 [17:05] <mhall119> why?
   8 [17:05] <mhall119> Well, because it's fast
   9 [17:05] <mhall119> it's fun
  10 [17:05] <mhall119> and, believe it or not, it's easy!
  11 [17:06] <mhall119> this session isn't going to make you masters of the command line
  12 [17:06] <mhall119> but it will make you more comfortable with it
  13 [17:06] <mhall119> and show you some of the cool things you can do with just a handful of commands
  14 [17:06] <mhall119> so, to get things started, lets all pull up a terminal
  15 [17:07] <mhall119> if you're using Unity, you can hit Alt-F2 and type "gnome-terminal"
  16 [17:07] <mhall119> you should also be able to it ctrl-alt-T to get a new terminal window
  17 [17:07] <mhall119> if you're on KDE, alt-f2 then "kterm" should do it
  18 [17:07] <mhall119> if you're on anything else, I'll just assume you know how already ;)
  19 [17:08] <mhall119> everybody able to get a terminal window up?
  20 === zobbo is now known as zobbo|away
  21 [17:09] <mhall119> I'll take your silence to mean that everybody has a terminal open
  22 [17:09] <mhall119> or that nobody is listening
  23 [17:09] <mhall119> either way, we're moving on
  24 [17:10] <mhall119> first off, type "pwd" and hit enter
  25 [17:10] <mhall119> "pwd" stands for "Present Working Directory", and it tells you where you are in the file system
  26 [17:10] <mhall119> sorry, type "pwd" in your terminal, not IRC
  27 [17:11] <mhall119> You can think of the command line as a super file manager, and just like Nautilus (the GUI file manager), you are always "in a directory"
  28 [17:11] <mhall119> so, pwd tells you which directory you're in
  29 [17:11] <mhall119> next, type "ls" and hit enter
  30 [17:12] <mhall119> "ls" stands of "list" and it'll do exactly that, list what's in your current directory
  31 [17:12] <mhall119> so now you know where you are and what's there
  32 [17:12] <mhall119> next, let's get moving
  33 [17:12] <mhall119> "cd" stands for "change directory"
  34 [17:12] <mhall119> type "cd /tmp/" in your terminal and hit enter
  35 [17:13] <mhall119> that will change your current directory to /tmp/
  36 [17:13] <mhall119> you can verify this by running "pwd" again
  37 [17:13] <mhall119> and you can run "ls" again to see what's in /tmp/
  38 [17:13] <mhall119> /tmp/ is used by a lot of programs as a place to put "temporary" files
  39 [17:13] <mhall119> we're going to use it for the files we're going to play with
  40 [17:14] <mhall119> everybody in /tmp/?
  41 [17:14] <mhall119> any questions so far?
  42 [17:15] <mhall119> ok, let's make a file
  43 [17:17] <mhall119> ok, run "touch test"
  44 [17:17] <mhall119> "touch" creates a new, empty file, in this case we named it "test"
  45 [17:17] <mhall119> next we're going to make a directoy
  46 [17:17] <mhall119> run "mkdir classroom"
  47 [17:18] <mhall119> "mkdir" as it's name implies, makes a directory, in this case we named it "classroom
  48 [17:18] <mhall119> now let's put our test file into our new directory
  49 [17:18] <mhall119> run  "mv test classroom"
  50 [17:18] <mhall119> "mv" means "move", and does exactly that
  51 [17:19] <mhall119> now "cd classroom" to move into that new directory
  52 [17:19] <mhall119> and "ls" to see that "test" is in there
  53 [17:19] <mhall119> now, let's put some content into our test file
  54 [17:20] <mhall119> run: echo "Hello world" > test
  55 [17:20] <mhall119> "echo" just prints out what you pass it, in this case "Hello world"
  56 [17:20] <mhall119> the > is called a redirect, I'll explain what it's doing in a minute
  57 [17:20] <mhall119> but, suffice it to say, that full command puts "Hello world" into our test file
  58 [17:21] <mhall119> you can check that by running "cat test"
  59 [17:21] <mhall119> "cat" will print the contents of a file out for you to see
  60 [17:22] <mhall119> now let's make a copy of our test file
  61 [17:22] <mhall119> run "cp test backup"
  62 [17:22] <mhall119> "cp" stands for copy
  63 [17:22] <mhall119> now if you run "ls", you should see both "test" and "backup"
  64 [17:22] <mhall119> and if you run "cat backup" you should see "Hello world", because that's what was in "test"
  65 [17:23] <mhall119> okay, now that's pretty much the basics of using the command line for file management
  66 [17:23] <mhall119> any questions on that before we get into the fun stuff?
  67 [17:24] <ClassBot> kkitano asked: what does cat stand for?
  68 [17:24] <mhall119> "cat" stands for "concatenate"
  69 [17:25] <mhall119> because you can give "cat" multiple files, and it will print their contents one after the other
  70 [17:25] <mhall119> concatenating them
  71 [17:25] <mhall119> try it out by running "cat test backup", and you'll see "Hello world" twice
  72 [17:25] <mhall119> okay, now for the fun stuff
  73 [17:26] <mhall119> one of the most powerful aspects of the Linux command line is the ability to redirect input and output
  74 [17:26] <mhall119> we did that earlier with >
  75 [17:26] <mhall119> > redirects the output from a command and puts it into a file
  76 [17:26] <mhall119> we used it to put the output from "echo" into our file "test"
  77 [17:27] <mhall119> you can also use >>, which will append to the end of a file, instead of replacing all it's existing content
  78 [17:27] <mhall119> < will take input from a file and pass it to a program
  79 [17:28] <mhall119> finally, the "pipe" symbol: |
  80 [17:28] <mhall119> this will redirect input and output between 2 programs, instead of between a program and a file
  81 [17:29] <mhall119> using pipes, you can chain multiple programs together
  82 [17:30] <mhall119> I'm going to introduce some common command line programs, and show you how to do powerful things by joining them together with pipes
  83 [17:30] <mhall119> our first one is "ps", which will give you a list of running processes
  84 [17:30] <mhall119> go ahead and run "ps"
  85 [17:31] <mhall119> is probably won't show much, because the default settings only show what's running on your current terminal
  86 [17:31] <mhall119> to get a list of everything running on your system, we need to pass it some additional "flags",
  87 [17:31] <mhall119> run "ps -ef" and you'll see a whole lot more information
  88 [17:32] <mhall119> don't worry about what it all is right now
  89 [17:32] <mhall119> we're only going to worry about the first 2 columns, which are the username and the process id or PID
  90 [17:34] <mhall119> next is "grep", which is a very powerful text searching tool
  91 [17:35] <mhall119> try "grep 'world' test"
  92 [17:35] <mhall119> and it'll search the contents of our test file for the word "world"
  93 [17:36] <mhall119> now, let's combine them with pipes to do something useful
  94 [17:36] <mhall119> suppose we want to list all the processes that involve "python"
  95 [17:36] <mhall119> just run "ps -ef |grep python"
  96 [17:36] <mhall119> this will get a list of all the processes on your system, and send that list to grep, which will only print out those lines that contain the word "python"
  97 [17:38] <mhall119> any questions about what we just did?
  98 [17:39] <mhall119> okay, moving on to our next command: "find"
  99 [17:39] <mhall119> "find" will give you a list of files in a directory and all it's sub directoryies
 100 [17:40] <mhall119> fun "find /tmp" and you'll see a lot of information go by
 101 [17:40] <mhall119> that's every file under the /tmp directory
 102 [17:41] <mhall119> now let's say we want to see every file under /tmp/ where the word "test" is in the file name, or in the name of one of it's parent directories
 103 [17:41] <mhall119> we can do this by "piping" the output from find into grep again
 104 [17:41] <mhall119> run "find /tmp |grep test"
 105 [17:42] <mhall119> you will probably see more than just out test file, that's okay
 106 [17:42] <mhall119> another useful command is "file"
 107 [17:43] <mhall119> "file" just tells you what kind of file something is
 108 [17:43] <mhall119> run "file test"
 109 [17:43] <mhall119> and it should tell you that it's an ASCII text file
 110 [17:44] <mhall119> it's important to note that, unlike Windows, Linux doesn't need file extensions to know what kind of file something is
 111 [17:44] <mhall119> run "cp test test.png" to make a copy of our text file called "test.png"
 112 [17:45] <mhall119> if this were Windows, it woud think that test.png is an image
 113 [17:45] <mhall119> but if you run "file test.png", it still knows that it's content is only text
 114 [17:45] <mhall119> okay, we don't need to keep this file around, so remove it by running "rm test.png"
 115 [17:45] <mhall119> "rm" obviously, stands for "remove"
 116 [17:46] <ClassBot> suprengr90 asked: is it worth mention the location on keyboard of "|" as it doesn't show on the keyboard [exactly] the same?
 117 [17:48] <mhall119> on standard US-en keyboards, it's shift+backslash
 118 [17:48] <mhall119> I'm not sure about other keyboard layouts
 119 [17:49] <mhall119> okay, now for some real fun
 120 [17:49] <mhall119> "xargs" is a very handy program that will take each line that it takes as input, and pass it to another program
 121 [17:50] <mhall119> so, let's say we want to know all the python scripts in /usr/bin
 122 [17:51] <mhall119> since the filenames in /usr/bin don't end with .py, even if they're python files, we can't use find+grep like we did before
 123 [17:51] <mhall119> but, using "xargs" and "file", we can check the content type of each
 124 [17:51] <mhall119> so try running: find /usr/bin |xargs file |grep 'python.*script'
 125 [17:51] <mhall119> let me break that down
 126 [17:52] <mhall119> 1) "find /usr/bin" this will produce a list of all the files in /usr/bin
 127 [17:52] <mhall119> we then pass that list to:
 128 [17:52] <ClassBot> There are 10 minutes remaining in the current session.
 129 [17:52] <mhall119> 2) "xargs file", this says for every file in the list, run that file through the "file" command
 130 [17:53] <mhall119> this gives us a list of all the files + their content type information
 131 [17:53] <mhall119> which we pass to
 132 [17:53] <mhall119> 3) "grep 'python.*script'" which will filter the output, displaying only those that contain "python" and "script" with any amount of text between them (the .* part)
 133 [17:54] <mhall119> now, take a minute to think about the number of steps and repetition it would take to do the same from the GUI with a mouse
 134 [17:55] <mhall119> alright, a couple final commands before the session is over
 135 [17:56] <mhall119> the "kill" command takes a process ID, like we say using "ps", and forces the process to end
 136 [17:56] <mhall119> be careful with this command
 137 [17:57] <ClassBot> There are 5 minutes remaining in the current session.
 138 [17:57] <mhall119> but, suppose you wanted to kill all gwibber processes
 139 [17:57] <mhall119> gwibber is the default Ubuntu twitter client
 140 [17:58] <mhall119> we can use "ps -ef |grep gwibber" like we did before to see all the gwibber processes
 141 [17:58] <mhall119> and then run "kill" on each one
 142 [17:58] <mhall119> but that can be tedious
 143 [17:58] <mhall119> instead, let's introduce "awk"
 144 [17:59] <mhall119> awk is a very powerful scripting too, but we're going to use it for something very basic, printing out the 2nd column only from ps
 145 [17:59] <mhall119> the 2nd column is the process id
 146 [17:59] <mhall119> so run: ps -ef |grep gwibber | awk '{print $2}'
 147 [17:59] <mhall119> now you have the process id's for the gwibber processes
 148 [18:00] <mhall119> now we can use xargs again to pass each one to kill
 149 [18:00] <mhall119> (again, don't actually try this)
 150 [18:01] <mhall119> ps -ef |grep gwibber |awk '{print $2}'|xargs kill
 151 [18:01] <mhall119> will find all the gwibber process ids and pass them to the "kill" command
 152 [18:01] <mhall119> now, it's hard to remember all the ways to use commands, not even power users remember them all
 153 [18:01] <mhall119> fortunately, there's easy ways to find out
 154 [18:02] <mhall119> almost every command will take the "--help" flag and give you a short description of how to use it
 155 === ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat || Event: Ubuntu Open Week - Current Session: Introduction to AppArmor - Instructors: jjohansen
 156 [18:02] <ClassBot> Logs for this session will be available at http://irclogs.ubuntu.com/2011/05/04/%23ubuntu-classroom.html following the conclusion of the session.
 157 [18:02] <mhall119> also, "man" will give you the full documentation

MeetingLogs/openweekNatty/ConqueringTheCommandLineForBeginners (last edited 2011-05-04 17:58:48 by x1-6-00-26-f2-da-01-61)