Vous êtes sur la page 1sur 12

Ten ESSENTIAL UNIX Commands

These are ten commands that you really need to know in order to get started with UNIX.
They are probably similar to commands you already know for another operating system.

Command Example Description

1. ls ls Lists files in current directory


ls -alF List in long format

2. cd cd tempdir Change directory to tempdir


cd .. Move back one directory
cd ~dhyatt/web-docs Move into dhyatt's web-docs directory

3. mkdir mkdir graphics Make a directory called graphics

4. rmdir rmdir emptydir Remove directory (must be empty)

5. cp cp file1 web-docs Copy file into directory


cp file1 file1.bak Make backup of file1

6. rm rm file1.bak Remove or delete file


rm *.tmp Remove all file

7. mv mv old.html new.html Move or rename files

8. more more index.html Look at file, one page at a time

9. lpr lpr index.html Send file to printer

10. man man ls Online manual (help) about command

Ten VALUABLE UNIX Commands


Once you have mastered the basic UNIX commands, these will be quite valuable in
managing your own account.

Command Example Description

1. grep grep "bad word" * Find which files contain a


<str><files> certain word

2. chmod <opt> chmod 644 *.html Change file permissions read


<file> chmod 755 file.exe only
Change file permissions to
executable
3. passwd passwd Change passwd

4. ps <opt> ps aux List all running processes by


ps aux | grep dhyatt #ID
List process #ID's running by
dhyatt

5. kill <opt> <ID> kill -9 8453 Kill process with ID #8453

6. gcc (g++) gcc file.c -o file Compile a program written in


<source> g++ fil2.cpp -o fil2 C
Compile a program written in
C++

7. gzip <file> gzip bigfile Compress file


gunzip bigfile.gz Uncompress file

8. mail mail me@tjhsst.edu < Send file1 by email to


(pine) file1 someone
pine Read mail using pine

9. telnet <host> telnet Open a connection to vortex


ssh <host> vortex.tjhsst.edu Open a secure connection to
ssh -l dhyatt jazz as user dhyatt
jazz.tjhsst.edu

10. ftp <host> ftp station1.tjhsst.edu Upload or Download files to


ncftp ncftp metalab.unc.edu station1
<host/directory> Connect to archives at UNC

Ten FUN UNIX Commands


These are ten commands that you might find interesting or amusing. They are actually quite
helpful at times, and should not be considered idle entertainment.

Command Example Description

1. who who Lists who is logged on your machine

2. finger finger Lists who is on computers in the lab

3. ytalk ytalk Talk online with dhyatt who is on


<user@place> dhyatt@threat threat

4. history history Lists commands you've done recently

5. fortune fortune Print random humerous message


6. date date Print out current date

7. cal <mo> cal 9 2000 Print calendar for September 2000


<yr>

8. xeyes xeyes & Keep track of cursor (in


"background")

9. xcalc xcalc & Calculator ("background" process)

10. mpage <opt> mpage -8 file1 Print 8 pages on a single sheet and
<file> | lpr send to printer (the font will be
small!)

Ten HELPFUL UNIX Commands


These ten commands are very helpful, especially with graphics and word processing type
applications.

Command Example Description

1. netscape netscape & Run Netscape browser

2. xv xv & Run graphics file converter

3. xfig / xpaint xfig & (xpaint Run drawing program


&)

4. gimp gimp & Run photoshop type program

5. ispell <fname> ispell file1 Spell check file1

6. latex <fname> latex file.tex Run LaTeX, a scientific


document tool

7. xemacs / pico xemacs (or Different editors


pico)

8. soffice soffice & Run StarOffice, a full word


processor

9. m-tools (mdir, mdir a: DOS commands from UNIX (dir


mcopy, mcopy file1 a: A:)
mdel, mformat, Copy file1 to A:
etc. )

10. gnuplot gnuplot Plot data graphically


Ten USEFUL UNIX Commands:
These ten commands are useful for monitoring system access, or simplifying your own
environment.

Command Example Description

1. df df See how much free disk space

2. du du -b subdir Estimate disk usage of directory in


Bytes

3. alias alias lls="ls -alF" Create new command "lls" for long
format of ls

4. xhost xhost + Permit window to display from x-


threat.tjhsst.edu window program from threat
xhost - Allow no x-window access from
other systems

5. fold fold -s file1 | lpr Fold or break long lines at 60


characters and send to printer

6. tar tar -cf subdir.tar Create an archive called subdir.tar


subdir of a directory
tar -xvf subdir.tar Extract files from an archive file

7. ghostview gv filename.ps View a Postscript file


(gv)

8. ping ping threat.tjhsst.edu See if machine is alive


(traceroute) traceroute Print data path to a machine
www.yahoo.com

9. top top Print system usage and top resource


hogs

10. logout logout or exit How to quit a UNIX shell.


(exit)

Unix Commands
Command/Syntax What it will do
awk/nawk [options] file scan for patterns in a file and process the results
cat [options] file concatenate (list) a file
cd [directory] change directory
chgrp [options] group file change the group of the file
chmod [options] file change file or directory access permissions
chown [options] owner file change the ownership of a file; can only be done by the superuser
chsh (passwd -e/-s)
change the user's login shell (often only by the superuser)
username login_shell
compare two files and list where differences occur (text or binary
cmp [options] file1 file2
files)
compress [options] file compress file and save it as file.Z
copy file1 into file2; file2 shouldn't already exist. This command
cp [options] file1 file2
creates or overwrites file2.
cut (options) [file(s)] cut specified field(s)/character(s) from lines in file(s)
date [options] report the current date and time
dd [if=infile] [of=outfile] copy a file, converting between ASCII and EBCDIC or swapping
[operand=value] byte order, as specified
diff [options] file1 file2 compare the two files and display the differences (text files only)
df [options] [resource] report the summary of disk blocks and inodes free and in use
du [options] [directory or
report amount of disk space in use
file]
echo [text string] echo the text string to stdout
ed or ex [options] file Unix line editors
emacs [options] file full-screen editor
expr arguments evaluate the arguments. Used to do arithmetic, etc. in the shell.
file [options] file classify the file type
find directory [options]
find files matching a type or pattern
[actions]
finger [options]
report information about users on local and remote machines
user[@hostname]
ftp [options] host transfer file(s) using file transfer protocol
grep [options] 'search string'
argument
egrep [options] 'search search the argument (in this case probably a file) for all
string' argument occurrences of the search string, and list them.
fgrep [options] 'search
string' argument
gzip [options] file
gunzip [options] file compress or uncompress a file. Compressed files are stored with
a .gz ending
zcat [options] file
head [-number] file display the first 10 (or number of) lines of a file
hostname display or set (super-user only) the name of the current machine
kill [options] [-SIGNAL] send a signal to the process with the process id number (pid#) or
[pid#] [%job] job control number (%n). The default signal is to kill the process.
ln [options] source_file
link the source_file to the target
target
lpq [options] show the status of print jobs
lpstat [options]
lpr [options] file
print to defined printer
lp [options] file
lprm [options]
remove a print job from the print queue
cancel [options]
ls [options] [directory or
list directory contents or file permissions
file]
mail [options] [user]
simple email utility available on Unix systems. Type a period as
mailx [options] [user] the first character on a new line to send message out, question
Mail [options] [user] mark for help.
man [options] command show the manual (man) page for a command
mkdir [options] directory make a directory
more [options] file
less [options] file page through a text file
pg [options] file
mv [options] file1 file2 move file1 into file2
octal dump a binary file, in octal, ASCII, hex, decimal, or
od [options] file
character mode.
passwd [options] set or change your password
paste [options] file paste field(s) onto the lines in file
pr [options] file filter the file and print it on the terminal
ps [options] show status of active processes
pwd print working (current) directory
rcp [options] hostname remotely copy files from this machine to another machine
rlogin [options] hostname login remotely to another machine
remove (delete) a file or directory (-r recursively deletes the
rm [options] file
directory and its contents) (-i prompts before removing files)
rmdir [options] directory remove a directory
rsh [options] hostname remote shell to run on another machine
saves everything that appears on the screen to file until exit is
script file
executed
stream editor for editing files from a script or from the command
sed [options] file
line
sort [options] file sort the lines of the file according to the options chosen
source file read commands from the file and execute them in the current
. file shell. source: C shell, .: Bourne shell.
report any sequence of 4 or more printable characters ending in
strings [options] file <NL> or <NULL>. Usually used to search binary files for ASCII
strings.
stty [options] set or display terminal control options
tail [options] file display the last few lines (or parts) of a file
tar key[options] [file(s)] tape archiver--refer to man pages for details on creating, listing,
and retrieving from archive files. Tar files can be stored on tape
or disk.
tee [options] file copy stdout to one or more files
telnet [host [port]] communicate with another host using telnet protocol
touch [options] [date] file create an empty file, or update the access time of an existing file
translate the characters in string1 from stdin into those in string2
tr [options] string1 string2
in stdout
uncompress file.Z uncompress file.Z and save it as a file
uniq [options] file remove repeated lines in a file
uudecode [file] decode a uuencoded file, recreating the original file
encode binary file to 7-bit ASCII, useful when sending via email,
uuencode [file] new_name
to be decoded as new_name at destination
vi [options] file visual, full-screen editor
wc [options] [file(s)] display word (or character or line) count for file(s)
report the binary, source, and man page locations for the
whereis [options] command
command named
which command reports the path to the command or the shell alias in use
who or w report who is logged in and what processes are running
concatenate (list) uncompressed file to screen, leaving file
zcat file.Z
compressed on disk

Some Useful UNIX Files and Commands


by D.W. Hyatt
Helpful Files
The following files may be useful when trying to write your PVM programs in this
class.

• The online UNIX Dictionary of over 40,000 words:


• /usr/dict/words The UNIX dictionary

• Some useful system files in the directory /etc or /usr/games/lib/fortune:


• /etc/passwd The password file
• /etc/HOSTNAME The name of the computer
• /etc/issue The logon banner
• /usr/games/lib/fortunes/fortune The source for UNIX fortunes

Some Useful Commands

Word Count, or "wc"


A helpful UNIX command is the "word count" program that can count how
many words are in the file.
wc -w <filename> counts the number of words in a file
wc -l <filename> counts lines in a file.

Grab Regular Expression, or "grep"


Another helpful command is "grep" for grabbing lines from a file that contain a
specific string pattern, or regular expression. The command grep <string> <files>
looks through a list of files and finds lines that contain the specific word in the string
argument.

grep pvm_pack *.cpp will look for occurrences of the string "pvm_pack" in all
files ending in ".cpp".
grep "My name is" * will look in all files in a directory trying to find the
string "My name is".

Input / Output Redirection


The UNIX operating system has a number of useful tools for allowing other
programs to work with one another. One of the ways to handle screen
input and output with I/O Redirection, and ways to link several programs
together with "pipes".

With the use of the > for sending output to a file, a user can easily covert from screen
display programs to ones that save the output without major changes in rewriting
code. It is also very convenien for grabbing the output from various UNIX
commands, too.
myprogram > myoutfile
This takes the output of "myprogram" and sends it a file called
"myoutfile".

ls -alF > filelist


This runs the command "ls", but saves the directory listing to a file rather than
displaying it on the screen.
In order to convert a program that originally required lots of user input into one that
runs on its own, the input redirection symbol < can be used to say where to get the
values.

program2 < myinput


This runs "program2" but takes any keyboard input from the file
"myinput". It is important the input values are in the proper sequence in
the file "myinput" since there will not be ways to reply to prompts at the
console.

Pipes
The vertical bar "|" is called the pipe symbol, and it is designed for linking
commands together to make them more powerful. The way it works is that
the output from one command is sent as input to the next, thus creating a
new command.

ls -alF | grep ".cpp"


This will list all files in a directory, and will then grab the names of only the
ones that contain the string ".cpp" in the name, or the C++ source files.

The system() command in C


The system() command is actually a C function that is very valuable for
accessing UNIX commands from within a C program. It can also be used to
run other programs you have already written. Be careful with extensive
use of this command because according to the online manual pages
(man), there are a few bugs such as not being able to break out of infinite
loops because interrupts are not processed, and some other security
issues. For many of the things we will be doing in this class, though, this
command will be quite useful.

system( "ls - alF");


This will run the "ls" command from within a C program and display the
results to the screen.

system ( "ps -aux | grep dhyatt > outfile");


This will run the "ps" command, the will send that output to "grep" which
will look for occurrences of "dhyatt", and finally will print the results to a
file called "outfile" rather than displaying anything on the screen.

Getting Started with vi


by Donald Hyatt
What is vi?
The fundamental UNIX editor is called vi. It is a primitive editor, but it will be
found on every UNIX system, and works well when logging in remotely from
home. There are just a few commands that one needs to learn at first, and they
will be sufficient to write simple files. For more advanced features, consult some
of the handouts and books available in the CS lab.

The Basics of vi
There are essentially three Modes in vi, the Insert Mode where one is typing in
new text, the Edit Mode where one is modifying text that is already there, and
the Command Mode where one is interacting with the operating system with
actions like reading and writing files.
Most of the commands require just a simple letter or two, but be careful to note that vi is case
sensitive, and capital letters do different things from lower case letters. It is not necessary to
learn all of these commands, but eventually they will become second nature. The basic
operations needed to create a file are related to each of those modes, so let's look at them
separately.
• INSERT Mode - For adding text to a file
The three most common ways to enter the Insert Mode are:

Letter Action

i Starts inserting in front of the current cursor position

I Starts adding at the front of the line

a Starts adding after the cursor

A Starts adding at the end of the line

o Starts opening a new line underneath the cursor

O Starts opening a line above the cursor.

<Esc> Gets out of Insert Mode

• EDIT Mode
- Generally for moving the cursor and deleting stuff.
In the Edit Mode, the keys do not type letters, but do other actions such as
cursor movement, deletions, copying lines, etc.

Letter Simple Cursor Movement

h Moves cursor left one space Note: the Arrow keys


do work locally, but
sometimes mess up
j Moves cursor down one line
over a network.

k Moves cursor up one line


l Moves cursor right one space

Fast Cursor Movement

w Moves the cursor a full word at a time to the right

b Moves the cursor back to the left a word at a time

^ Moves the cursor to the front of a line

$ Moves the cursor to the end of a line

<ctrl>f Moves the cursor forward a full page of text at a time

<ctrl>b Moves the cursor backward a full page of text at a time

Modifying Text

x Deletes the character under the cursor

Deletes the line where the cursor is located (type d


dd
twice!)

n dd Delete n consecutive lines ( n is an integer)

Replaces the character under the cursor with the next


r
thing typed

J Joins current line with the one below (Capital J!)

u Undoes the last edit operation

<ctrl> r Redo (Undoes the last undo operation)

Cut and Paste Operations


yy Copies or yanks a line ( 5yy yanks 5 lines)

Puts the yanked text on the line below the cursor (lower
p
case p)

P Puts the yanked text above the current line (capital P)

Note: If vi is already in the input mode, text from that or another


window may be highlited using the left mouse button, and copied into
place by pressing the middle mouse button.

• COMMAND Mode - For interacting with the operating system.


To enter the Command Mode, a colon " : " must precede the actual
command.

Letter Action

: r <file> reads a file from disk into the vi editor

: w <file> writes current file to disk

: wq writes the file and quits vi

: q! quits without writing (useful if you've messed up!)

Vous aimerez peut-être aussi