A handful of useful Unix commands


The following are a collection of commonly used Unix commands, which students have found useful when dealing with a Unix system or server. This is not a complete list of the commands, nor is it designed to comprehensively cover each command. Rather, it will allow the new Unix user to function in a Unix environment as they learn their way around. (Optional usage is shown in square brackets [] .)


Command

Function

Example

Explanation

ls

List the files in the current directory.

ls

Show me the files in the directory I am currently in.

cat

Display the contents of a whole file.

cat myfile

Show me all of the lines in "myfile", if myfile is longer than 1 screen I will only see the last 20 lines or so.

more

Display the contents of a file page by page.

more myfile

Show me the first page of myfile. To see the next lines touch space bar or the Enter key. To quit type q.

head

Display the first 10 lines of a file.

head myfile

[head -20 myfile]

Let me see the first 10 lines of myfile.

[Show me the first 20 lines of myfile]

tail

Display the last 10 lines of a file.

tail myfile

[tail -20 myfile]

Show me the last 10 lines of myfile.

[Like head, show 20 lines.]

grep

Find a string in a file or files.

grep 'stuff' afile

Display all the lines in afile that contain the string "stuff", including lines that have words like stuffy or stuffing. Grep is a very powerful command.

file

Ask the system to guess what a file contains.

file myfile

The system will guess about the contents of a file. If it tells you that the file is a binary, you shouldn't try to display it.

cd

Change to a different directory

cd mydir

[cd ..]

Change to the directory "mydir" which is a subdirectory of the current directory.

[change to the next higher directory in the tree. The ".." notation is an abbreviation.]

mkdir

Create a new directory.

mkdir mydir

Create a directory called mydir from the current directory

pwd

Show me the directory I am currently working in.

pwd

Shows the current directory. Useful if you want to change to a different directory.

cal

Display a calendar of the current month, or year.

cal

[cal 2002]

Display the current month.

[Output a calendar for the year 2002.]

date

Display the current system date and time.

date

Show me what day and time it is on the Unix machine I am connected to.

who

Show me who is logged onto the system and where they are logged in from.

who

Shows me the people currently logged onto the system. Useful if I want to talk with them. Shows the login id, not their real name.

w

Tell me who is logged on and what they are doing.

w

A more comprehensive display than who. Shows the login id, as well as what they are doing and how long they have been idle.

finger

Display information about one or more users.

finger jl26

Displays what the system knows about a user, in this case jl26. Important because it gives the user's real name.

talk

Establish a 2-way connection between you and another user.

talk jl26

Sets up a two way, online chat session with the user. Both users must be using a recognized terminal type. Exit with ^C.

write

Send a message to another user.

write jl26

Send a message to a user. In this case jl26. User, (jl26), must be logged into the system for this to work. End message with ^D.

wall

Send a message to everyone logged onto the system

wall

Send a message to everyone logged into the system. This should only be used in extremes.

mesg

Disable, or enable talking or getting write messages.

mesg n

[mesg y]

You can stop getting messages, and disable talk by turning off (n) messages. Use y to turn them back on.

stty

Set up terminal functions and values

stty erase ^H

This sets the ^H (backspace) key as the erase key. Use stty with great caution, you can cause your terminal to stop communicating. stty -a shows all the options. BE CAREFUL!

man

Invoke the online manual

man man

Display the online manual page for the "man" command. This is a great tool to see what commands do, and how to use them.

wc

Count the number of lines, words, and characters in a file.

wc myfile

Shows the number of lines, words, and characters, (in that order), in myfile.

exit

Leave the Unix machine.

exit

This will log you off.