UNIX Tutorial
Contents
1. Introduction
Please use the following tutorial to familiarize yourself with the UNIX command-line environment.
The iSchool uses GNU/Linux on our cPanel servers, and has Mac OS and/or Windows on the built-in computers in the classrooms.
The primary resource for this tutorial is “A user guide to the UNIX system,” by Rebecca Thomas and Jean Yates. In order to help users have a thorough understanding of how the UNIX system works, we provide basic information before covering the main commands. If you need a quick guide, please skip the first section. Examples in this tutorial are most often run on the iSchool cPanel on a Mac machine.
2. Symbols and procedures
2.1. Shell prompt ends with $
, %
or >
The shell prompt is printed to signal its readiness to accept your command.
2.2. Command
The command
is the command from a human to a computer. An action (or function) can be performed after calling the command
.
2.3. Command line
The command line is the following characters of the shell prompt $
until you terminate the command line using the Return key.
2.4. Command format
$ command options arg1 arg2 |
2.5. Argument
Argument is the specify additional information to operate on. It usually comes after the command. The brackets []
indicate an optional argument.
2.6. Options
Each option indicates the possible modification of the original command. It usually appears after the normal command.
2.7. Control characters
Control characters do not represent a printable character. They are the computer's special interpretation.
To type a control character: Hold the CTRL
key and the letter (D
for example), then release the CTRL
key.
2.8. Space character
The space character can be typed by pressing the space bar. It is frequently required when typing a command. UNIX command line is picky when it comes to a space character.
Below is the example of a command line without a space character.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls-l - bash: ls - l: command not found |
With a space character, a command ls
and an option -l
will show file or directories, size, modified date and time, file or folder name and owner of file and its permission.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls -l total 16K drwxrwxr - x 2 linhcao2 linhcao2 57 Jun 29 16 : 47 . / drwx - - x - - x 29 linhcao2 linhcao2 4.0K Jun 29 14 : 46 .. / - rw - rw - r - - 1 linhcao2 linhcao2 32 Jun 29 15 : 53 newfile.py - rw - rw - r - - 1 linhcao2 linhcao2 31 Jun 29 15 : 54 oldfile.py - rw - rw - r - - 1 linhcao2 linhcao2 120 Jun 29 16 : 47 output.log |
Examples
linhcao2@linhcao2.web.ischool.illinois.edu [~] # rm -r resume |
#
is the root (superuser) shell prompt indicating in most system that you become root or login as root.
rm
is the command name.
-r
is the option.
resume
is the argument referring to the folder named resume
.
3. Getting Started
3.1. Login
Further information about using SSH to connect to the iSchool cPanel can be found on this Web Development Resources page (login required.)
3.2. Logout
You can officially log out of the cPanel session by typing CTRL + D
or exit
. You will know you have successfully logged out of the system when you see the displayed message below:
linhcao2@linhcao2.web.ischool.illinois.edu [~] # logout Connection to cpanel.ischool.illinois.edu closed. |
4. Basic commands
4.1. Shell Basics
Tab Completion
-
A single
Tab
. TheTab
key can be used to complete a command, directory name, or a file name. -
Multiple
Tab
. The shell will show the available options that you can type.
E.g: Pressing Tab
twice after typing python
reveals multiple versions of the programming language python
that are available from the current shell.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / public_html] # python python python2 python2. 7 python2. 7 - config python2 - config python - config |
Accessing the previous commands
CTRL + R
: look for the previous commands while typing part of the command- ↑↑ or ↓↓ : access the log of a number of past commands (usually 500)
4.2. Files and Directories
Useful symbols
~
– A tidle is used to access your home directory..
– A single period is used to refer to the current directory...
– Two periods are used to refer to the directory immediately above the current directory.
Navigating Directories: cd
and pwd
cd
- Description
Change directory
- Example
This command below will change the directory to the home directory.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / public_html] # cd ~ linhcao2@linhcao2.web.ischool.illinois.edu [~] # |
pwd
- Description
Print Working Directory
- Example
This command below will print the current working directory.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / public_html] # pwd / home / linhcao2 / public_html |
Manipulating Directories: mkdir
and rmdir
mkdir
- Description
creates a directory
- Example
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # mkdir tmp linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / tmp / |
tmp
is the new created directory in the ischool
directory.
rmdir
- Description
removes a directory if it is empty
- Example
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / tmp / linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # rmdir tmp linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # |
The use of rmdir
removes the created EMPTY tmp
directory in the ischool
directory.
Commands for Files: touch
, cp
, mv
, rm
, and file
touch
- Description
creates a file if it does not currently exist.
- Example
The ischool
directory below contains the oldfile.py. Using touch newfile.py
creates a newfile.py
file in the directory.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / oldfile.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # touch newfile.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / newfile.py oldfile.py |
cp
- Description
creates a copy of a file in another directory and/or with a different name.
- Syntax
cp [options] [source file ] [destination] |
- Examples
a) Copy the file newfile.py
currently located in the ischool
directory, [source file], to the public_html
directory, [destination].
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cp newfile.py ~/public_html/ |
b) Creates a copy of newfile.py
named newfile-bak.py
in the current directory.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cp newfile.py newfile-bak.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / newfile - bak.py newfile.py oldfile.py |
mv
- Description
Moves a file or directory to another location or name. Unlike cp
, mv
erases the original file after placing the file in a new location/name.
- Syntax
mv [options] [source] [destination] |
- Examples
E.g: Move newfile.py
from current directory, [source], into an existed folder named lis452
, [destination], and renames it to main.py
.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # mv newfile.py ~/public_html/lis452/main.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # |
mv
is useful to correct a misspelled filename:
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # mv newfiel.py newfile.py |
rm
- Description
r
em
oves a file.
- Syntax
rm [options] [source file ] |
- Examples
This command below removes the file newfile-bak.py
in the current directory.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / newfile - bak.py newfile.py oldfile.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # rm newfile-bak.py linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # ls . / .. / newfile.py oldfile.py |
file
- Description
tells you what type of file something is.
- Example
The newfile.py
is the Python script.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # file newfile.py newfile.py: Python script, ASCII text executable |
4.3. Output Redirection
Redirect stdout >
This redirects stdout to a file. If the file already existed, it would be overwritten with the new file.
- Example
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo cpanel > ischool.txt linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat ischool.txt cpanel |
Now you can see the contents of the created file ischool.txt
using the cat
command.
Append stdout >>
This appends stdout to an existed file instead of replacing contents of that file.
- Example
The ischool.txt
file has one existing line cpanel
. We append help desk
to the ischool.txt
file.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo help desk >> ischool.txt linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat ischool.txt cpanel help desk linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # |
Redirect stdin <
This can be used to get the standard input from the file instead of the keyboard.
- Example
The command sort
can be used to sort lines of text files.
a) Manual solution
You need to enter the stdin from the keyboard. First, you need to type the command sort
and press Enter
. You need to type the lines that you want to sort. Type CTRL + D
when you are done.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # sort ischool cpanel help desk itd unix |
After pressing CTRL + D
, you will see the result underneath the input.
cpanel help desk ischool itd unix |
b) Automated solution
Insert the contents to the keyboard can be automated using the file. In this case, we create a file named test.txt
and edit the file with an editor. The file test.txt
looks like this:
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat test.txt ischool cpanel help desk itd unix |
To sort the lines in the file test.txt
, we can redirect the input from the file.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # sort < test.txt cpanel help desk ischool itd unix |
Pipes |
This takes/“pipes” standard output from one program, or process, and sends it to another command’s standard input.
- Example
Using the same test.txt
above, we are going to sort the lines in the text file using this command:
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat test.txt | sort cpanel help desk ischool itd unix |
In this example, the output of the cat
command is the content of the test.txt
file. Then, it pipes to sort
command as the input. In other words, the sort
command uses output of the cat
command as the input, and it displays the sorted lines.
4.4. Working with Text Files
echo
- Description
Echo the STRING(s) to standard output.
- Syntax
echo [short option] ... [strings]... |
- Examples
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo test test linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo 'test' test linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo "test" test |
cat
- Description
Concatenate FILE(s), or standard input, to standard output.
- Syntax
cat [option] ... [ file ] ... |
- Examples
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo test > output.log linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat output.log test |
cat
can be a useful command when you need to combine/concatenate multiple text files into a single file. This example shows when two log files are combined into one file namedoutput.log
.
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo test2 >> output.log linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # echo test3 test4 >> output.log linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # cat output.log test test2 test3 test4 |
head
- Description
Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -
, read standard input.
- Syntax
head [option] ... [ file ] ... |
- Examples
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # head output.log test 1 test 2 test 3 test 4 test 5 test 6 test 7 test 8 test 9 test 10 |
tail
- Description
Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -
, read standard input.
- Syntax
tail [option] ... [ file ] ... |
- Examples
linhcao2@linhcao2.web.ischool.illinois.edu [~ / ischool] # tail output.log test 8 test 9 test 10 test 11 test 12 test 13 test 14 test 15 test 16 |
4.5. Working Remotely
SSH
SSH
stands for "Secure Shell." SSH
can be used to connect to a remote computer. Most modern operating systems (except Windows) support SSH by default. If you are using Windows, third-party programs such as PuTTY (recommended) or Cygwin give you the ability to use SSH
. If you are in a non-Windows environment, pull up a shell, and you’re ready to use ssh
.
As we mentioned earlier, you can use SSH
to login to the iSchool cPanel.
SCP
SCP
stands for "Secure Copy." SCP
can be used to copy files to/from a remote machine from/to your local machine using ssh
connection. You might need to enter your remote machine password if needed.
- Syntax
scp [options] [source] [destination] |
The source and destination can be either<username>@<remote host>:<path to file>
or<local file>
.
-
Example
-
Copy a local file to the remote machine
The command below can be used if you want to copy the file alloutput
from the current local directory to the folder ischool
in the remote machine which is your iSchool cPanel.
gwork060:ischool linhcao2$ scp alloutput linhcao2@cpanel.ischool.illinois.edu: / home / linhcao2 / ischool Password: stdin: is not a tty alloutput 100 % 81 74.3KB / s 00 : 00 |
- Copy a file from a remote machine to the local directory
If you want to copy the file newfile.py
from the current remote directory to the folder ischool
in the local machine, you can use the command below:
gwork060:ischool linhcao2$ scp linhcao2@cpanel.ischool.illinois.edu: / home / linhcao2 / ischool / newfile.py ~ / ischool Password: stdin: is not a tty newfile.py 100 % 32 32.6KB / s 00 : 00 |
For a Windows user, you can open up a Windows command prompt (NOT PuTTY). Navigate to the folder C:\Program Files (x86)\PuTTY)
. You can run the pscp
command instead of the scp
command here.
Example
pscp C:\Users\Desktop\ file .txt linhcao2@cpanel.ischool.illinois.edu: / home / linhcao2 / public_html / |
NOTE: Please remember to use the Tab for completion.
For more detailed instructions, utilize the tutorial available on Box.