Unix Tutorial

Unix Tutorial
Intermediate
CS Help Desk: Marc Jarvis, Monica Ung, 2014
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Helpful info
● SOCS wiki
http://socsinfo.cs.mcgill.ca/wiki/Main_Page
● Help desk - McConnell 209N
[email protected]
● CSUS help desk - 3rd floor Trottier (look for the flag)
http://csus.cs.mcgill.ca/
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Shell
A command-line interpreter that lets the user communicate with the
operating system.
Shells: sh, bash, csh, tsch
echo $0 - the name of the running process
echo $SHELL - the user’s shell filepath
echo $PATH - the shell’s search paths
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Environment Variables
A unique environment maintained until you log out.
env - list all the pre-defined environment variables
TEST=”myVar” - set your own variable
TEST2=15
echo $TEST $TEST2 - outputs: myVar 15
export TEST2=”newVar” - change the variable’s value
PS1 - primary prompt string
default: PS1=“[\u][\h][\w]”
CS Help Desk: Marc Jarvis, Monica Ung, 2014
What is a shell script?
Sequences of commands stored in a text file for the shell
to run.
Uses:
● Create your own commands
● Automate tasks (eg: at start-up, before shutting down)
● Customize your profile
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Simple script - Hello World
● Create a new file
vim Hello
● Write the script
#!/bin/bash
echo Hello World
● Change permissions
chmod u+x Hello
● Run the script
./Hello
CS Help Desk: Marc Jarvis, Monica Ung, 2014
File Information
touch file.txt
ls -l file.txt
> -rw------- 1 jsmith nogroup 0 Sep 23 10:14 file.txt
[permissions][# of links][user][group][filesize][date modified][filename]
● Permissions: drwxrwxrwx
[directory?][user][group][others]
CS Help Desk: Marc Jarvis, Monica Ung, 2014
File Permissions - Method 1
chmod - Change file mode bits
r:read, w:write, x:execute
u:user, g:group, o:others, a:all three (ugo)
Examples:
● chmod a+rx file
●
Add read and execute permissions to all
● chmod ou-rw
●
Remove read and write permissions from others and the user
CS Help Desk: Marc Jarvis, Monica Ung, 2014
File Permissions - Method 2
chmod - Change file mode bits
Examples:
● chmod 777 file
●
Enable full permissions to all three
● chmod 652 file
●
Enable read and write to user,
read and execute to group,
write to others
rwx____
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7
-rw-r-x-w- = 652
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Common shell scripts
.login (for csh and tcsh shells), .profile (for bash shells)
●
●
●
These are bash scripts ran at login
system-wide script: /etc/profile
➢ this file gets run by anyone who logs into that system
personal script: ~/.profile
➢ this file gets run only when you log in
Customizations you can do to your profile
●
Add aliases for commands
➢
➢
●
●
alias lsa=‘ls -la’
alias sshjsmith=‘ssh [email protected]’
Change your prompt (normally it’s “username@host[current directory]: ”)
Append new values to environment variables
➢
➢
PATH=$PATH:.
This adds “.” (the current directory) to the search path
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Shell script example
#!/bin/bash
if [ -z "$1" ]; then
echo usage: $0 directory
exit
fi
SRCD=$1
TGTD="/var/backups/"
OF=home-$(date +%Y%m%d).tgz
tar -cZf $TGTD$OF $SRCD
●
●
checks if you give an argument ($1)
if argument given, make backup of your specified file/folder and store backup in
/var/backups/home-DATE.tgz
For more info on bash scripting: http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Archiving and compressing files
● tar combines multiple files into one archive
● gzip compresses the data
tar [options] [archiveName] [targetFilesOrFolders]
Options:
●
●
●
●
●
c = create archive
z = gzip it (compress it)
v = verbose (it will describe each step it’s doing)
f = file
x = extract archive
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Archiving and compressing files
Examples:
● tar cvzf myTarFile.tar.gz backup
➢
We created a compressed archive of our folder called ‘backup’ and
named the archive myTarFile.tar.gz. We told the command to be
verbose.
● tar xvzf myTarFile.tar.gz
➢
We extract a compressed archive called myTarFile.tar.gz. We told
the command to be verbose.
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Editing files with Vim
vim - Vi IMproved
2 modes: Command and Edit
●
●
●
●
‘a’ or ‘i’ to enter Edit Mode
‘esc’ to enter Command Mode
:w → save
:q
:wq
:q!
→ exit
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Vim - Navigation
How to navigate in command mode:
●
●
●
●
●
●
●
arrow keys or h , j , k , l
➢ Hold ‘shift’ button to move by window
w: move cursor one word-length forward
b: move cursor one word-length backwards
$: go to end of the line
gg: go to beginning of document
G: go to end of document
10G: go to the 10th line
Searching:
●
●
/word: searches for ‘word’ in the document
n: go to next match
N: go to previous match
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Vim - Editing
Inserting text (going into edit mode):
●
●
●
●
●
i: start inserting text before the cursor
a: start inserting text after the cursor
A: go to end of line and go into edit mode
s: delete character at the cursor and go into edit mode
dw: delete word where your cursor is and stay in command mode
Copy and paste:
●
●
●
●
●
yy: copy a line (“yank”)
dd: cut a line (“delete”)
p: paste
yw: copy word (“yank word”)
dw: cut word (“delete word”)
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Vim resources
Type ‘vimtutor’ in the terminal.
Cheatsheet: http://vim.rtorr.com/
More: http://allhotkeys.com/vim-hotkeys.html
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Grep and Find
●
●
“grep” searches for patterns inside files
“find” is a file locator
grep hello *.doc
●
searches for any “.doc” files that contain the word “hello”
grep ^h* *
●
searches for files that have any lines starting with “h”
find /home -name comp*
●
●
search for file with filename starting with “comp”.
start the search at the home directory
find . -name a*
●
search for files starting with “a” in the current directory (“.”)
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Which
● shows path of commands
● useful if you forget where your custom
command is stored
which ls
which cd
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Viewing and killing processes
ps aux
● Lists currently running processes and their information
●
●
●
a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
●
●
●
●
●
PID: unique ID associated with the process
RSS: Resident Set Size (how much memory is allocated to the process)
VSZ: Virtual Memory Size (how much memory a process is allowed, including swap space)
TTY: Terminal type (terminal that executed the command)
STAT: process’ status code
kill [PID]
● Terminates process
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Piping and redirection
Output of one command is the input of
another command
●
●
●
●
●
ls -la | less
➢ The output of “ls -la” is sent to the “less” command
ls -l > file.txt
➢ The output of “ls -l” is written to file.txt
cat file1.txt >> file2.txt
➢ The text from file1 is appended to the end of file2
cat file1.txt >! file2.txt
➢ The text from file1 overwrites all content in file2
sort < list_of_stuff.txt
➢ The “sort” command reads from the file and sorts the items
CS Help Desk: Marc Jarvis, Monica Ung, 2014
A bigger piping example
ps aux | grep conky | grep -v grep
| awk '{print $2}' | xargs kill
● Kills the process called conky
➢
➢
➢
➢
➢
ps aux: lists all running processes
grep conky: find the process called conky
grep -v grep: exclude all lines not matched in previous grep
awk ‘{print $2}’: retrieve the 2nd word from the line found by
grep, which is the PID (process ID number) of conky
xargs kill: use the PID to kill the process
● This is useful when you have a frozen process and want to kill it.
Instead of doing separate commands, we can pipe them together!
CS Help Desk: Marc Jarvis, Monica Ung, 2014
Transferring files to/from CS account
scp [path of source] [destination path]
●
●
●
Transferring a file from your local computer to your CS account
➢ scp /path/to/file username@host:“/path/on/server”
➢ Example: scp /Users/john/website/index.html [email protected]:
“/home/2014/jsmith/public_html/index.html”
Note: when writing the path, if a filename or folder name has a space, add “\” before the space.
➢ scp /Users/john/My\ Pictures/me.jpg [email protected]:
“/home/2014/jsmith/pictures/me.jpg”
Copying a file from CS account to your local computer
➢ scp username@host:“/path/on/server” /path/to/file
➢ Example: scp [email protected]:“/home/2014/jsmith/file.doc”
/Users/john/Document/file_from_cs_account.doc
Alternative to scp is rsync.
Check it out by doing man rsync