1. Why the Command Line? 2 1 Why the Command Line? • Isn’t that going back to the dark ages? • Don’t file managers make all that obsolete? Why the Command Line? GNU and Linux commands About my favourite file manager Linux for Teachers • Doesn’t it take so much longer to type all those crazy commands? • “Aren’t you just telling us all this crap to waste our precious time we could spend learning something useful?” • I’ll tell you a secret Nick Urbanik This document Licensed under GPL—see section 9 5, 6 December 2005 My Dirty Secret Outline • You may be better with a file manager than I am • you may have had more practice than I have. . . Contents 1 Why the Command Line? 1.1 Advantages of the Command Line . . . . . . . . . . . . . . . . . . . . . . Comparing GNU commands with DOS Isn’t it too slow? 3.1 Doing it even faster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How can I get help with these commands? What’s the GNU, anyway? Some Things to Do Being the System Administrator 7.1 Getting more software . . . . . . . . . . . . . . . . . . . . . . . . . . . . Some Things to Read License Of This Document 2 2 2 3 4 4 5 6 6 6 7 7 • . . . because my favourite file manager is the command line – . . . keeps umbrella handy to protect against the expected shower of rotten tomatoes • Still here? Hmm, okay, I’ll tell you why. 2 3 1.1 Advantages of the Command Line Advantages of the Command Line • Allows you to automate things • Provides quick ways of getting at things similar to what you did before 4 5 6 7 • quicker than pulling down menus 2 Comparing GNU commands with DOS • You are nearly as old as I am, • so many of you are familiar with DOS commands. • If you were just out of school, this comparison would be useless 8 9 Comparing GNU commands with DOS 3. Isn’t it too slow? GNU ↔ DOS Action list files (short) list files (long) copy files move files rename files GNU ↔ DOS Action change directory make directory remove directory show current directory show content of text file GNU cd dirname mkdir dirname ... rmdir dirname ... pwd cat file DOS cd dirname md dirname rd dirname cd type file GNU ls ls -l cp sourcefile ... target mv sourcefile ... target mv sourcefile target DOS dir /s dir copy sourcefile move sourcefile ren sourcefile 3 3.1 Doing it even faster • Reverse search through history 4 ¤ § Control-R ¥ repeatedly – type any characters from a previous command and press ¦ until you see the command you want target target target 3.1 Doing it even faster Doing it even faster with loops • command line editing, tab-completion and history all make the command line fast. . . • . . . but the real speed comes with automation • We can automate things with loops • Here are the last few things I did with loops: • list PDF documents I wrote, with the number of pages: $ for i in *trans.pdf;do o="$(pdfinfo $i|egrep ’^Pages:|^Author:|^Title:’)";if echo $o| egrep -q ’Author: +Nick’;then echo $i:;echo "$o";fi;done ← • Set timestamp correctly in family photos: $ for i in 2005_09_11 2005_09_12 2005_09_13 2005_09_14 2005_09_15 2005_09_16 2005_09_17 2005_09_18;do pushd $i;pwd;exif-timestamp-adjust.pl *.jpg;popd;done ← • View all my teaching handouts: $ for i in *slides-beamer-handout.pdf;do xpdf $i&done ← • Pretty-print my C++ programs: $ for i in *.cpp;do pretty-print-cpp $i| lpr;done ← • Make a hundreds table for my son: $ for ((i=1;i<=100;++i));do echo -n "$i "; if ((i % 10 == 0));then echo ’’;else echo -n ’& ’;fi;done > hundreds-table.tex ← 3 Isn’t it too slow? • Bash shell provides command-line editing § ¤ §¤¤¤ §§ Home ¥ End ¥ ← ¥ → ¥ ,¦ ,¦ ,¦ – Move: ¦ § ¤¤ §§¤ § ¤ – Delete words: ¦ ¥ ¥ Esc ¥ Esc ¦ , ¦ D Backspace ¦ ¥ ¤ § Control-k ¥ – Delete to end of line: ¦ • tab completion §¤ – type the first few characters of a command, file, . . . and press the ¦ ¥ Tab key: the shell will complete the name • A history of previous commands § §¤ ¤ – press ↑ , ↓ ¦¥ ¥ ¦ Doing it fast 4 How can I get help with these commands? • A few commands are built into the bash shell; you can get help for these by typing $ help ← How can I get help with these commands? 5. What’s the GNU, anyway? • The other commands are in /bin, /usr/bin • you can go there and have a look • each one has a “man page” • To read the page for ls, you can do $ man ls ← • To search for word in the man page, type / word §¤ • To quit, type: q ¦¥ But where is the woman command? Isn’t this sexist? • “man” is short for “manual” 5 6. Some Things to Do 6 6 Some Things to Do • Write a for loop to count up to 100: $ for ((i = 0; i <= 100; ++i));do echo $i;done ← • See all the man pages for programs in /bin: $ cd /bin; for i in *;do man $i;done ← • Do the same thing for the commands in /usr/bin ¤ § Control-C ¥ interrupt a loop that gets stuck going “forever” will • Note: pressing ¦ Some things to do 7 Being the System Administrator • You write to anything under your home directory, or in /tmp with your own account • . . . but to write anywhere else, you need to be the system administrator and have root access. • I highly recommend that you use sudo to become the system adminstrator. • To do that you can follow the separate guide available at http://nicku.org/ lpic102/lpic/general-linux-2/lab/sudo/sudo.pdf 5 What’s the GNU, anyway? What’s the GNU, anyway? Being the system administrator 7.1 Getting more software Getting more software • You can use yum to install new software • You can become root by either: • GNU stands for “GNU’s Not Unix” • It’s the project started by Richard Stallman • Aims to provide all we need without contaminating our computers with non-free software • Enables membership of the Church of Emacs. – using sudo  – . . . or by typing $ su - ← and then entering the root password  • Then do: $ sudo yum -y update ← or # yum -y update ← • To install Blender, do: $ sudo yum -y install blender ← • To install Scribus, do: $ sudo yum -y install scribus ← 8. Some Things to Read 7 8 Some Things to Read References [1] Dr. Peter Salus. The Daemon, the GNU & the Penguin. Grocklaw 2005. http://www. groklaw.net/staticpages/index.php?page=20051013231901859. [2] GNU General Public License. http://www.gnu.org/copyleft/gpl.html 9 License Of This Document License Of This Document Copyright © 2005 Nick Urbanik You can redistribute modified or unmodified copies of this document provided that this copyright notice and this permission notice are preserved on all copies under the terms of the GNU General Public License as published by the Free Software Foundation—either version 2 of the License or (at your option) any later version.