--- title: 'Unix/Linux Basics' taxonomy: category: - docs --- [toc] ## File System{#system} ### File and Directory{#file} 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. ![](348_901.jpeg) 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**. ### Absolute Path and Relative Path{#path} 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. ```nohighlight /home/a/b59999/report/report1.txt ``` * **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.” ```nohighlight report/report1.txt ``` ## Basic Commands {#command} ### `pwd` – Command for Displaying Current Directory{#pwd} ```nohighlight pwd ``` **Example** ```nohighlight [b59999@hx001 ~]$ pwd ``` ```nohighlight /home/a/b59999 # The current directory is displayed ``` ### `ls` – Command for Displaying File List{#ls} ```nohighlight 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. **Example** ```nohighlight [b59999@hx001 ~]$ ls ``` ```nohighlight file1.txt file2.bmp dir1 # A filename is displayed [b59999@hx001 ~]$ ls -a ``` ```nohighlight . .cshrc .tcshrc file1.txt dir1 # Filenames starting with “.” are also displayed .. .login .bashrc file2.bmp [b59999@hx001 ~]$ ls -F ``` ```nohighlight file1.txt file2.bmp dir1/ # "/" is appended to a directory [b59999@hx001 ~]$ ``` ### `cd` – Command for Changing Current Directory{#cd} ```nohighlight cd [directory_name] ``` **Example** ```nohighlight [b59999@hx001 ~]$ pwd ``` ```nohighlight /home/a/b59999 # The current directory is /home/a/b59999 ``` ```nohighlight [b59999@hx001 ~]$ cd dir1 # Changing the current directory to dir1 [b59999@hx001 ~]$ pwd ``` ```nohighlight /home/a/b59999/dir1 # The current directory has been changed to dir1 ``` ### `cp` – Command for Copying Files and Directories{#cp} ```nohighlight 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. **Example** ```nohighlight [b59999@hx001 ~]$ ls -F file1.txt dir1/ ``` ```nohighlight [b59999@hx001 ~]$ cp file1.txt file2.txt # Copying file1.txt as file2.txt [b59999@hx001 ~]$ ls -F ``` ```nohighlight file1.txt file2.bmp dir1/ # file2.txt has been created ``` ```nohighlight [b59999@hx001 ~]$ cp file2.txt dir1 # Copying file2.txt to dir1 [b59999@hx001 ~]$ ls dir1 ``` ```nohighlight file2.txt # file2.txt has been copied to dir1 ``` ```nohighlight [b59999@hx001 ~]$ cp -R dir1 dir2 # dir1 is copied to dir2 [b59999@hx001 ~]$ ls -F ``` ```nohighlight file1.txt file2.txt dir1/ dir2/ # dir2 has been created [b59999@hx001 ~]$ ls dir2 ``` ```nohighlight file2.txt # dir1 has been copied to dir2 including the tree structure [b59999@hx001 ~]$ ``` ### `mkdir` – Command for Making New Directories{#mkdir} ```nohighlight mkdir [directory_name] ``` **Example** ```nohighlight [b59999@hx001 ~]$ ls file1.txt file2.bmp [b59999@hx001 ~]$ mkdir dir1 [b59999@hx001 ~]$ ls -F ``` ```nohighlight file1.txt file2.bmp dir1/ # dir1 has been created [b59999@hx001 ~]$ ``` ### `rm` – Command for Deleting Files and Directories{#rm} ```nohighlight 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. **Example** ```nohighlight [b59999@hx001 ~]$ ls -F file1.txt file2.bmp dir1/ ``` ```nohighlight [b59999@hx001 ~]$ rm file1.txt # Deleting file1.txt [b59999@hx001 ~]$ ls -F ``` ```nohighlight file2.bmp dir1/ # file1.txt has been deleted [b59999@hx001 ~]$ ls -F dir1 ``` ```nohighlight dir2/ # dir2 is under dir1 ``` ```nohighlight [b59999@hx001 ~]$ rm -R dir1 # Deleting dir1 [b59999@hx001 ~]$ ls -F ``` ```nohighlight file2.bmp # dir1 and its tree structure are deleted ``` ### `mv` – Command for Moving Files (for Changing Filenames){#mv} 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. ```nohighlight mv [source_filename] [new_filename] ``` **Example** ```nohighlight [b59999@hx001 ~]$ ls -F file1.txt file2.bmp dir1/ ``` ```nohighlight [b59999@hx001 ~]$ mv file1.txt dir1 # Moving file1.txt to dir1 [b59999@hx001 ~]$ ls -F ``` ```nohighlight file2.bmp dir1/ # file1.txt no longer exists [b59999@hx001 ~]$ ls dir1 ``` ```nohighlight file1.txt # Moved to dir1 ``` ```nohighlight [b59999@hx001 ~]$ mv file2.bmp file3.bmp # Moving file2.bmp to file3.bmp [b59999@hx001 ~]$ ls -F ``` ```nohighlight file3.bmp dir1/ # The filename has been changed to file3.bmp ``` ### `cat` – Command for Displaying File Contents on Screen{#cat} ```nohighlight cat [filename] ``` **Example** ```nohighlight [b59999@hx001 ~]$ ls -F file1.txt file2.bmp dir1/ [b59999@hx001 ~]$ cat file1.txt ``` ```nohighlight abcdefghijklmnopqrstuvwxyz # The content of file1.txt is shown [b59999@hx001 ~]$ ``` ### `more` and `less` – Commands for Displaying File Contents{#less} 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. ```nohighlight more (or less) [filename] ``` **Example** ```nohighlight [b59999@hx001 ~]$ more file3.c #include <stdio.h> /* The content of file3.c is shown */ #include <math.h> #include <stdlib.h> (…omission...) 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. ``` ### `logout` – Command for Logout{#logout} ```nohighlight logout ``` **Example** ```nohighlight [b59999@hx001 ~]$ logout # Logging out ``` ### `man` – Command for Viewing Manual of Commands{#man} **Example: displaying the explanation of the ls command** ```nohighlight [b59999@hx001 ~]$ man ls ``` To display man command messages in English, the environment variable LANG to C. **In the case of tcsh** ```nohighlight [b59999@hx001 ~]$ setenv LANG C ``` **In the case of bash** ```nohighlight [b59999@hx001 ~]$ export LANG=C ``` ## How to Use vi Editor{#vi} 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** ```nohighlight 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. ### vi Command Reference{#vi} **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) Search ----------- | ------------------------------------------------------------------------------------------ /_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 f_x_ | Jump forward from cursor position to next _x_ on current line. F_x_ | Jump backward from cursor position to next _x_ on current line. t_x_ | Jump forward from cursor position to the character followed by next _x_ on current line. T_x_ | 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 ------------- | -------------------------------------------------------------- m_x_ | 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** Inserting ---------------------- | ---------------------------------------------------------- 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) Yank -------- | ------------------------------------------ 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 ## How to Use Emacs{#emacs} ### What is Emacs– {#emacs_top} **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. ### Starting Emacs{#emacs_boot} ```nohighlight 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. ### Emacs Screen {#emacs_display} If **Emacs** is executed in an environment with X (a window system), it starts as an X client opening a new window. ![](348_902.jpeg) If it is executed in an environment without X, the following screen shows: ![](348_903.jpeg) 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. ![](348_904.jpeg) * **Echo line** It is the line below the mode line, and it shows messages of **Emacs**. It is also used to input **Emacs** operations. ### Basic Operations of Emacs{#emacs_use} 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]** ). **<ins>Opening a file ( <code>C-x</code> <code>C-f</code> )</ins>** 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. ```nohighlight 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. ```nohighlight 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. ![](348_905.jpeg) **<ins>Entering Text</ins>** 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. **<ins>Japanese Input System</ins>** 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. ```nohighlight | きょうとだいがく | ``` 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. ```nohighlight | 京都大学 | ``` 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` . **<ins>Moving Cursor</ins>** 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 ![](348_907.jpeg) **<ins>Deletion and Copying</ins>** 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. **<ins>Canceling and Undoing Operations</ins>** 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!” **<ins>Saving File ( <code>C-x</code> <code>C-s</code> )</ins>** 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. **<ins>Erasing Editing Buffer</ins>** 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. ### Finishing Emacs {#emacs_end} 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. ) ```nohighlight 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. ```nohighlight Modified buffers exist; exit anyway– (yes or no) ``` ### To Master Emacs{#emacs_master} **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.