Introduction to

Thierry Toutain (RCS April 2016)

Command line - Terminal --line interfaces are often preferred by advanced computer users, as they often provide a more concise and powerful means to control a program or .” Wikipedia

● Text-based interface with a command interpreter (shell, ) in you can and execute commands to create/manage your data (files). ● Many pre-exisiting unix commands understood by bash. ● Build your own commands (scripts) tailored for your needs (next by Sabry)

First commands and shortcuts

● hostname (try Hostname letter case matters!) ●

(who is logged in)

(and top users)

● compgen - (display all the commands available)

● whatis hostname

● man hostname (man on-line manual pages, navigate with Page Up/Down, quit with 'q')

● hostname abel

● Use up/down arrows to browse typed commands

(try also Crtl+)

● !wh (! execute last command starting with the given string)

● h+Tab (autocompletion)

● Crtl+c interrupts current command (try 30)

● Crtl+l clears the window

● Ctrl+a (go to start of line)

● Ctrl+e (go to end of line)

● ... Files - Directories – Filesystem - Paths

Root (topdir) / (try -d -L 1 /) bin usr lib … work cluster projects

jobs ... users home ... software

user1 … userN user1 … userN

$SCRATCH

$HOME = /cluster/home/username /usit/abel/u1/username (try try also which pwd)

Home (try $HOME try ) Absolute and relative paths Navigate in the filesystem

● Absolute paths always start with '/' the root directory

● Try to go to your scratch area using “” which is the command to navigate around cd /work/users/username (use autocompletion to speed-up typing) pwd cd (equivalent to cd ~ or cd $HOME) cd – (back to the previous location which is scratch area)

● Relative paths are defined relatively to the current location (as given by pwd)

● Try to go to your scratch again cd /work/users pwd cd username (no need to type the full path) pwd cd .. (up one level) pwd cd (back to home directory) pwd cd . (current location) pwd Your home directory

$HOME = /cluster/home/username /usit/abel/u1/username ~ is a shortcut (try echo ~)

username (try ~ or simply ls)

00Readme_abel nobackup

-bash-4.1$ ls -l ~ (or simply ls -l) total 3 - rw- r-- r-- 1 root root 3004 Mar 30 01:26 00_README_Abel d rwx r-x r-x 2 guest074 users 0 Mar 30 01:25 nobackup

permissions owner group size date of last modification name Try: g+ nobackup ls -l chmod g-w nobackup Try ls -l --color ll='/bin/ls -l --color' ll

Let's this alias permanent: ll -a

Create the configuration .bash_profile nano ~/.bash_profile

Add the following two lines: alias ll='/bin/ls -l --color' PS1='abel>'

PS1 redefines the prompt. Something more fancy: PS1=”\[\e[4;43m\]\h:\w\[\e[0m\]>”

nano (text editor)

● nano filename (or nano -m filename for mouse support). ● Type text and navigate with arrow keys and Page Up/Down.

● Save with Crtl+o or Crtl+x to save and .

● Crtl+w to search for a string.

● Alt+g to to a given line number.

● …

● Crtl+g for (and Crtl+x to exit help menu).

.bash_profile vs .bashrc

● Log out and in to check that ll now works

● But it won't if you open another bash shell because .bash_profile is not read by the shell in this case: abel> .bash_profile alias ll='/bin/ls -l --color' abel> bash abel> ll bash: ll: command not found abel>exit

● Solution: put the alias in .bashrc and “source” it from .bash_profile abel> .bash_profile .bashrc abel> echo "source ~/.bashrc" > .bash_profile (> redirect output to a file) abel> cat .bash_profile source ~/.bashrc Basic commands for file/directory manipulation

● Create/view//move/delete a file abel>echo "first line" > testfile Alternatively: abel>echo "second line" >> testfile abel> cat > testfile abel>cat testfile first line first line second line second line Crtl+D

abel>less 00_README_Abel abel>head 00_README_Abel abel>tail 00_README_Abel

abel>cp testfile testfile.txt Avoid space *,;øæå in filenames abel>ll * -rw-r--r-- 1 guest074 users 23 Mar 31 14:06 testfile -rw-r--r-- 1 guest074 users 23 Mar 31 14:07 testfile.txt

abel>rm testfile (alias ='/bin/rm -i' prompt before every removal) abel>ll test* -rw-r--r-- 1 guest074 users 23 Mar 31 14:07 testfile.txt

abel>mv testfile.txt another-testfile.txt abel>ll another-testfile.txt

-rw-r--r-- 1 guest074 users 23 Mar 31 14:07 another-testfile.txt ● Create/copy/move a directory abel>mkdir testdir abel>rmdir testdir

abel>mkdir testdir abel>mkdir testdir/subdir (mkdir -p testdir/subdir1/subdir2/subdir3)

abel>mv another-testfile testdir/ (last / is optional) abel>rmdir testdir abel> rm -fr testdir (dangerous!)

abel>mkdir testdir abel> testdir another-testdir cp: omitting directory `testdir' abel> ll abel> cp -r testdir another-testdir abel>rmdir testdir abel>mv another-testdir testdir

summary

● Learn about commands (man, whatis, which) and speed-up typing (autocompletion, history,Ctrl+a, Ctrl+e).

● Navigate in the filesystem with cd, pwd.

● Customise your shell with .bashrc (alias, prompt +more to come)

● Create and manage files and directory (nano, cat, cp, mv, rm, mkdir, rmdir, chmod).