Introduction to UNIX
Thierry Toutain (RCS April 2016)
Command line - Terminal -Shell “Command-line interfaces are often preferred by more advanced computer users, as they often provide a more concise and powerful means to control a program or operating system.” Wikipedia
● Text-based interface with a command interpreter (shell, bash) in which you can type 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 talk by Sabry)
First commands and shortcuts
● hostname (try Hostname letter case matters!) ● whoami
● who (who is logged in)
● top (and top users)
● compgen -c (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
● !wh (! execute last command starting with the given string)
● h+Tab (autocompletion)
● Crtl+c interrupts current command (try sleep 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 tree -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 pwd try also which pwd)
Home directory path (try echo $HOME try env ) Absolute and relative paths Navigate in the filesystem
● Absolute paths always start with '/' the root directory
● Try to go to your scratch area using “cd” 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 ls ~ 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: chmod g+w nobackup ls -l chmod g-w nobackup Try ls -l --color alias ll='/bin/ls -l --color' ll
Let's make this alias permanent: ll -a
Create the configuration file .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 exit.
● Crtl+w to search for a string.
● Alt+g to move to a given line number.
● …
● Crtl+g for help (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> cat .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> mv .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/copy/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 test* -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 rm='/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> cp 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).