Unix/Linux Basics

Various types of data that users create on UNIX are recorded as files , and be stored in containers that called directories . You can create a directory in a directory recursively, you can manage files hierarchically.

This file structure is called the tree structure because it looks like a tree with branches. In UNIX, the root of the tree is called the root directory and denoted by “/ (slash).” The directory that a user is working is called the current directory.

The concept the path is needed to specify a file on the tree structure. You can specify a file in two methods: absolute path and relative path.

  • Absolute path method

The method is to specify a file relative to the root directory.

Example: the case of specifying the “report1.txt” in Figure 1.

  • Relative path method

The method is to specify a file relative to the current directory.

Example: the case of specifying the “report1.txt” in Figure 1 relative to the current directory “/home/a/b59999.”




[b59999@hx001 ~]$ pwd
/home/a/b59999    # The current directory is displayed

ls [option] [filename] [directory]

Important Command Line Options

Option Effect
-l Displays details of files
-F “/“ is appended to directories and “*” is appended to executables to make file types clear in the display
-a Files whose names starts with “.”, those for system use, are also displayed.


[b59999@hx001 ~]$ ls
file1.txt file2.bmp dir1    # A filename is displayed
[b59999@hx001 ~]$ ls -a
. .cshrc .tcshrc file1.txt dir1    # Filenames starting with “.” are also displayed
.. .login .bashrc file2.bmp
[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/    # "/" is appended to a directory
[b59999@hx001 ~]$

cd [directory_name]


[b59999@hx001 ~]$ pwd
/home/a/b59999    # The current directory is /home/a/b59999
[b59999@hx001 ~]$ cd dir1    # Changing the current directory to dir1
[b59999@hx001 ~]$ pwd
/home/a/b59999/dir1    # The current directory has been changed to dir1

cp [options] [copy_source] [copy_destination]

Important command line options

Option Effect
-R If the copy source is a directory, the entire tree structure under and including the directory is copied.


[b59999@hx001 ~]$ ls -F
file1.txt dir1/
[b59999@hx001 ~]$ cp file1.txt file2.txt    # Copying file1.txt as file2.txt
[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/    # file2.txt has been created
[b59999@hx001 ~]$ cp file2.txt dir1    # Copying file2.txt to dir1
[b59999@hx001 ~]$ ls dir1
file2.txt    # file2.txt has been copied to dir1
[b59999@hx001 ~]$ cp -R dir1 dir2    # dir1 is copied to dir2
[b59999@hx001 ~]$ ls -F
file1.txt file2.txt dir1/ dir2/    # dir2 has been created
[b59999@hx001 ~]$ ls dir2
file2.txt    # dir1 has been copied to dir2 including the tree structure
[b59999@hx001 ~]$

mkdir [directory_name]


[b59999@hx001 ~]$ ls
file1.txt file2.bmp
[b59999@hx001 ~]$ mkdir dir1
[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/    # dir1 has been created
[b59999@hx001 ~]$

rm [option] [filename] [directory]

Important command line options

Option Effect
-R If the target is a directory, the entire tree structure under and including the directory is deleted.


[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/
[b59999@hx001 ~]$ rm file1.txt    # Deleting  file1.txt
[b59999@hx001 ~]$ ls -F
file2.bmp dir1/    # file1.txt has been deleted
[b59999@hx001 ~]$ ls -F dir1
dir2/    # dir2 is under dir1
[b59999@hx001 ~]$ rm -R dir1    # Deleting dir1
[b59999@hx001 ~]$ ls -F
file2.bmp    # dir1 and its tree structure are deleted

The mv command is used to move files and directories. This command can also be used to changing the filename, by moving a file to the same directory.

mv [source_filename] [new_filename]


[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/
[b59999@hx001 ~]$ mv file1.txt dir1    # Moving file1.txt to dir1
[b59999@hx001 ~]$ ls -F
file2.bmp dir1/    # file1.txt no longer exists
[b59999@hx001 ~]$ ls dir1
file1.txt    # Moved to dir1
[b59999@hx001 ~]$ mv file2.bmp file3.bmp    # Moving file2.bmp to file3.bmp
[b59999@hx001 ~]$ ls -F
file3.bmp dir1/    # The filename has been changed to file3.bmp

cat [filename]


[b59999@hx001 ~]$ ls -F
file1.txt file2.bmp dir1/
[b59999@hx001 ~]$ cat file1.txt
abcdefghijklmnopqrstuvwxyz    # The content of file1.txt is shown
[b59999@hx001 ~]$

By using the more command and the less command instead of the cat command stops and waits for the next command after displaying each page in the case of a large file.

more (or less) [filename]


[b59999@hx001 ~]$ more file3.c
#include <stdio.h>    /* The content of file3.c is shown */
#include <math.h>
#include <stdlib.h>
  for(i=0; i<10; i++){
    a[i] = b[i]*c[i];
--More--(10%)    <-- Waiting for a command after displaying one page
            Pressing the space key advances the display one page.



[b59999@hx001 ~]$ logout    # Logging out

Example: displaying the explanation of the ls command

[b59999@hx001 ~]$ man ls

To display man command messages in English, the environment variable LANG to C.

In the case of tcsh

[b59999@hx001 ~]$ setenv LANG C

In the case of bash

[b59999@hx001 ~]$ export LANG=C

A UNIX family OS includes the vi Editor as standard. The vi Editor has an unique operational architecture, we explain in detail below.

How to Start vi Editor

vi [filename]

Two Modes of vi Editor

The vi editor has two modes.

  • Command mode

You make operations other than entering characters such as searching, replacing, saving a file,
and deleting a character or a line.

  • Edit mode

you enter characters.

The vi editor starts in the command mode when it opens. To enter the edit mode, press one of the following keys: i , I , a , A , o , O . To return to the command mode from the edit mode, press the ESC key.

Moving Cursor

Character Oriented Jump ------------------------ | ------------------------------------ h, j, k, l | left, down, up, right (←, ↓, ↑, →)

Text Oriented Jump ------------------- | -------------------------------- w, W, b, B | Previous/next word
e, E | End of word
), ( | Top of previous/next sentence
}, { | Top of previous/next paragraph ]], [[ | Top of previous/next section

Line Oriented Jump ------------------- | ---------------------------------------------------- 0 (zero), $ | Beginning/end of current line
^ | First character (other than space) of current line +, – | First character of next/previous line
_n_l | n’th character of current line
H | First line on the screen
M | Center line on the screen
L | Bottom line on the screen
_n_H | n’th line from the top
_n_L | n’th line from the bottom

Screen Oriented Jump
----------------------------------------------------------------------- | -------------------------------------------------- CTRL + f, CTRL + b | Scroll to next/previous screen
CTRL + d, CTRL + u | Scroll downward/upward half screen
CTRL + e, CTRL + y | Display another line at the top/bottom of window z RETURN | Show the line of cursor at the top of screen
z. (zed dot) | Show the line of cursor at the center of screen
z- (zed hyphen) | Show the line of cursor at the bottom of screen
CTRL + l, CTRL + r | Refresh screen (no scroll)

----------- | ------------------------------------------------------------------------------------------ /pattern | Search forward for pattern
pattern | Search backward for pattern
n, N | Repeat previous search in the same/opposite direction
/, – | Repeat previous search forward/backward
fx | Jump forward from cursor position to next x on current line.
Fx | Jump backward from cursor position to next x on current line.
tx | Jump forward from cursor position to the character followed by next x on current line. Tx | Jump backward from cursor position to the character following next x on current line.
; | Repeat previous search on current line
, | Repeat previous search on current line in opposite direction

Jump by Line Number
----------------------------------- | -----------------------------
CTRL + g | Display current line number
_n_G | Jump to line number n
G | Jump to last line of file
n | Jump to line number n

Mark Position ------------- | -------------------------------------------------------------- mx | Mark current position as x
x | Jump to x
“ | Return to previous mark or to the location prior to a search `x | Jump to start of a line containing x
`` | Return to start of previous line containing the mark

Quitting Commands

Quitting Commands
----------------- | ------------------------------------------------------------------------
ZZ | Quit vi writing (saving) the file
x | Quit vi writing (saving) the file
wq | Quit vi writing (saving) the file
w | Write (save) the file
w! | (Forcibly) write (save) the file
q | Quit editing
q! | Quit editing (canceling all edit)
e! | Return to the version of current file at the time of last write (save)

Editing Commands

---------------------- | ---------------------------------------------------------- i, a | Append text before/after the cursor
I, A | Insert text at beginning/end of line
o, O (Capitalized O) | Open a new line for entering text below/above the cursor

Change -------- | -------------------------------------------------- r | Change character
cw | Change word
cc | Change current line
C | Change text from current position to end of line R | Overwrite text
s | Replace character with a text
S | Replace current line with a text

Moving and Deleting -------------------- | ------------------------------------------------------------------------------------------- x | Delete character
X | Delete the character before cursor
dw | Delete a word
dd | Delete current line
D | Delete remainder of line
p, P | Paste deleted text after/before cursor
“_n_p | Paste the text of deletion buffer number n after cursor (effective for last 9 deletion)

-------- | ------------------------------------------ yw | Yank (copy) word
yy | Yank current line
“_a_yy | Yank current line to the buffer named a p, P | Paste yanked text after/before cursor
“_a_p | Paste text of buffer “a” before cursor

Other Commands --------------- | -------------------------------------------- . (dot) | Repeat the last editing command
u, U | Undo the last edition/recover current line j | Concatenate two lines

Using ex Command
---------------- | --------------------------------------------------
d | Delete a line
m | Move a line
co, :t | Copy a line /, – | Repeat previous search forward/backward
.,$d | Delete the text from current line to end of file

Emacs is a sophisticated highly customizable text editor often used on UNIX family OS.
Editing work using Emacs consists of reading the target file to the buffer, editing the content, and writing the edited buffer to the target file. In Emacs editing work, you can switch between multiple editing files, and each buffer is assigned with a name. The name of the buffer is usually inherited from the name of the target file.

emacs [filename]

If a filename is specified, an editing buffer having the same name is created and the file content is read to the buffer. If there is no file to read (the specified file does not exist), the editing buffer becomes empty. If a filename is not specified, an editing buffer named “scratch” is created.

If Emacs is executed in an environment with X (a window system), it starts as an X client opening a new window.

If it is executed in an environment without X, the following screen shows:

The Emacs screen consists of three sections.

  • Text window

It is the largest section where input texts are displayed.

  • Mode line

It is the black and white reversal line at the bottom of the text window. it shows status of the text window.

  • Echo line

It is the line below the mode line, and it shows messages of Emacs. It is also used to input Emacs operations.

Operations such as moving cursor are done by special key input. There are two ways of special key input as below.

  • Entering a character key with the CTRL key pressed down (in this page, this is denoted as C-[character_key] ).
  • Entering a character key after pressing the ESC key and then releasing it (in this page, this is denoted as M-[character_key] ).

Opening a file ( C-x C-f )

To read a file to a buffer after opening Emacs, enter C-x C-f (enter x with CTRL pressed then enter f with CTRL pressed) on the keyboard, and you see the following message on the echo line.

Find file: ~/

If you did not start the Emacs at the home directory, the path of the current directory is shown instead of ~/. Enter the name of the file you want to edit (“test.txt” in this example) next to the message, and press the Enter key.

Find file: ~/test.txt

A buffer named test.txt is created, and the content of the file is read to the buffer. If the file does not exist, a message (New file) is shown on the echo line.

Entering Text

Characters are input at the cursor position of the text window by typing them on the keyboard. To delete character, press the Delete key, not the Backspace key.

Japanese Input System

To enter Japanese text, you need to start a Japanese input system, by entering C-(\ [backslash] with CTRL pressed). When the system is started, [あ] will be displayed in the left of the mode line, and the roman-letter-to-kana conversion will be available. To switch off the system, press C-\ again.

Typing “kyoutodaigaku”on the keyboard displays the following characters converted from the romaji.

| きょうとだいがく |

The vertical bars “|” on the both sides indicate that characters in between can be converted to kanji characters. Pressing the Space key to convert into kanji. The mark at the left corner is changed from [あ] to [漢] meaning kanji conversion.

| 京都大学 |

And then press the Space key once more, the next candidate is displayed. To determine the conversion, press the Enter key or C-l . You can also change the length of a segment by pressing C-i or C-o . When you execute kanji conversion for one of the segments, move the cursor to the target segment and press the Space key. To cancel a kanji conversion, press C-c .

Moving Cursor

How to move a cursor is as below.

col 1 col 2
C-p move up
C-n move down
C-f move right
C-b move left
C-a move to the beginning of the line
C-e move to the end of the line

Each of the commands mean: p of C-p is previous, n of C-n is next, f of C-f is forward, b of C-b is backward, a of C-a is ahead, and e of C-e is end of line.

col 1 col 2
C-v scroll the screen to the next view
M-v (press ESC, release it, and then press v) scroll the screen to the previous view
M-< move to the top of the buffer
M-> move to the bottom of the buffer

Deletion and Copying

There are several ways to delete a character.

col 1 col 2
Delete key Deletes one character to the left.
C-d Deletes one character to the right of the cursor.
C-k Deletes characters from the right of the cursor to the end of the line.

You can also delete a specified section of text. First, mark the cursor position as the base of the section by pressing C-@ . The message “Mark set” on the echo line indicates that the current cursor position has been marked.
Next, move the cursor to indicate the section that you want to delete and press C-w . This deletes the section that spans from the marked position to the current cursor position.

Texts deleted by C-k and C-w are saved in the copy buffer, you can paste the deleted texts by pressing C-y . The copy buffer is overwritten every time C-k or C-w is pressed. Note that texts deleted by C-d and the Delete key are not saved in the copy buffer.

Canceling and Undoing Operations

To cancel the operation, press C-g . When this cancel command is accepted, the echo line shows the Quit message.
To cancel the last action you performed, press C-x u . The echo line shows the message “Undo!”

Saving File ( C-x C-s )

To save a buffer you are editing, press C-x C-s . If you have been editing a scratch buffer, pressing C-x C-s shows “File to save in: ~/” on the echo line. You specify a save destination filename.

Erasing Editing Buffer

To erase an editing buffer, press C-x k . The echo line shows “Kill buffer: (default test.txt)”. Then you press the Enter key. If the editing buffer has not been edited, it is erased without any message. If the editing buffer has been edited, the echo line shows “Buffer test.txt modified; kill anyway– (yes or no).” To erase the buffer with the edited content canceled, enter “yes” and press the Enter key. To cancel the erasion of the editing buffer, enter “no” and then press the Enter key.

To finish Emacs, press C-x C-c . If there is an unsaved editing buffer, the echo line shows a message of the format as shown below. ( The following filename part is an example. )

Save file /home/a/b59999/test.txt– (y, n, !, ., q, C-r or C-h)

To save the file before finishing Emacs, enter “y” and then press the Enter key. If you do not save the file, enter “n” and press the Enter key. A message shows again on the echo line as below. If you really want to finish without saving the file, enter “yes” and then press the Enter key. If you enter “no” and then press the Enter key, the quitting process is canceled.

Modified buffers exist; exit anyway– (yes or no)

Emacs provides a tutorial. Please try it if you want to master Emacs quickly. In the tutorial, you will learn a lot of other functions that are not introduced here. The tutorial starts by pressing M-– t . The tutorial uses a buffer. Erase it by C-x k when you finish the tutorial.

Copyright © Academic Center for Computing and Media Studies, Kyoto University, All Rights Reserved.