---
# System prepended metadata

title: Some useful terminal commands
tags: [linux, terminal, shared]

---

# Some useful terminal commands
###### tags: `shared` `linux` `terminal`

+ Open *.pdf/*.png
    `open <file_name>`
+ Downloading files from server
    `scp <userName>@140.114.26.9:<directory> <fileName1> <fileName2>`
+ Uploading foles to server
    `scp <fileName1> <fileName2> <userName>@140.114.26.9:<directory> `
+ Creating soft links
    `ln -s <source> <destination>`
+ Executing using soft link `./matlab`
    `./matlab [-nojvm] < <input_file.m> > <output_file.txt> [&]`
+ Sample Matlab code for saving figures/data (more at: https://www.mathworks.com/help/matlab/ref/saveas.html)
    `plot(<arguments>);`
    `save('example.mat');`
    `saveas(gcf,'example.png');`
    `close all;`
+ Adding user (need root)
    `adduser <user_name>`
+ Simple Linux bash commands
    + Change directory
        `cd <directory>`
    + Create folder
        `mkdir <directory>`
    + Remove file (use with care)
        `rm [-r|-f] <target>`
        -r: recursive(directory)
        -f: force
    + Move file
        `mv <source> <destination>`
    + Copy file
        `cp <source> <destination>`
    + Show directory contents
        `ls [-a|-l|-t] <directory>`
    + Search for string: grep
        `ls -alt ./ | grep *.txt`
    + Alias (short commands)
        `alias='ls -alt'`
    + Install packages(need sudo in finserver)
        `apt-get install <package_name>`
+ Environment variable (alternative for soft link)
    + For single user:(in /home/.bash_profile)
        `export PATH=$PATH:<PATH1>:<PATH2>`
        `source .bash_profile`
    + For all users:(in /etc/profile)(needs sudo)
        `export PATH=$PATH:<PATh1>:<PATH2>`
        `source /etc/profile`
    + For temporary usage:(inside terminal)
        `export PATH=$PATH:<PATH1>:<PATH2>`
    + After setting is done, you can use `matlab` instead of `./matlab`
+ execute Matlab/programs in background
    + Just append `&` at the end of the command (this will end once ssh is shut down)
    + Execute commands in background even if ssh is sut down
        `nohup <command> [< input.m] [> output.txt] &` (executing commands)
        `tail -f output.txt` (check output status)
        `lsof output.txt` (check nohup PID)
        `kill <PID>` (terminate process)
    + Find out current working directory of a running process
        `lsof -p <PID> | grep <cwd>`
    + See https://unix.stackexchange.com/questions/94357/find-out-current-working-directory-of-a-running-process for more details
+ Top command for GPU/CUDA
    + Show resources used (updated every second)
	    `watch -n0.1 nvidia-smi`
    + Advanced: `htop`
+ Task running statuses
    + More at: https://serverfault.com/questions/319684/what-s-s1-t-r-mean-in-ps-ax-ps-list
    + `ps aux | grep <task_name>`
    + To suspend/stop/restart a process
	    + From: https://www.tecmint.com/13-basic-cat-command-examples-in-linux/
	    + `kill -[TSTP|STOP|CONT] <pid_of_process>`
```
ROCESS STATE CODES
       Here are the different values that the s, stat and state output
       specifiers (header "STAT" or "S") will display to describe the state of
       a process.
       D    Uninterruptible sleep (usually IO)
       R    Running or runnable (on run queue)
       S    Interruptible sleep (waiting for an event to complete)
       T    Stopped, either by a job control signal or because it is being
            traced.
       W    paging (not valid since the 2.6.xx kernel)
       X    dead (should never be seen)
       Z    Defunct ("zombie") process, terminated but not reaped by its
            parent.

       For BSD formats and when the stat keyword is used, additional
       characters may be displayed:
       <    high-priority (not nice to other users)
       N    low-priority (nice to other users)
       L    has pages locked into memory (for real-time and custom IO)
       s    is a session leader
       l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
       +    is in the foreground process group
```
    
+ 5 commands to check memory usage on Linux: http://www.binarytides.com/linux-command-check-memory-usage/
+ Check file size
    + `df -s /home/*`
    + `du -sh /home/*`




