---
# System prepended metadata

title: NTU BIME LAB304 LINUX TUTORIAL
tags: [NTUToolmenLab]

---

# NTU BIME LAB304 LINUX TUTORIAL
###### tags: `NTUToolmenLab`

github https://github.com/linnil1/2018_LAB_Tutorial

## Go through Folders
* `pwd`
show where you are
* `cd [name]`
go into folder `name`
`cd name1/name2`
`cd ..` go to parent folder
`cd ~` go to home folder
`cd /` go to root folder

* `ls [dir='.']`
list all things in dirctionary 
`ls -al` show all information in dirictionary

## Modify folder
* `mkdir [name]`
Create new folder
`mkdir -p xxx` create with no error

* `rm [name]`
remove it
`rm a b`
`rm -r [name]` remove recursively

* `cp [name] [newname]`
`cp a b/c`
`cp a b/` These will copy `a` to `b/a`

* `mv [name][newname]`
`cp a b/c`
`cp a b/` These will move `a` to `b/a`
`cp -r a b/` These will move `a` to `b/a` where `a` is dictionary

## Run script

### make it executable
Use `ls -al` to get permission
```
$ ls -al
total 16
drwxr-xr-x 2 linnil1 linnil1 4096 Jul 15 15:27 .
drwxr-xr-x 5 linnil1 linnil1 4096 Jul 15 13:21 ..
-rwxr-xr-x 1 linnil1 linnil1   34 Jul 15 13:23 test
-rw-r--r-- 1 linnil1 linnil1   34 Jul 15 13:23 test.sh
```

https://help.ubuntu.com/community/FilePermissions

`chmod` used for changing permission

`chmod +x test.sh` make `test.sh` executable
(If you know that I can be run on shell)

then run it by `./hello.sh`

### stop it

run it
```
cd L2/
chmod +x hello.sh
./hello.sh
```
It will stop by itself.

```
chmod +x helloCont.sh
./helloCont.sh
```
This will continuous run.
In linux, use `ctrl+c` to stop

```
chmod +x helloDontStop.sh
./helloDontStop.sh
```

`ctrl+c` is ask programt to stop
`ctrl+\` will terminate it forcely

### pipe to file
use `cat [name]` to show the data in file.

`./helloCont.sh > log`
Overwite `log` file with output data

`./helloCont.sh >> log`
Append output data to `log` file

In unix system, 
`1` is stdout (default)
`2` is stderr
`./helloCont.sh 1> logstd 2> logerr`

output to same file
`./helloCont.sh > log 2>&1`

### show part
`cat [name] | more`
use page to show

`cat [name] | head -n x`
show x  lines

`cat [name] | tail -n x`
show x lines from bottom

### more things

pipe to trash(null)
`./helloCont.sh > /dev/null`

## vim
Vim has:
* Normal mode
    For navigation.
    To replace mouse click.
    You can see more detail on CheatSheet
    ![](https://c1.staticflickr.com/3/2598/4178373148_b39a3131ff_b.jpg)
* Edit mode
    For edit text
* Visual mode
    
### Practice
`cd L3`
`vim hello.py`

Enter `7Gf-2li[Backspace]>[ESC]`

![](https://i.imgur.com/Otv8VzI.png)


## git 

* `git init`
init git repo

* `git clone`
download repo from xx

* `git pull`
download repo
and it will do somethings for you

* `git push`
Upload repo

* `git status`
Check the modification of repo

* `git diff`
Difference of repo
![](https://i.imgur.com/Hkfa1oK.png)

* `git add [name]`
comfirmed the difference of repo

* `git commit`
comfirmed the modification of repo

* `git log`
Log of commit

* `git checkout xx`
return to xx commit status

## Screen
screen
A methods that you can perserve your session
and it can open more session for you.

`screen` start session
`[Ctrl + a] + c` create session
`[Ctrl + a] + 0` Jump to window 0
`[Ctrl + a] + d` dettach
`screen -a` continue last session

Cheat Sheet
![](https://i.imgur.com/G1yVFW3.png)
https://www.cheatography.com/gissehel/cheat-sheets/screen/

## ssh
use to login
`ssh -p [port] [name]@[domain.name]`
like
`ssh -p 22 test@test.com.tw`

## scp
put file to remote
`$ scp –P [port] [localpath] [name]@[domain.name]:[path]`
put folder to remote
`$ scp –P [port] -r [localpath] [name]@[domain.name]:[path]`
get file from remote
`$ scp –P [port] [name]@[domain.name]:[path] [localpath]`
get folder from remote
`$ scp –P [port] -r [name]@[domain.name]:[path] [localpath]`

## gpu
`$ CUDA_VISIBLE_DEVICES=0 python3 train.py`
To control use first gpu in this program.


## Advance script
* `echo`
get environment variable
`echo 123`
`echo $PATH`

* `htop`
'htop' to monitor processes.
You can stop it by choosing it and `k` to kill.

* `grep` find pattern
`$ grep "hello" hello.py –nH`
`$ cat hello.py | grep “hello” –nH`

* `&&` `||`
run two more script by some condition
  * `$ A && b `
Run b if A command is successed.
`$ cat hello.py && cat hello.py`
`$ cat hell.py && cat hello.py`
  * `$ A || b `
Run b only if A command is failed
`$ cat hello.py || cat hello.py`
`$ cat hell.py || cat hello.py`

* shell script
Put all your entered in command line into a file.
see `L3/hellomore.sh`

* `curl`
  * Download
`curl –O https://raw.githubusercontent.com/linnil1/2018_LAB_Tutorial/master/L3/hello.py `
  * Run script by URL
`curl –s https://raw.githubusercontent.com/linnil1/2018_LAB_Tutorial/master/L3/hello.sh | bash `
  * But this may be harmful
`curl –s https://raw.githubusercontent.com/linnil1/2018_LAB_Tutorial/master/L4/hack.sh | bash`


* Extract archieve
In linux, there have three type of archive file.
  * `*.zip`
`$ unzip z2.zip`
  * `*.tar`
`$ tar xf z1.tar`
  * `*.tar.gz`
`$ tar xzf z3.tar.gz`
