Using the Linux Command Line¶
Most of what we will do in this course can easily be done with a command- line window, sometimes called a shell window. The command-line window lets us type commands to run programs.
By default, command-line windows in Ubuntu uses the BASH command-line, and when you first launch it you will see a prompt something like this:
tjd@tjd-ubuntu-desktop:~/cmpt383$
This prompt has a lot of parts:
tjd@tjd-ubuntu-desktop
is the user name and computer name. This is useful when you are using multiple computers or multiple accounts.~/cmpt383
is the current working directory, i.e. the directory that the command-line will look in first when you run a program. The~
is shorthand for you personal directory.$
marks the end of the prompt and the beginning of where the user types commands. In these notes we will usually denote the shell prompt using just a$
.
Note
Command-line prompts are highly customizable (e.g. see this posting as a start). You a can configure your prompts to provide lots of useful information.
Some Useful Shell Commands¶
The shell has dozens and dozens of commands, although to get started you only need to know a few simple ones. Here is a brief summary of the commands we’ll discuss here:
Command Summary pwd
returns present working directory ls
lists files and folders of pwd cd
change the pwd rm
delete a file cp
copy a file man
print a manual page less
print contents of a file (paged) cat
print file contents (unpaged)
pwd
prints the present working directory, e.g.:$ pwd /home/tjd/cmpt383
The present working directory is also sometimes called the current working directory.
ls
andls -l
list all the files and folders in the present working directory, e.g.:$ ls a1/ readme.txt $ ls -l total 8.0K drwxr-xr-x 2 tjd tjd 4.0K 2011-12-01 16:15 a1/ -rw-r--r-- 1 tjd tjd 30 2011-12-01 16:15 readme.txt
In this example,
a1
is a folder, andreadme.txt
is a file.cd
changes the current working directory, e.g.:$ ls a1/ readme.txt $ cd a1 $ pwd /home/tjd/cmpt383/a1 $ cd $ pwd /home/tjd
Note that
cd
on its own returns you to your home directory.cd ..
changes the current working directory to be the parent of the directory the shell is in now. Runningcd ..
multiple times in a row is an easy way to “walk up” a folder structure to some top-level folder.rm
deletes a file. For example, supposetemp.txt
is in the current directory. Then this command deletes it forever:$ rm temp.txt
Beware that
rm
truly deletes the file: it does not put the file in the desktop trash!cp
makes a copy of a file. For example, supposea1q1.cpp
is in the current directory. Then this command makes a copy of it calleda1q1.cpp.old
:$ cp a1q1.cpp a1q1.cpp.old
man
prints a help page (called a manual page, or man page for short) for a command. For example,ls
many options:$ man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and ..
This only shows the first few lines of the
ls
man page:ls
has lots of options!Note that for some commands
man
is not helpful, e.g.:$ man cd No manual entry for cd
Commands like
cd
are part of the BASH shell language, and there are no man pages for shell commands. However, if you typeman bash
, then you will get (a very long) man page documenting all the BASH commands (includingcd
).less
displays the contents of a text file, pausing at each page:$ less helloworld.cpp // helloworld.cpp #include <iostream> using namespace std; int main() { cout << "Hello, world!\n"; }
If the file being displayed doesn’t fit on the screen, then a single page of the file is displayed at a time. Press:
- spacebar to go to the next page
- b to go the previous page
- q to quit
less
immediately - /banana to search for the string
banana
cat
prints the contents of a file to the terminal without pausing at pages the wayless
does. It is a simpler command that is useful for small files, or for use with other commands. Here’s an example:$ cat helloworld.cpp // helloworld.cpp #include <iostream> using namespace std; int main() { cout << "Hello, world!\n"; }
Note
You can also manipulate files and folders interactively in the GUI. Just open the folder you want to change, and use the typical drag- and-drop actions you are probably already familiar with.
In practice, it is sometimes easier to use the GUI instead of the command- line. For example, copying a folder it is easier in a GUI.
Sometimes the command-line is best. For example, if you want to delete all
the files ending with .bak
, then you can type this:
$ rm *.bak
The *
is a special meta-character that matches any string of
characters, and so *.bak
matches all file names ending with .bak
.
Editing Program Files¶
Ubuntu has a number of text editors you might want to try:
Sublime Text 3 is a popular modern editor known for its speed, ease-of-use, and extendability. It runs on Windows, Mac, and Linux systems. Technically it is not free, however you can freely evaluate the complete editor for as long as you like. You can get it from here.
Visual Studio Code is another popular modern text editor that is fast, free, extendable, and runs on many plateforms. You can download it from here.
emacs is an old but still popular text editor designed for programmers. It shoudl be installed by default, and can be run like this:
$ emacs hello.cpp
Note
If emacs is not on your Ubuntu system, you can install it by typing this command:
$ sudo apt-get install emacs
Out of the box, emacs has excellent support for many programming languages, including syntax-coloring, bracket-matching, and GDB debugger integration. It also has special modes for writing makefiles, editing plain text, and so on. It can even do things like read your email, browse the web, and play games.
Emacs extremely customizable, with hundreds and hundreds of add-ons available for it.
vim (or vi) is another old but still extremely popular programming editor:
$ vi hello.cpp
Like emacs, vim has good support for programming out of the box, and it also extremely customizable with lots of add-ons already written.
vim uses a modal style of editing which is different than emacs and other text editors. This takes some getting used to since you can’t just start typing in vim: you need to first select a text-entering mode. Once you get used to it and learn a few commands, vim may be the fastest editor around. It loads very quickly (in contrast to emacs and IDEs) and experienced vim users can do amazing things in just a few keystrokes.
Note
Vim and emacs users often have “religious wars” about which editor is better. Ultimately, both are fine: use what you prefer. Some programmers use both, e.g. vim for quick edits to small files, and emacs for larger files or sets of files.
gedit is a relatively simple text editor that works much like Notepad on windows. It should be installed by default, and you can use like this:
$ gedit hello.cpp
While you can certainly use gedit to write programs, it doesn’t appear to have all the useful features common in editors designed specifically for programming.