UNIX In 90 Minutes

By Brandon Hutchinson (brandonhutchinson@hotmail.com)

Modified by Tivon Luker to more suit our environment.


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 processes 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 file1 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 a tarfile

Extract contents of a 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

Command

Usage

Description

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 the directory /home/hutchib, 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,” meaning 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).


The next three characters (characters 2 through 4) correspond to user permissions, or permissions corresponding to the owner of the directory. They are “rwx,” meaning 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. If you are not the owner of the directory, but are a member of the group “nsm,” these permissions apply to you. They are “r-x,” meaning 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?


One important thing to remember is that 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.

The following is an example of a long listing of a file:


-rwxr-xr-x 1 hutchib nsm 36 Jun 4 10:13 long_cmd.sh


The first character is a “-,” meaning that we are looking at an ordinary file (i.e. a textfile, binary, etc.). The file is readable and executable by user, group, and other, and is user-writable—the owner of the file, hutchib, can write to the file.


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

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


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


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

Useful “vi” commands

The “vi” editor has two modes: “Input” and “Command” mode. 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 otherwise 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 may also 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:

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”:

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


In the above example, I executed a shell script called “long_cmd.sh” that would take several minutes to complete. I cannot interact with the terminal while this script is executing, so I pressed “Ctrl-Z” to suspend the script. After suspending the script, I put it in the background with the “bg %1” command. I list the background processes with the “jobs” command. The background process would have continued to execute and would have completed; the shell will notify you when the job has completed. For this example, I put the job back in the foreground with the “fg %1” command.