Command Line Q&A

   1 [22:04] <ClassBot> Logs for this session will be available at http://irclogs.ubuntu.com/2011/01/29/%23ubuntu-classroom.html following the conclusion of the session.
   2 [22:05] <pleia2> ok, unfortunately our scheduled instructor is a no-show, but this is a popular session so paultag and I will fill in with a more generic command line Q&A
   3 [22:05] <pleia2> When we use the command line we are using a shell. In this context a shell is the most basic way a user can interact with a computer's operating system
   4 [22:06] <pleia2> A shell is a piece of software. A unix type system can have many different shells installed.
   5 [22:06] <pleia2> by default, Ubuntu uses "bash" for the shell that the user sees (and "dash" for /bin/sh - the shell that the system uses)
   6 [22:06] <pleia2> For this session we don't need to be concerned with the differences between dash and bash, but be aware that they exist
   7 [22:08] <pleia2> now we won't have a set agenda, so we're just going to open the floor to questions, so open up a terminal and as away!
   8 [22:08] <pleia2> s/as/ask
   9 [22:08] <paultag> anyone? bueller? bueller?
  10 [22:09] <pleia2> ok, well while we wait for questions I can run through some of the basics I first learned
  11 [22:09] <paultag> There :)
  12 [22:09] <ClassBot> paultag asked: Why do we use that scary black terminal thing? What use is it?
  13 [22:10] <pleia2> Well, the easy answer is that you typically don't have to, these days with Ubuntu there are ways to do many of your common system tasks via the graphical interface
  14 [22:10] <ClassBot> obengdako asked: without screen and byobu would there be a nicer way of multitasking in shells not including using & and disown
  15 [22:10] <paultag> Humm, what an interesting question
  16 [22:10] <pleia2> but it helps when you want to learn more about how your system works, have to do some complicated changes (maybe due to a hardware problem, graphics not working?) or if you need to do debugging (program crashing? launch it from a terminal instead to see if it tells you the problem )
  17 [22:11] <paultag> the UNIX system was created in the footsteps of the ITS system at MIT. It really was designed to allow processes to stop, pause, and resume without breaking like other systems of the day
  18 [22:11] <paultag> so you can always stop / start apps, using ctrl + z, fg / ctrl + s / q and so forth
  19 [22:12] <paultag> but it was designed to be "under" your bash shell, screen and byobu really are unique because they let you buffer bash through that
  20 [22:12] <paultag> and it lets you really isolate processes in a way that it was not really designed to do, so the short answer is no. If you want to detach, quit, and login elsewhere on the same system, you'll need something like screen
  21 [22:12] <paultag> or know how to redirect stuff in /proc/ to your shell, but that's not basic!
  22 [22:12] <ClassBot> vigs asked: could you list a few commands of intermediate level (not too simple like cd etc.) which can be used to easily do tasks that people regularly do ?
  23 [22:12] <pleia2> this is a good question, and lets us get into the basics a little
  24 [22:13] <pleia2> so for those of you who never opened a terminal before (or feel hopelessly lost when you do) the "cd etc" in this question include:
  25 [22:13] <pleia2> ls
  26 [22:13] <pleia2> when you open a terminal and type "ls" it gives you a directory listing of all your files, by default when you open a terminal this will be in your home directory
  27 [22:14] <pleia2> to change directories you use "cd" so to go into folder foo/ you "cd foo"
  28 [22:14] <pleia2> to manipulate files you have commands like move "mv" and copy "cp"
  29 [22:15] <paultag> (rename is just a move;)
  30 [22:15] <pleia2> to edit a file you can use the command line editor "nano" so: nano filename
  31 [22:15] <pleia2> (no need to include .txt in the filename)
  32 [22:15] <pleia2> now, intermediate commands
  33 [22:15] <pleia2> there are LOTS of them!
  34 [22:15] <ClassBot> Ddorda asked: even though you said we should be concerned about it, you mentioned dash, and i know there are more few kinds of shells. can you explain the differences?
  35 [22:15] <pleia2> paultag: hold up :)
  36 [22:16] <paultag> Shoot, sorry :)
  37 [22:16] <pleia2> "history" is a good one, it gives you the full history of commands you've typed
  38 [22:16] <pleia2> "df" tells you how much space you have in different partitions (try "df -h" for human readable)
  39 [22:17] <paultag> don't forget stuff like `netstat' and `lsof'
  40 [22:17] <paultag> those can help you see active connections to files or external sockets :)
  41 [22:17] <pleia2> "lsmod" is super helpful when debugging hardware problems - it's "ls" (list) plus "mod" which means modules
  42 [22:17] <pleia2> so it "lists modules" that you have loaded
  43 [22:17] <pleia2> modules are like drivers in windows
  44 === Flavio is now known as Guest43676
  45 [22:18] <pleia2> you can also manipulate permissions of files on the command line, using commands like "chmod" "chown" and "chgrp"
  46 [22:19] <pleia2> a quick google search for "linux commands" will find lots more :)
  47 [22:19] <paultag> `clicompanion' rocks for this, as well
  48 [22:19] <paultag> and it was written by a member of the beginners team, so it's aimed for new users!
  49 [22:19] <pleia2> paultag: ppa?
  50 [22:19] <paultag> Yeah, I'll grab the repo ID
  51 [22:20] <paultag> http://okiebuntu.homelinux.com/okwiki/clicompanion <-- that's the howto :)
  52 [22:20] <paultag> pleia2: mind if I move on to Ddorda's question?
  53 [22:20] <pleia2> paultag: please do
  54 [22:21] <pleia2> The author of the Bash Cookbook speaks at the Philadelphia LUG I used to run, he did a presentation on bash vs dash a couple years ago, his slides are here: http://princessleia.com/plug/2008-JP_bash_vs_dash.pdf
  55 [22:21] <paultag> Each shell has a different feature set -- just like KDE / Fluxbox / GNOME / Unity have their strong points and weak points, so do the shells. Each has a slightly different dialect of the same language
  56 [22:21] <paultag> shells like zsh have a cult following
  57 [22:21] <paultag> csh trys to be like C, so scripting will be easier to those with C background ( but it ends up just being confusing )
  58 [22:22] <paultag> 99% of the time bash does what you need it to do -- but there are those "1%-ers"
  59 [22:23] <ClassBot> gpc asked: Why isn't the .txt needed?
  60 [22:23] <paultag> the full filename on the computer includes ".txt" -- unless of course it does not on the machine ;) -- so to edit a file "foo.txt" you need to use `nano foo.txt', and not `nano foo'
  61 [22:24] <pleia2> +if
  62 [22:24] <paultag> because to the GNU userland / filesystem / kernel foo.txt is a totally different file then foo, or FOO.txt
  63 [22:24] <paultag> when in doubt, use tab
  64 [22:25] <pleia2> Linux doesn't tend to be as strict about extensions as a system like Windows is, so while Windows may complain that a file has no ".txt" on it, it's pretty common in linux
  65 [22:25] <paultag> +1
  66 [22:25] <paultag> we use magic numbers at the head of the file
  67 [22:25]  * pleia2 hasn't used .txt in years :)
  68 [22:25] <paultag> that's why you see "#!--->" at the top of a script
  69 [22:25] <paultag> pleia2: me neither ;)
  70 [22:26] <paultag> 22:25 < mhall119> for more command line fun, run "file foo" and it'll tell you what kind of file "foo" is.
  71 [22:26] <paultag> Yeah, this. That uses the magic number of a file to "ID" it
  72 [22:26] <pleia2> great tip :)
  73 [22:26] <ClassBot> mhall119 asked: Where can I find helpful instructions on how to use all these commands?
  74 [22:27] <paultag> manual pages!
  75 [22:27] <pleia2> most commands have "man" pages, so if you wnt to learn more about ls: man ls
  76 [22:27] <paultag> +1 pleia2 :)
  77 [22:27] <pleia2> some also have info pages
  78 [22:27] <pleia2> info ls
  79 [22:27] <paultag> manual pages are also great for looking at files, as well
  80 [22:27] <pleia2> and most also have a --help, so: ls --help
  81 [22:28] <paultag> +1
  82 [22:28] <ClassBot> txomon asked: the X enviromenth is executed in a shell, how can it execute more than a windows? doesn't it suspend it?
  83 [22:28] <paultag> Interesting question, txomon
  84 [22:28] <paultag> tritium: the trick here is using subprocesses
  85 [22:29] <paultag> so, when you start X, it uses raw library calls to the video subsystem ( through a networked interface ) to draw your windows
  86 [22:29] <paultag> X it's self is just a server, much like apache2
  87 [22:30] <paultag> and just like any server, when you connect with a client ( like a web browser for apache ), it handles each client on it's own
  88 [22:30] <paultag> in that way, when you start an X application, it connects ( like a browser ) to it, and "talks" back and forth
  89 [22:31] <paultag> and so each process is isolated and talking over a socket :)
  90 [22:31] <ClassBot> mhall119 asked: Oh No! "ls --help" is too long and scrolls out of view, how can I display it a page at a time?
  91 [22:31] <pleia2> you can pipe the output to a command "less" or "more"
  92 [22:31] <pleia2> ls --help | less
  93 [22:32] <pleia2> that confines it to your terminal, you just hit the space bar to page down, esc to exit
  94 [22:33] <ClassBot> Ddorda asked: recently i saw someone climbing up in the shell prompt! was it magic?
  95 [22:33] <pleia2> if you hit the up arrow, you can get to the last command you typed
  96 [22:33] <pleia2> *super* helpful!
  97 [22:33] <pleia2> if you hit the up arrow more, it scrolls through your history one line at a time
  98 [22:33] <ClassBot> txomon asked: When I press cntl+alt+<functionkey> I switch to another shell, is there any difference between that and executing a shell in X enviroment?
  99 [22:34] <paultag> Not one bit. You won't be able to start X apps by default ( you'll need to start an X server session and export it ), but you have the exact same shell as an X shell
 100 [22:34] <paultag> but on TTY1, you can run something like `export DISPLAY=":0.0"' to use X apps
 101 [22:34] <paultag> but no, same commands, same system
 102 [22:35] <pleia2> 14:35:40 < Ddorda> for ex. if you get a super long answer to "ls", he didn't use |more, but scrolled up
 103 [22:36] <pleia2> clarification from previous question
 104 [22:36] <pleia2> most terminals allow you to to hold down shift and hit the "page up" key to read the terminal buffer
 105 [22:36] <ClassBot> mhall119 asked: what if I want to re-run a command I ran like 50 commands back, I remember the start of it, but not the parameters, do I have to hit 'up' 50 times?
 106 [22:37] <pleia2> no, there are a couple ways to tackle this
 107 [22:38] <pleia2> you can search through the history, so if you know your command starts with "ls" you can: history | grep ls
 108 [22:38] <paultag> !command is usually the most common way ( if it was the last command you ran with that binary )
 109 [22:38] <paultag> or grepping history, too :)
 110 [22:38] <pleia2> you'll see a number next to the list it returns, so in mine I see:
 111 [22:38] <pleia2>   499  info ls
 112 [22:38] <pleia2> to run that again, I just: !499
 113 [22:39] <pleia2> there are also fancy ways to search bash history, but they aren't consistant between shells
 114 [22:39] <pleia2> 14:39:09 < mhall119> paultag: pleia2: with bash, you can also hit ctrl+r and start typing the beginning of the command
 115 [22:39] <pleia2> 14:39:15 < mhall119> it'll search ~/.bash_history
 116 [22:39] <pleia2> ^^ a good example :)
 117 [22:39] <paultag> +1 there
 118 [22:40] <ClassBot> UndiFineD asked: I always forget how I executed a command in the past, how can i eassily retrieve that ? , I think it was something like: !command
 119 [22:40] <paultag> 22:39 < UndiFineD> paultag: is it !command:p to show the command before execution ?
 120 [22:40] <paultag> UndiFineD: yes, thanks :)
 121 [22:41] <ClassBot> txomon asked: When I want to run a X app, I must start a shell, but if I close it, the app closes. Is there any way to let it work without that shell?
 122 [22:41] <paultag> Oh yes, this is a good one
 123 [22:41] <paultag> the trick is when you close a TTY it's like "hanging up" the side of your TTY
 124 [22:41] <paultag> so, there's a fun binary called "nohup", which takes the SIGHUP call and trashes it
 125 [22:42] <paultag> so, to run nm-applet in a x terminal and close it, try running it likeL
 126 [22:42] <paultag> `nohup nm-applet &'
 127 [22:42] <paultag> it will log output for you as well :)
 128 [22:43] <paultag> it's really handy and fun to use
 129 [22:43] <paultag> that or alt+f2
 130 [22:43] <paultag> then the system'll take care of forking it and pushing it to the background
 131 [22:44] <ClassBot> vigs asked: When I do a ctrl+r search, it takes me to the last time i used the command. Suppose I want to browse through all the uses of that command, is there a way to do that with Ctrl+r ?
 132 [22:45] <paultag> I don't think so :(
 133 [22:45] <paultag> try patching bash! ;)
 134 [22:46] <paultag> usually tabing would work, but I've not tried it
 135 [22:46] <paultag> nope, tabbing fails. I have no idea
 136 [22:46] <ClassBot> LeGambitteur asked: is the command history limited ? If so how to increase it ?
 137 [22:47] <pleia2> oh
 138 [22:47] <pleia2> 14:46:54 < mhall119> pleia2: hit ctrl-r again while it's searching and it'll step through anything that matches the current search
 139 [22:47] <pleia2> there we go :)
 140 [22:47] <pleia2> the command history is 500 by default
 141 [22:47] <paultag> 22:46 < mhall119> pleia2: hit ctrl-r again while it's searching and it'll step through anything that matches the current search
 142 [22:47] <pleia2> to increase it you'll need to edit your .bashrc to add:
 143 [22:47] <paultag> Oh shoot. Sorry for the double post :)
 144 [22:47] <pleia2> export HISTSIZE=1000
 145 [22:47] <pleia2> ^^ makes it 1000 rather than 500
 146 [22:48] <ClassBot> obengdako asked: nohup is it equivalent to command + & + disown &
 147 [22:48] <paultag> Wooo, I love this question
 148 [22:48] <paultag> YES! and it's actually slightly more "clean" since bash is not even sending a HUP signal
 149 [22:48] <paultag> BUT, that's a "bashism"
 150 [22:49] <paultag> it's not present as a binary in the system, or some sort of global flag, so know that that command will only work for bash
 151 [22:49] <paultag> I'm not sure about dash, but the shell I wrote did not have disown, so I had to use nohup ( and it works globally )
 152 [22:49] <paultag> so if you are writing for others, use nohup. If you're just playing around, disown is great
 153 [22:50] <paultag> obengdako: is that good?
 154 [22:50] <paultag> or were you looking for something else?
 155 [22:51] <ClassBot> UndiFineD asked: how to log compilation into one file ?
 156 [22:51] <ClassBot> UndiFineD asked: how to log compilation into one file ? >1&2 or something
 157 [22:51] <ClassBot> There are 10 minutes remaining in the current session.
 158 [22:51] <paultag> usually it's 2>&1
 159 [22:52] <paultag> and that takes stderr and pipes it to stdout, so that you can dump both together
 160 [22:52] <paultag> it's handy for logging errors as well as messages from the application in question
 161 [22:52] <paultag> foo > bar # will push stdout into a file
 162 [22:52] <paultag> I'm not totally clear on the question, pleia2 ?
 163 [22:53] <pleia2> that's good :)
 164 [22:53] <paultag> cool :)
 165 [22:56] <ClassBot> There are 5 minutes remaining in the current session.
 166 [22:56] <paultag> 5 minutes, take your best shot, guys!
 167 [22:58] <ClassBot> txomon asked: Is there any good guide to learn to use shell?
 168 [22:58] <paultag> tons. The best way is to use it to do casual stuff ( browsing the filesystem, renaming files, and so forth ), but again, the `clicompanion' app is fantastic for this
 169 [22:59] <paultag> it can help teach the basics. Once you have that, a bash scripting guide actually helps day-to-day bash a lot
 170 [22:59] <paultag> and keep in mind that once you're past the basics, you're learning the shell and not the GNU/Linux userland so much :)
 171 [22:59] <pleia2> http://tldp.org/LDP/abs/html/ is great (don't be scared off by "advanced"
 172 [22:59] <paultag> (stuff like disown)
 173 [22:59] <paultag> +1 pleia2
 174 [23:00] <paultag> I learned from that page, and I love it
 175 [23:00] <paultag> 22:58 < _marx_> learning the bash shell oreilly book
 176 [23:00] <paultag> not bad either, a great book for sure

UserDays/01292011/Command Line Q&A (last edited 2011-01-29 23:03:20 by alderaan)