Bash

From SGEES Wiki
Jump to navigation Jump to search
Unix/Linux shell is very powerful tool, however it might look not friendly for inexperienced users. Fortunately you don't need too much skills for starting.

Recommendations for further reading:

   http://wbell.web.cern.ch/wbell/BashIntro/
   http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
   http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html

What you REALLY need for the first time

man

man man
man whatever

Navigation in man

q - quit
Up, Dn, Left, Right, PgUp, PgDn, Home, End - navigate
/ - forward search 
? - backward search 
n - search next

cd

cd (without any) -- go to your home directory
cd path/to, cd ~/path/to -- go to directory relative to your home
cd /path/to -- go to directory relative to root

pwd

pwd -- get Path to Working Directory

ls

ls - list files in current directory
ls /path/to - list files at other directory
ls -l - long format list
ls -a - show all files (hidden, system, pipes, symlinks e.t.c)

df (disk free)

df - show free space at current storage
df /path/to - show free space at other folder

du (disk usage)

du -- calculate size of files and folders in current directory 
du /path/to -- size of files and folders in other directory 

ps

List of processes
ps ax|grep name - find a process by name

kill PID - kill process by it's PID (got by ps)
killall process - kill all processes with given name
kill -9 PID - kill the process mercilessly

find

Find files, directories, e.g. let's find all files which name begins from “sunday”:

find /path/to/folder -type f -name "sunday*" 

rm

Erase files and directories

rm /path/to/file -- erase single file
rm file1 file2 file3 fileN -- erase several files 
rm -r /path/to/folder -- erase directory with all subdirectories

Erase files by mask:

find /path/to/files -type f -name "foo.ba?" -delete

mkdir

mkdir folder -- make new directory 

Make new directory and parent directories as needed:

mkdir -p /path/to/grandparent/parent/child/folder

ln

ln -- create symlink or hardlink

From folder to current directory

ln -s /path/to/folder .

From one directory to another

ln -s /path/to/folder /path/to/link

cp

cp -- copy files and folders

Copy single file

cp /path/to/source/filename.ext /path/to/destination

Copy folder

cp -R /path/to/source/folder /path/to/destination

scp

scp -- Copy files and folders over SSH

Copy single file

scp user@server:/path/to/source/file.ext /path/to/destination 

From one server to another

scp user@server1:/path/to/source/file.ext user@server2:/path/to/destination

Copy folder

scp -r user@server:/path/to/source/folder /path/to/destination 

Keyboard shortcuts

Autocompletion

Tab-Tab-Tab

History

Ctrl+R, then type something then Ctrl+R Ctrl+R Ctrl+R 

Execution break

Ctrl+C

Shell exit

Ctrl+D

Job managing

Ctrl+Z - active job suspend
jobs - jobs list
fg %job_number - put job to foreground (e.g.: fg %3)
bg %job_number - put job to background
disown %job_number - detach background process from terminal 

Examples of job managing

Variant 1: you have run long process over ssh and realized that you need to disconnect, but keep the process running:

Ctrl+Z 
bg %1 
disown %1 

Variant 2: start the process with nohup:

nohup my_program 

Variant 3: use screen or tmux

ssh user@server
man screen
screen