01-Programmer's Survival Guide For Mac and Ubuntu - Terminal, File System, Users and Editors
01-Programmer's Survival Guide For Mac and Ubuntu - Terminal, File System, Users and Editors
1 of 7
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
Programmer's Survival
Guide for Mac & Ubuntu
Terminal, File System, Users
and Editors
1. Command-line "Terminal"
2. Unix File System
3. Basic Commands
3.1 pwd (Print Current Working Directory)
3.2 cd (Change Working Directory)
3.3 ls (List Directory's Contents)
3.4 cat, less (Viewing File Contents)
3.5 Shortcut Keys - IMPORTANT
3.6 Other Frequently-used Commands
3.7 Getting Helps
1. Command-line "Terminal"
5.2 Display Text Files in Command-line Programmers uses command-line to enter commands to the operating system,
5.3 Console-based Text Editors - nano
instead of graphical user interface (GUI). This is because command-line is much
more flexible than graphical interface, e.g., you can provide additional options to a
command; you can pipe the output of one command into another command; you can put a set of commands in a script to
automate a task.
A shell (or command interpreter) is a program that lets you issue commands to interact with the operating system, via a
text-based interface. It provides a set of commands and utilities. It also has its own programming language for writing shell
scripts. There are many shell programs available, from the legacy and obsoleted sh (Bourne Shell), csh (C Shell), ksh (Korn
Shell), to newer bash (Bourne Again Shell), tcsh (Tenex C Shell).
Terminal is an application that runs a shell program. By default, the Terminal in Ubuntu and Mac OS X runs the bash shell,
although you are free to switch into another shell program, such as tcsh.
To launch a Terminal:
In Mac OS X: Open "Finder" Go Utilities Select "Terminal". Drag the "Terminal" to your dock since you need to use it
frequently.
In Ubuntu: Open "Dash" type "Terminal"; or choose "Applications" lens Installed Select "Terminal". Drag the
"Terminal" to your Launcher since you need to use it frequently.
A Terminal displays a command prompt ending with "$" sign, in the form of:
In Mac OS X: "ComputerName:CurrentDirectory Username$"
In Linux: "Username@ComputerName:CurrentDirectory$"
You can enter commands after the command prompt. For example, enter "pwd" to print the current working directory:
$ pwd
.......
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
2 of 7
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
Home Director y
Unix is a multi-user operating system (although most of you, in particular the Mac users, use it as a single-user personal
computer). Each user on the system is allocated a directory for storing his files, known as home directory. The users' home
directories are allocated under /home for Ubuntu, or /Users for Mac OS X, with a sub-directory name the same as the
username (or login ID). The home directory of the current login user is denoted as "~". It contains sub-directories such as
~/Desktop, ~/Downloads, ~/Documents, ~/Movies, ~/Music, and etc.
3. Basic Commands
3.1 pwd (Print Current Working Director y)
The Terminal session maintains a so-called current working directory. All relative pathnames/filenames are relative to the
current working directory. To display the current directory, issue command "pwd" (print working directory):
// Print Current Working Directory
$ pwd
......
When a Terminal is launched, it sets the initial working directory to the home directory of the current login user (denoted as
"~").
The current working directory is often included as part of the command prompt.
cd
cd
cd
cd
/
/usr/local
mysql
myproject/bin
//
//
//
//
Change
Change
Change
Change
directory
directory
directory
directory
(absolute)
(absolute)
(relative)
(relative)
to
to
to
to
the root
"/usr/local"
mysql of the current directory
myproject/bin of the current directory
You can cd in multiple stages (e.g., one cd for each sub-directory - recommended), or cd in a single stage with the full
pathname.
$ cd /
$ cd usr
// "/"
// "/usr"
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
3 of 7
$ cd local
$ cd mysql
$ cd bin
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
// "/usr/local"
// "/usr/local/mysql"
// "/usr/local/mysql/bin"
// Same As
$ cd /usr/local/mysql/bin
You can use "/" to denote the root; "~" to refer to the home directory of the current login user; ".." (double-dot) to refer to
the parent directory; "." (single-dot) to refer to the current directory; and "-" (dash) to refer to the previous directory. For
example,
$
$
$
$
$
cd
cd
cd
cd
cd
~
~/Documents
..
-
//
//
//
//
//
Setting proper working directory can greatly simplify your work. For example, to compile a Java program called "Hello.java"
in "~/myproject/java":
1. Set the working directory to "~/myproject/java", and reference the file with filename only (without the path):
$ cd ~/myproject/java
$ javac Hello.java
2. You can also refer to a file with its full pathname in any working directory:
// Any working directory
$ javac ~/myproject/java/Hello.java
Although setting working directory is NOT really a necessity, it can be very messy at times without the correct working
directory (e.g., the output file "Hello.class" is created in the current directory). You should always set a proper working
directory before issuing any command with file references.
long format
2
2
2
1
myuser
myuser
myuser
myuser
myuser
myuser
myuser
myuser
1024
1024
1024
8445
Mar
Mar
Mar
Mar
22
22
22
22
21:32
21:32
21:32
17:30
Desktop
Documents
Downloads
examples.desktop
Wildcard *
You can list selected files using wildcard *, which matches 0 or more (any) characters. For example,
$ ls *.java
$ ls -l *.java
$ ls -ld my*
You could, of course, view the contents of a directory using a File Manager (such as "Finder" in Mac, or "Home Folder" in
Ubuntu) more conveniently.
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
4 of 7
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
Auto-Complete: You can type the first few characters for the pathname or filename, and press TAB key to auto-complete.
Copy/Paste:
In Mac OS X: use Cmd+C and Cmd+V.
In Ubuntu: use Shift+Ctrl+C and Shift+Ctrl+V. You can also use middle mouse-button to copy/paste the highlighted
text on the command-line. (The Ctrl+C is used as interrupt signal; Ctrl+D for end of text input; Ctrl+Z for suspending
current process.)
clear
who
whoami
which program-name
//
//
//
//
Clear Screen
List all login users
Show the current login user
Show the location of the program, e.g., "which javac"
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
5 of 7
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
$ sudo cat /etc/shadow
[sudo] password for myuser:
......
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
4.2 Processes
Unix is a multi-process, multi-user operating system. It can run many processes concurrently.
You can use GUI applications to view all running processes and terminate a particular process (similar to "Task Manager" in
Windows).
In Mac OS X: launch "Activity Monitor" (Under /Applications/Utilities) and select "All Processes".
In Ubuntu: launch "System Monitor" (search from Dash) and select "Processes".
On command-line, you can use command ps to list all the processes and kill to terminate a particular process:
$
$
$
$
$
$
ps
// Print processes of current user
ps -e
// Print all processes
ps -ef
// Print all processes in full-listing
ps -ef | grep "mysql" // Print processes containing mysql
ps aux | grep "mysql" // same as above
top
// Display resource usage and top processes
$ kill pid
$ kill -9 pid
5.1 Graphical Programming Text Editors - gedit, jedit, sublime and others
A good graphical programming text editor, which provides syntax highlighting, is crucial in programming and development.
In Ubuntu, the default "gedit" is an excellent programming text editor.
In Mac OS X, you can use the default "TextEdit" to create plain text file by choosing the option "Make Plain Text" (under
"Format"). However, TextEdit is NOT a programming text editor and does not provide syntax highlighting. I strongly
strongly suggest that you install gedit, jedit, sublime or other programming text editors.
Other popular graphical editors include kate (KDE text editor) and kedit (another KDE editor).
5.2 Display Text Files in Command-line - cat, less, head and tail
If possible, use a graphical text editor to display a text file. Nonetheless, you can use the following commands to display a text
file in command-line:
cat filename: Concatenate file or print the file to console.
less filename: Display the file in pages. You can scroll the display with Up, Down, PageUp, PageDown keys. less
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
6 of 7
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
More on cat
The command cat has many usages. You can use cat to concatenate (join) a few files. For example,
$ cat file1 file2 file3
// display file1, file2 and file3 on the console
$ cat file1 file2 file3 > file4
// Concatenate file1, file2 and file3 as file4
You can also use cat command to create a small text file. cat without argument uses standard input as its input. You could use
Ctrl+D to signal EOF. For example:
$ cat > out.txt
The quick brown fox jumps over the lazy dog[Ctrl+D]
$ cat out.txt
The quick brown fox jumps over the lazy dog
nano
nano is small, simple and easy-to-use text editor, that is suitable for creating/editing small files. nano is a GNU text editor
available for Mac and Unix Systems. nano replaces its predecessor called pico.
To start nano, open a Terminal and issue:
$ nano
$ nano filename
To exist nano, press Ctrl+X, followed by "y" (yes) to save the file.
You can run nano with sudo for editing restricted system files, as follows:
$ sudo nano filename
07/08/2015 1:51 PM
Programmer's Survival Guide for Mac and Ubuntu - Terminal, File Sy...
7 of 7
http://www.ntu.edu.sg/home/ehchua/programming/howto/Unix_Survi...
emacs
The comprehensive GNU emacs editing environment.
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan ([email protected]) | HOME
07/08/2015 1:51 PM