{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LINUX Shell\n",
    "\n",
    "This time, we will learn very basic things on LINUX. You can find any good references from anywhere, and I will explain very basic things which are essential to follow the course materials.\n",
    "\n",
    "## Some Jargons\n",
    "\n",
    "* **directory**: For Windows users, the word \"directory\" may not be familiar. It is similar to \"folder\".\n",
    "* **terminal**: Decades ago, there was no \"monitor\" like today. Terminal was used as a tunnel between the computer or calculator and human (thus \"terminal\". In other words, it displays things from computer to people OR gets input from person to computer. Nowadays, terminal means a program which does similar job, but with much powerful functionalities.\n",
    "* **shell**: Shell is an application running on terminal, and it interprets what people type to the terminal. Thus it is also called \"command line interpreter\". `bash` is one of such shells. \n",
    "* **prompt**: A serial of characters which indicates that the terminal is ready to get command from you. It's `$` on Ubuntu and `>>>` for Python.\n",
    "* `ls`: \"List\"\n",
    "* `cd`: \"Change Directory\"\n",
    "* `cp`: \"Copy\"\n",
    "* `mv`: \"Move\"\n",
    "* `mkdir`: \"Make Directory\"\n",
    "* `pwd`: \"Present Working Directory\", i.e., your present location\n",
    "* `chmod`: \"Change Mode\"\n",
    "* `rm`: \"Remove\"\n",
    "* `~`: The home directory. Type `cd ~` and then `pwd` to see what `~` means.\n",
    "\n",
    "If you want, you can find manuals from [here](https://ss64.com/bash/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tutorial\n",
    "\n",
    "Now I will show you how some simple commands work.\n",
    "\n",
    "    aaa@YPB:~/AO_2017$ ls\n",
    "    00_Preface-English.ipynb  01_Installation.ipynb  02_Python_Basic.ipynb   html\n",
    "    00_Preface-Korean.ipynb   01_LINUX_Shell.ipynb   03_Get_the_Taste.ipynb  README.md\n",
    "\n",
    "By default, you may see colors for different types, e.g., gray for normal files and blue for directories. \n",
    "\n",
    "    aaa@YPB:~/AO_2017$ ll\n",
    "    total 200\n",
    "    drwxrwxr-x 5 aaa aaa  4096  3 15 03:13 ./\n",
    "    drwxrwxr-x 5 aaa aaa  4096  3 14 01:18 ../\n",
    "    -rw-rw-r-- 1 aaa aaa 22660  3 13 23:07 00_Preface-English.ipynb\n",
    "    -rw-rw-r-- 1 aaa aaa 27503  3 13 15:22 00_Preface-Korean.ipynb\n",
    "    -rw-rw-r-- 1 aaa aaa 16134  3 13 23:20 01_Installation.ipynb\n",
    "    -rw-rw-r-- 1 aaa aaa  2184  3 15 03:13 01_LINUX_Shell.ipynb\n",
    "    -rw-rw-r-- 1 aaa aaa 21730  3 13 23:48 02_Python_Basic.ipynb\n",
    "    -rw-rw-r-- 1 aaa aaa 81065  3 14 03:19 03_Get_the_Taste.ipynb\n",
    "    drwxrwxr-x 8 aaa aaa  4096  3 15 02:50 .git/\n",
    "    drwxrwxr-x 2 aaa aaa  4096  3 14 22:43 html/\n",
    "    drwxr-xr-x 2 aaa aaa  4096  3 15 02:51 .ipynb_checkpoints/\n",
    "    -rw-rw-r-- 1 aaa aaa   342  3 14 00:03 README.md\n",
    "\n",
    "Here, the directory or files start with `.` are the ones which are *hidden*. The first column means the permissions (see some manual for `chmod`). \n",
    "\n",
    "Let's make a test file:\n",
    "\n",
    "    vi sample.txt\n",
    "\n",
    "`vi` is a default editor. Hit `i` and type \"hello\". Hit `Esc`, and then colon (`:`). You will see the colon appeared at the bottom. Type \"`wq`\" and hit `Enter`. `wq` means write and quit. Then typing `ls` will show you a new file named \"`sample.txt`\".\n",
    "\n",
    "\n",
    "Now let's move it to a new directory. You can type\n",
    "\n",
    "    mkdir test         <--- make directory named \"test\"\n",
    "    cd test            <--- go to that directory\n",
    "    mv ../sample.txt . <--- move the \"sample.txt\" to here (.)\n",
    "    pwd                <--- print your current location\n",
    "    ls                 <--- show file list\n",
    "    \n",
    "You may want to rename or copy the file:\n",
    "\n",
    "    cp sample.txt ..     <--- copy sample.txt to the higher directory\n",
    "    cp sample.txt qq.txt <--- copy sample.txt to the higher directory with a new name \"qq.txt\"\n",
    "    mv sample.txt pp.txt <--- move sample.txt as pp.txt, i.e., identical to \"rename\"!    \n",
    "    \n",
    "How can we remove `pp.txt` and the directory `test`?\n",
    "\n",
    "    rm pp.txt    <--- remove pp.txt\n",
    "    cd ..        \n",
    "    rm -r test   <--- remove test directory\n",
    "    \n",
    "As you saw, to delete directory, you need an option \"`-r`\", which means \"recursive\". If you don't do so, you will get an error message \n",
    "\n",
    "    rm test\n",
    "    rm: cannot remove 'test/': Is a directory\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Little More\n",
    "\n",
    "You may frequently need a list of filenames. For example, you want to open FITS files \"abc.fits\", \"def2.fits\", ..., \"zer0.fits\", which have no clue on the naming. One way is to make a text file contains the file names, and load it from Python. Then, you can use for loop to open each file sequentially. \n",
    "\n",
    "    ls *.fits >> list.txt\n",
    "    \n",
    "Here, `*` means \"any string\", i.e., `ls *.fits` will list any file ends with `.fits`. `>>` means \"add to\", or \"append to\". That is, all the output of `ls *.fits` will be added to a file named `list.txt`. If there is no file named `ls *.fits`, it will automatically make it.\n",
    "    \n",
    "    ls *.fits > list.txt\n",
    "    \n",
    "Test what happens when you use \"`>`\".\n",
    "    \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}