Other UNIX commands1
To copy a file: use the
cpcommand.2For example, to copy the file
CreateSubdirectories.Cfrom the directory~seligman/root-classto your current working directory, type:> cp ~seligman/root-class/CreateSubdirectories.C $PWD
In UNIX,
$PWDmeans the results of thepwdcommand.3
To look at the contents of a text file: Use the less command.4
This command is handy if you want to quickly look at a file without editing it. To browse the contents of file
CreateSubdirectories.C, type:> less CreateSubdirectories.C
While
lessis running, type a space to go forward one screen, typebto go backward one screen, typeqto quit, and typehfor a complete list of commands you can use.
To get help on any UNIX command: type
man <command-name>While
manis running, you can use the same navigation commands asless. For example, to learn about thelesscommand, type:> man less
To delete a file: Use the rm command.5 For example:
> rm CreateSubdirectories.C
To edit a file: I suggest you use
emacs.6 For example, to edit the file CreateSubdirectories.C:> emacs CreateSubdirectories.C
This may appear to “lock up” your UNIX window. If this is an issue, either create a new UNIX window or learn about ampersands at the end of a command line.
The
emacsenvironment is complex, and you can spend a lifetime learning it.7 For now, just use the mouse to move the cursor and look at the menus. When you get the chance, I suggest you take theemacstutorial by selecting it under the Help menu.8
Tip
Are you quitting emacs after you change a file, only to start up the editor again a moment later? Hint: look at . If you’re editing many files, try opening them all with and switch between them using . Remember to use once in a while.
Learn how to cut and paste in whatever editor you use. If you don’t, you’ll waste a lot of time typing the same things over and over again.
Figure 3: http://xkcd.com/378 by Randall Munroe.
If you’re feeling bored, type Meta-x butterfly in emacs and see what
happens.
- 1
A colleague of mine once said, “The commands in UNIX are the initials of the people that the designers went to high school with.”
A good story, but it’s not true. The reality is that when UNIX was first developed in the 1960s, computers had six orders of magnitude less memory than they do today. The space for internal tables containing items like command names was at a premium. From then to now, UNIX command names tend to be terse.
- 2
Over the years, as students began to use laptops, I noticed that many of them had an interesting misconception: The
cpcommand copies a file from one place to another on the same computer. It does not copy a file from a remote server to your laptop! For that you usescp; useman scpto learn more. Note that if you’re working on a remote server such as the ones at Nevis, there’s no reason to usescpfor this tutorial.- 3
A period (
.) is the usual abbreviation in UNIX for “the current directory” (did you remember that..means “the directory above this one”?) but many students missed the period the first time I taught this class.- 4
If the name is confusing: the less command was created as a more powerful version of the more command.
- 5
Super-duper warning: There is no Trash folder or “undo” operation in command-line UNIX! If you use rm on a file and later realize that was a mistake, there’s pretty much nothing you can do except re-create the file from scratch.
Be warned: A notorious UNIX prank is for someone to get you to type
rm -rf ~which performs an unrecoverable delete of all of your files without any warning. Don’t fall for it!- 6
If you’re already familiar with another text-based UNIX editor, such as
nanoorvim, you can use it instead.If you’re not using a remote UNIX server and you’re editing files on your laptop, make sure you’re using a plain-text editor. If you use an editor whose default mode is to not save files in plain text (Microsoft Word is one example; the poorly-named TextEdit on the Mac is another) you’re going to get confused. If you don’t already have such an editor, I suggest Notepad++ on MS-Windows and the free version of BBEdit for the Mac.
What do I mean by “you’re going to get confused” in the previous paragraph? When I taught Python to my father, he tried to use Microsoft Word to edit code. He didn’t understand why you couldn’t apply a bold or italic font style to text in a program. He also often forgot to use
Save As...and switch the file format toPlain Text, so his code was saved as a Word document.Of course, you’re not my father; otherwise I’d ask you if I could stay out late this Saturday. You may find Word is the best plain-text editor for you.
- 7
I’ve spent two of your lifetimes already, and the class has just started!
- 8
As a final aside: There’s lots of debate over which is the best UNIX editor:
emacs,vim, ornano. I’m aware thatemacsis now the minority choice. I recommendemacsbecause it’s the one I always use, for two reasons:As I discuss on the next web page, the keyboard commands are useful on the UNIX command line.
Emacs has a large number of modes that make it easier to edit code. This includes syntax highlighting and auto-indentation.
If you’re used to Python, the notion of auto-indenting will seem trivial to you: You have to indent your code anyway. For a language like C++, emacs can automatically indent code to make it more readable. This helps a lot when someone sends you a code file that’s an unreadable mish-mash.
These modes are language-dependent; e.g., the syntax highlighting and indentation are different when you’re editing a C++ file, a shell script, or a web file. To me, this more than makes up for any perceived deficiencies between emacs and vim.