Unix Tutorial

The following is a quick overview of essential UNIX commands.

Command Example Usage Description
ls

ls

ls –l

ls –ld directory

List directory contents

Long listing

List permissions on directory

pwd pwd Display current directory
cp

cp source target

cp –r source target

Copy file source to target

Copy directory, recursive copy

mv mv source target Move file or directory from source to target (the source is removed after the move)
rm

rm filename

rm –f filename

rm –rf directory

Remove a file

Remove a file in which you have directory write privileges but not file write privileges

Remove non-empty directory

mkdir mkdir directory Create a directory
rmdir rmdir directory Remove empty directory
ps

ps

ps –ef

ps –u userid

Display process statuses

Display all processes

Display all process for userid

kill

kill PID

kill -9 PID

Terminate process with process ID (PID). Sends UNIX process the TERM signal. This signal should terminate most UNIX processes.

Terminate process with PID. Sends UNIX process the KILL signal. This should only be used when normal attempts to terminate the process are ineffective.

grep

grep pattern filename

grep –l pattern filename(s)

Display all occurrences of pattern in filename

Display all filename(s) containing pattern

cat

cat filename

cat file file2

cat file1 file2>file3

Display contents of filename

Display contents of file1 and file2

Create file3 from contents of file1 and file2

more more filename Display file contents, one page at a time
man man command Display UNIX man(ual) page for given command
tar

tar tvf tarfile.tar

tar xvf tarfile.tar

tar cvf tarfile.tar*

Display contents of tarfile

Extract contents of tarfile

Create tarfile named tarfile.tar with all files and directories in the current directory

chmod chmod 755 directory Change file or directory permissions
gunzip gunzip file.gz Uncompress .gz file (generally used with tarfiles). If it is not in your path, check /vol.nsm/tools/bin.
vi vi file Edit file
find find / -name file Locate file starting in root (/) directory
exit exit Logout of your UNIX session
id id Display user name, user ID, and primary group ID

groups

-- or --

id -a

groups

-- or --

id –a

Display all UNIX groups to which you belong

hostname

-- or --

uname -n

hostname

-- or --

uname -n

Display the hostname of the UNIX machine
passwd passwd Change your UNIX password
env env Display your environment variables
nohup nohup command & Execute command in the background, and do not terminate the process if you logout of your UNIX session.
command & command & Execute the command in the background. Output will be sent to a file named nohup.out rather than your display.
jobs jobs List background processes
Ctrl-Z and bg Suspend current foreground process, put process in background for execution
gzip gzip filename Compress file (generally used with tarfiles)

File and Directory Permissions Demystified

When doing a long listing (ls –l) of a directory, we see the following entry:

drwxr-xr-x 2 hutchib nsm 512 Apr 20 11:09 bin

This is a directory named bin that is owned by user hutchib and group nsm. Let's explore the first 10 characters in detail.

  • The first character is a d, which indicates that we are looking at a directory. Other possibilities for the first character include an - for an ordinary file and an l for a symbolic link (like a shortcut in Windows).
  • Characters 2 through 4 correspond to user permissions, or permissions corresponding to the owner of the directory. They are rwx, which indicates that the directory is readable, writable, and executable by the user (the owner of the directory, hutchib).
  • Characters 5 through 7 correspond to group permissions. In this example, these permissions apply to the group nsm. They are r-x, indicating that the directory is readable and executable by any user belonging to group nsm.
  • Characters 8 through 10 correspond to other permissions. If you are not the owner of the directory, nor a member of the group, the other permissions are applicable. They are r-x, meaning that the directory is readable and executable by everyone else.

What do read, write, and execute permissions mean on a directory?

Read = You are able to list contents of the directory and display contents of files.

Write = You are able to create, modify, or delete files in the directory.

Execute = You are able to change into the directory. (Even if you had read and write privileges
on a directory, if you do not have execute privileges on the directory, you would not be able to get into (cd) the directory.)

Note: If you have write privileges to a directory, you may delete any file in that directory, even if it is not writable, and is owned by somebody else.

What do read, write, and execute permissions mean on a file?

Read = You are able to list the contents of a file.

Write = You are able to modify or delete the file. (If you have permission to write to a file, you
may modify it, but may not delete it, if the directory is not writable.)

Execute = You are able to execute the file. (This is needed if the file is a shell script or Executable.)

How do I change file/directory permissions?

You may change file and directory permissions with the chmod command.

The syntax is:

chmod octal-mode file_or_directory

You may recursively change permissions with the chmod –R command.

When looking at the following table, remember these values when setting file or directory permissions:

Read = 4
Write = 2
Execute = 1

Permissions Octal-mode Command

-rwxrwxrwx

User: rwx = 4 + 2 + 1 = 7

Group: rwx = 4 + 2 + 1 = 7

Other: rwx = 4 + 2 + 1 = 7

777 chmod 777 file_or_directory

-rwxr-xr-x

User: rwx = 4 + 2 + 1 = 7

Group: r-x = 4 + 1 = 5

Other: r-x = 4 + 1 = 5

755 chmod 755 file_or_directory

-rw-r--r--

User: rw- = 4 + 2 = 6

Group: r-- = 4

Other: r-- = 4

644 chmod 644 file_or_directory

Useful 'vi' Commands for Vim Text Editor

The vi editor has two modes: Input and Command. In Input mode, any characters you type will be entered into the text file. In Command mode, characters you type are used to move around the screen and modify the file. If you are unsure which mode you are in, press Esc, and you will be put into Command mode.

Command Description
h Move cursor one position left
j Move cursor one position down
k Move cursor one position up
l Move cursor one position right
:1 Go to beginning of file
G Go to bottom of file
0 Go to beginning of line
$ Go to end of line
i Insert text before cursor
a Append text after cursor
A Append text at end of line
Esc Enter command mode
:w Save the file, continue working
:wq Save the file and exit
:q! Exit without saving
:%s/search/replace/g Replace all occurrences of search with replace within the file

File Extension Primer

Extension Description
.tar.gz File was archived with tar and compressed with the gzip command. Use gunzip to uncompress it, then tar xvf filename.tar to extract it. (If gunzip is not in your path, you may find it in /vol.nsm/tools/bin)
.gz File was compressed with the gzip command. Use gunzip to uncompress it.
.Z File was compressed with the compress command. Use uncompress to uncompress it.
.zip File was compressed with the zip command. Use unzip to uncompress it.

You can use the UNIX command file to attempt to determine the file type.

Example:

$ file /home/hutchib/long_cmd.sh
/home/hutchib/long_cmd.sh: executable shell script

$ file /usr/bin/ls
/usr/bin/ls: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped

How do I redirect command output?

Generally, output of your UNIX commands is sent to standard output (STDOUT). You may redirect command output to a file by using:

command > file

If file does not exist, it will be created. If file already exists, it will be overwritten. You could instead append to the end of file by using:

command >> file

In addition to standard output, UNIX has a mechanism called standard error (STDERR). It is sometimes useful to discard standard error when performing commands. For example, when executing a find command on a UNIX system, you normally don't want to see permission denied messages when find attempts to read directories you don't have access to. The following command would redirect standard error to /dev/null, effectively discarding the output:

command 2>/dev/null

If you absolutely do not want the command to generate any output (e.g. a crontab entry), use the following command to send both standard output and standard error to /dev/null:

command >/dev/null 2>&1

How do I put a process in the background?

$ long_cmd.sh
Z[1] + Stopped (SIGTSTP) long_cmd.sh

$ bg %1
[1] long_cmd.sh&

$ jobs
[1] + Running long_cmd.sh

$ fg %1
long_cmd.sh

Execute a shell script called long_cmd.sh, which will take several minutes to complete. Since no interaction with the terminal can occur while this script is executing, press Ctrl-Z to suspend the script. After suspending the script, put it in the background with the bg %1 command. List the background processes with the jobs command. The background process will continue to execute and will complete; the shell will notify you when the job has completed. For this example, you can put the job back in the foreground with the fg %1 command.