Unix/Linux Tutorial
Table of Contents:
About
This document is a short introduction for new users to unix/linux. Much of the information in this tutorial was obtained from Treebeard's Unix Cheat Sheet. in the examples you type the command after the dollar sign $ in your Terminal Window. The output of the command is shown on the next line(s).
The Basics
Help on any Unix Command
Getting help is one of the most important things you will need to take from this tutorial. At any point you wish to learn more about any single command you can use the following tools.
| Tool | Description |
|---|---|
| man {command} | Type man ls to read the manual for the ls command. |
| whatis {command} | Give short description of command. |
| info {command} | Long description of command |
Example (to quit 'man' command hit the letter 'q', page down with arrow keys):
$ man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuSUX nor --sort.
Terminal Window
This is where you type all these fun commands, opening a new terminal window is slightly different on each GUI platform. Here are some of the most common ones.
- Redhat (ECE Lab Machines): Applications > System Tools > Terminal
- Ubuntu: Applications > Accessories > Terminal
- MacOS: Applications > Utilities > Terminal
Manage Directories
Change Directories
To move around the filesystem you will need to familiarize yourself with these commands. cd or change directory
| Command | Description |
|---|---|
| pwd | Show your current directory |
| cd {dirname} | There must be a space between. |
| cd | Go back to home directory, useful if you're lost. |
| cd ~ | Like cd by itself takes you to your home directory |
| cd .. | Go back one directory. |
Example:
$ pwd /home/students/user/ $ cd Directory1 $ pwd /home/students/user/Directory1/ $ cd $ pwd /home/students/user/
Make a New Directory
| Command | Description |
|---|---|
| mkdir {dirname} | Make a new directory with the name {dirname} |
Remove a Directory
| Command | Description |
|---|---|
| rmdir {dirname} | Only works if {dirname} is empty. |
| rm -r {dirname} | Remove all files and subdirs. Careful! |
Managing Files
List Files
Display a list of files in your current directory or in {path}
| Command | Description |
|---|---|
| ls {path} | It's ok to combine attributes, eg ls -laF gets a long listing of all files with types. |
| ls -l {path} | Long listing, with date, size and permisions. |
| ls -a {path} | Show all files, including important .dot files that don't otherwise show. |
| ls -F {path} | Show type of each file. "/" = directory, "*" = executable. |
| ls -R {path} | Recursive listing, with all subdirs. |
| ls {path} > {filename} | Redirect directory to a file. |
| ls {path} | less | Show listing one screen at a time. |
Example:
$ ls -laF
drwx-----x 23 user group 8192 2008-02-12 13:49 ./
drwxr-xr-x 142 user group 24576 2008-03-05 08:42 ../
-rw-r--r-- 1 user group 7475 2006-12-08 13:25 afile.txt
-rw-r--r-- 1 user group 3583 2007-02-02 13:07 anotherfile.txt
drwx------ 3 user group 8192 2007-09-17 08:46 Directory1/
drwxr-xr-x 2 user group 4096 2006-08-17 13:04 Directory2/
Copy a File or Directory
| Command | Description |
|---|---|
| cp {file1} {file2} | Copy file1 to file2. |
| cp -p {file1} {file2} | Copy file1 to file2 with the same permissions. |
| cp -r {dir1} {dir2} | Recursive, copy directory and all subdirs. |
| cat {newfile} >> {oldfile} | Append newfile to end of oldfile. |
Move/Rename a File
| Command | Description |
|---|---|
| mv {oldfile} {newfile} | Moving a file and renaming it are the same thing. |
Example:
$ ls -F file1 Directory/ $ mv file1 file2 $ ls -F file1 Directory/ $ mv file2 Directory/ $ cd Directory $ ls file2
Delete a File
| Command | Description |
|---|---|
| rm {file} | ? and * wildcards work like DOS should. "?" is any character; "*" is any string of characters. |
| rm -i {file} | Prompts you to confirm the removal of {file} useful when using the wildcard * |
View a Text File
| Command | Description |
|---|---|
| more {filename} | View file one screen at a time. |
| less {filename} | Like more, with extra features. |
| cat {filename} | View file, but it scrolls. |
Edit a Text File
| Command | Description |
|---|---|
| pico {filename} | The same editor PINE uses, so you already know it. |
| vi {filename} | Advanced text editor refer to the wikipedia article: http://en.wikipedia.org/wiki/Vi |
| emacs {filename} | Semi Advanced text editor refer to the wikipedia article: http://en.wikipedia.org/wiki/Emacs |
Create a New File
| Command | Description |
|---|---|
| cat > {filename} | Enter your text (multiple lines with enter are ok) and press control-d to save. |
| pico {filename} | Create some text and save it. |
| touch {filename} | Creates a empty file which you can then edit. Refer to Edit a Text File. |
Compare two files
| Command | Description |
|---|---|
| diff {file1} {file2} | Show the differences. |
| sdiff {file1} {file2} | Show files side by side. |
Other Useful Text Commands
| Command | Description |
|---|---|
| grep '{pattern}' {file} | Find regular expression in file. |
| sort {file1} > {file2} | Sort file1 and save as file2. |
| sort -o {file} {file} | Replace file with sorted version. |
| spell {file} | Display misspelled words. |
| wc {file} | Count words in file. |
Advanced Information
Wildcards and Shortcuts
- *
- Match any string of characters, eg page* gets page1, page10, and page.txt.
- ?
- Match any single character, eg page? gets page1 and page2, but not page10.
- [...]
- Match any characters in a range, eg page[1-3] gets page1, page2, and page3.
- ~
- Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it.
- .
- The current directory.
- ..
- One directory up the tree, eg ls ...
Pipes and Redirection
Pipes and Redirection are useful tools to chain commands together to do complex operations. They are usually for advanced users but some uses are very simple and hard to live with out.
- {command} > {file}
- Redirect output to a file, eg ls > list.txt writes directory to file.
- {command} >> {file}
- Append output to an existing file, eg cat update >> archive adds update to end of archive.
- {command} < {file}
- Get input from a file, eg sort < file.txt
- {command} < {file1} > {file2}
- Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt.
- {command} | {command}
- Pipe one command to another, eg ls | more gets directory and sends it to more to show it one page at a time.
File Permissions
Unix permissions concern who can read a file or directory, write to it, and execute it.
- 'r' = read
- 'w' = write
- 'x' = execute (for directory or programs)
Consider one of the examples from List Files above:
$ ls -l -rw-r--r-- 1 user group 7475 2006-12-08 13:25 afile.txt drwx------ 3 user group 8192 2007-09-17 08:46 Directory1/
The permissions on this file are displayed in the first column -rw-r--r--:
-rw-r--r-- drwx------ |||||||||| |||||||||| |||||||+++--- World Permission (rwx) ||||+++-------Group Permission (rwx) |+++----------User(owner) Permission (rwx) +-------------File Type "d" = directory '-' = file 'l' = Symbolic link
The other columns are as follows:
1 user group 7475 2006-12-08 13:25 afile.txt (*) (owner) (group) (size) (last modified date) (filename) * Number of symbolic links to this file
You can change file permissions with letters:
- 'u' = user (yourself)
- 'g' = group
- 'o' = world
- 'a' = everyone
- chmod u+rw {file}
- Give yourself read and write permission
- chmod u+x {file}
- Give yourself execute permission.
- chmod a+rw {file}
- Give read and write permission to everyone. (Use this with caution!)
- chmod o-rwx {file}
- Remove read, write and execute from world
Example:
$ ls -l afile.txt -rw-r--r-- 1 user group 7475 2006-12-08 13:25 afile.txt $ chmod u+x afile.txt $ ls -l afile.txt -rwxr--r-- 1 user group 7475 2006-12-08 13:25 afile.txt $ chmod o-r afile.txt $ ls -l afile.txt -rwxr----- 1 user group 7475 2006-12-08 13:25 afile.txt
System Info
| Command | Description |
|---|---|
| date | Show date and time. |
| df | Check system disk capacity. |
| du | Check your disk usage and show bytes in each directory. |
| more /etc/motd | Read message of the day, "motd" is a useful alias.. |
| printenv | Show all environmental variables. |
| quota -v | Check your total disk use. |
| top | Check out processes that are running on the machine. |
| uptime | Find out systems uptime info. |
| w | Who's online and what are they doing? |
How to Make an Alias
An alias lets you type something simple and do something complex. It's a shorthand for a command. If you want to type "dir" instead of "ls -l" then type alias dir 'ls -l'. The single quotes tell Unix that the enclosed text is one command. The command to create an alias differs on each shell.
The ECE default shell is bash which will automatically read aliases that are in your .bash_aliases file. The tcsh shell will read the .csh_aliases file.
Example of a .bash_aliases file (bash, sh and ksh use this format):
# alias {alias name}="{command}"
alias ls="ls -F"
alias ll="ls -lah"
alias ssh="ssh -X"
alias mroe="more"
alias vi="vim"
Example of a .csh_aliases file (csh and tcsh use this format):
# alias {alias name} "{command}"
alias ll "ls -la"
alias dir "ls -la | less"
alias rm "rm -i"
How to Make a Script
A Unix script is a text file of commands that can be executed, like a .bat file in DOS. Unix contains a powerful programming language with loops and variables that I don't really understand. Here's a useful example.
Unix can't rename a bunch of files at once the way DOS can. This is a problem if you develop Web pages on a DOS machine and then upload them to your Unix Server. You might have a bunch of .htm files that you want to rename as .html files, but Unix makes you do it one by one. This is actually not a defect. (It's a feature!) Unix is just being more consistent than DOS. So make a script!
Make a text file (eg with pico) with the following lines. The first line is special. It tells Unix what program or shell should execute the script. Other # lines are comments:
#! /bin/csh # htm2html converts *.htm files to *.html foreach f ( *.htm ) set base=`basename $f .htm` mv $f $base.html end
Save this in your home directory as htm2html (or whatever). Then make it user-executable by typing chmod u+x htm2html. After this a * will appear by the file name when you ls -F, to show that it's executable. Change to a directory with .htm files and type ~/htm2html, and it will do its stuff.
Think about scripts whenever you find yourself doing the same tedious thing over and over.
DOS vs UNIX Commands
| Action | DOS | UNIX |
|---|---|---|
| change directory | cd | cd |
| change file protection | attrib | chmod |
| compare files | comp | diff |
| copy file | copy | cp |
| delete file | del | rm |
| delete directory | rd | rmdir |
| directory list | dir | ls |
| edit a file | edit | pico |
| environment | set | env |
| find string in file | find | grep |
| help | help | man |
| make directory | md | mkdir |
| move file | move | mv |
| rename file | ren | mv |
| show date and time | time | date |
| show disk space | chkdsk | df |
| show file | type | cat |
| sort data | sort | sort |

