Monday, August 16, 2010
SPARC Enterprise M9000
Wednesday, July 21, 2010
Understanding UNIX permissions and chmod
Introduction
This is a topic that has been beaten to death both in books and on-line. For some reason, it seems that it is one of the most common misunderstandings that people have to face when learning how to write and/or configure their first cgi programs. This tutorial aims to clarify the concepts involved. Note that we will be referring to UNIX in a generic sense in this article. Most of what we are going to discuss here applies to all UNIX flavours. (such as Linux, SVR4, BSD etc.) It is also a good idea to type man chmod to check for the specific details on your system, too.
Users
A UNIX system serves many users. Users are an abstraction that denotes a logical entity for assignment of ownership and operation privileges over the system. A user may correspond to a real-world person, but also a type of system operation. So, in my system, I have user 'nick' that corresponds to me, but I also have user 'www' which corresponds to the privileges necessary to operate the local webserver. UNIX doesn't care about what the user means for me. It just knows what belongs to any given user and what each user is allowed to do with any given thing (file, program, device, etc) on the system. UNIX identifies each user by a User ID (UID) and the username (or login) such as 'nick' and 'www' is just an alias to the UID that makes humans more comfortable.
Groups
Users can be organized in groups. A user may belong to one or more groups of users. The concept of groups serves the purpose of assigning sets of privileges for a given resource and sharing them among many users that need to have them. (perhaps because they are all members of a project working team and they all need access to some common project files) So, on my system user 'nick' and user 'www' both belong to the group 'perlfect'. This way, they can have some shared privileges over the files for this site. User 'nick' needs them to edit the site, and user 'www' needs them to manage the webserver that will be publishing the site.
Ownership
Every file in UNIX has an owner user and an owner group. So, for any file in the system, user 'nick' may have one of the following ownership relations:
nick owns the file, i.e. the file's owner is 'nick'.
nick is a member of the group that owns the file, i.e. the file's owner group is 'perlfect'.
nick is neither the owner, nor belonging to the group that owns the file
Permissions
Every file on the system has associated with it a set of permissions. Permissions tell UNIX what can be done with that file and by whom. There are three things you can (or can't) do with a given file:
read it,
write (modify) it and
execute it.
Unix permissions specify which of the above operations can be performed for any ownership relation with respect to the file. In simpler terms, what can the owner do, what can the owner group do, and what can everybody else do with the file. For any given ownership relation, we need three bits to specify access permissions: the first to denote read (r) access, the second to denote (w) access and the third to denote execute (x) access. We have three ownership relations: 'owner', 'group' and 'all' so we need a triplet for each, resulting in nine bits. Each bit can be set or clear. (not set) We mark a set bit by it's corresponding operation letter (r, w or x) and a clear bit by a dash (-) and put them all on a row. An example might be rwxr-xr-x.What this means is that the owner can do anything with the file, but group owners and the rest of the world can only read or execute it. Usually in UNIX there is also another bit that precedes this 9-bit pattern. You do not need to know about it, at least for the time being.
So if you try ls -l on the command prompt you will get something like the following:
[nick@thekla src]$ ls -l
-rwxr-xr-x 1 nick users 382 Jan 19 11:49 bscoped.pl
drwxr-xr-x 3 nick users 1024 Jan 19 11:19 lib/
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl
The first column here shows the permission bit pattern for each file. The third column shows the owner, and the fourth column shows the owner group. By the time, the information provided by ls -l should be enough for you to figure out what each user of the system can do with any of the files in the directory.
Directories
Another interesting thing to note is that lib/ which is a directory has permissions, too. Permissions take a different meaning for directories. Here's what they mean:
read determines if a user can view the directory's contents, i.e. do ls in it.
write determines if a user can create new files or delete file in the directory. (Note here that this essentially means that a user with write access toa directory can delete files in the directory even if he/she doesn't have write permissions for the file! So be careful with this.)
execute determines if the user can cd into the directory.
chmod
To set/modify a file's permissions you need to use the chmod program. Of course, only the owner of a file may use chmod to alter a file's permissions. chmod has the following syntax: chmod [options] mode file(s)
The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A mode specifies which user's permissions should be changed, and afterwards which access types should be changed. Let's say for example:
chmod a-x socktest.pl This means that the execute bit should be cleared (-) for all users. (owner, group and the rest of the world) The permissions start with a letter specifying what users should be affected by the change, this might be any of the following:
u the owner user
g the owner group
o others (neither u, nor g)
a all users
This is followed by a change instruction which consists of a +(set bit) or -(clear bit) and the letter corresponding to the bit that should be changed.
Let's see some examples:
$ ls -l socktest.pl
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod a-x socktest.pl
$ ls -l socktest.pl
-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod g+w socktest.pl
$ ls -l socktest.pl
-rw-rw-r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod ug+x socktest.pl
$ ls -l socktest.pl
-rwxrwxr-- 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod ug-wx socktest.pl
$ ls -l socktest.pl
-r--r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
Strange numbers...
You might have encountered things like chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode.
Every one of the three digits on the mode number corresponds to one of the three permission triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 4 for r, 2 for w, 1 for x. If the permission bit you add this value to the number of the permission triplet. If it is cleared, then you add nothing. (Some of you might notice that in fact, the number for a triplet is the octal value corresponding to the three-bit pattern - if you don't know what an octal value is, it doesn't really matter, just follow the intstructions) So if a file has rwxr-xr-x permissions we do the following calculation:
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755
So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means 'everyone has full access to this file'
This is a topic that has been beaten to death both in books and on-line. For some reason, it seems that it is one of the most common misunderstandings that people have to face when learning how to write and/or configure their first cgi programs. This tutorial aims to clarify the concepts involved. Note that we will be referring to UNIX in a generic sense in this article. Most of what we are going to discuss here applies to all UNIX flavours. (such as Linux, SVR4, BSD etc.) It is also a good idea to type man chmod to check for the specific details on your system, too.
Users
A UNIX system serves many users. Users are an abstraction that denotes a logical entity for assignment of ownership and operation privileges over the system. A user may correspond to a real-world person, but also a type of system operation. So, in my system, I have user 'nick' that corresponds to me, but I also have user 'www' which corresponds to the privileges necessary to operate the local webserver. UNIX doesn't care about what the user means for me. It just knows what belongs to any given user and what each user is allowed to do with any given thing (file, program, device, etc) on the system. UNIX identifies each user by a User ID (UID) and the username (or login) such as 'nick' and 'www' is just an alias to the UID that makes humans more comfortable.
Groups
Users can be organized in groups. A user may belong to one or more groups of users. The concept of groups serves the purpose of assigning sets of privileges for a given resource and sharing them among many users that need to have them. (perhaps because they are all members of a project working team and they all need access to some common project files) So, on my system user 'nick' and user 'www' both belong to the group 'perlfect'. This way, they can have some shared privileges over the files for this site. User 'nick' needs them to edit the site, and user 'www' needs them to manage the webserver that will be publishing the site.
Ownership
Every file in UNIX has an owner user and an owner group. So, for any file in the system, user 'nick' may have one of the following ownership relations:
nick owns the file, i.e. the file's owner is 'nick'.
nick is a member of the group that owns the file, i.e. the file's owner group is 'perlfect'.
nick is neither the owner, nor belonging to the group that owns the file
Permissions
Every file on the system has associated with it a set of permissions. Permissions tell UNIX what can be done with that file and by whom. There are three things you can (or can't) do with a given file:
read it,
write (modify) it and
execute it.
Unix permissions specify which of the above operations can be performed for any ownership relation with respect to the file. In simpler terms, what can the owner do, what can the owner group do, and what can everybody else do with the file. For any given ownership relation, we need three bits to specify access permissions: the first to denote read (r) access, the second to denote (w) access and the third to denote execute (x) access. We have three ownership relations: 'owner', 'group' and 'all' so we need a triplet for each, resulting in nine bits. Each bit can be set or clear. (not set) We mark a set bit by it's corresponding operation letter (r, w or x) and a clear bit by a dash (-) and put them all on a row. An example might be rwxr-xr-x.What this means is that the owner can do anything with the file, but group owners and the rest of the world can only read or execute it. Usually in UNIX there is also another bit that precedes this 9-bit pattern. You do not need to know about it, at least for the time being.
So if you try ls -l on the command prompt you will get something like the following:
[nick@thekla src]$ ls -l
-rwxr-xr-x 1 nick users 382 Jan 19 11:49 bscoped.pl
drwxr-xr-x 3 nick users 1024 Jan 19 11:19 lib/
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl
The first column here shows the permission bit pattern for each file. The third column shows the owner, and the fourth column shows the owner group. By the time, the information provided by ls -l should be enough for you to figure out what each user of the system can do with any of the files in the directory.
Directories
Another interesting thing to note is that lib/ which is a directory has permissions, too. Permissions take a different meaning for directories. Here's what they mean:
read determines if a user can view the directory's contents, i.e. do ls in it.
write determines if a user can create new files or delete file in the directory. (Note here that this essentially means that a user with write access toa directory can delete files in the directory even if he/she doesn't have write permissions for the file! So be careful with this.)
execute determines if the user can cd into the directory.
chmod
To set/modify a file's permissions you need to use the chmod program. Of course, only the owner of a file may use chmod to alter a file's permissions. chmod has the following syntax: chmod [options] mode file(s)
The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A mode specifies which user's permissions should be changed, and afterwards which access types should be changed. Let's say for example:
chmod a-x socktest.pl This means that the execute bit should be cleared (-) for all users. (owner, group and the rest of the world) The permissions start with a letter specifying what users should be affected by the change, this might be any of the following:
u the owner user
g the owner group
o others (neither u, nor g)
a all users
This is followed by a change instruction which consists of a +(set bit) or -(clear bit) and the letter corresponding to the bit that should be changed.
Let's see some examples:
$ ls -l socktest.pl
-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod a-x socktest.pl
$ ls -l socktest.pl
-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod g+w socktest.pl
$ ls -l socktest.pl
-rw-rw-r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
$ chmod ug+x socktest.pl
$ ls -l socktest.pl
-rwxrwxr-- 1 nick users 1874 Jan 19 10:23 socktest.pl*
$ chmod ug-wx socktest.pl
$ ls -l socktest.pl
-r--r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl
Strange numbers...
You might have encountered things like chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode.
Every one of the three digits on the mode number corresponds to one of the three permission triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 4 for r, 2 for w, 1 for x. If the permission bit you add this value to the number of the permission triplet. If it is cleared, then you add nothing. (Some of you might notice that in fact, the number for a triplet is the octal value corresponding to the three-bit pattern - if you don't know what an octal value is, it doesn't really matter, just follow the intstructions) So if a file has rwxr-xr-x permissions we do the following calculation:
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755
So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means 'everyone has full access to this file'
Thursday, May 27, 2010
A quick reference list of vi editor commands
Using the viEditor
Objectives
This module introduces the vi editor and describes the vi commands.
These commands include the input commands, the positioning
commands, and the editing commands.
Upon completion of this module, you should be able to:
l Describe the fundamentals of the vi editor
l Modify files by using the vi editor
The following course map shows how this module fits into the current
instructional goal.
Fundamentals of the vi Editor
The visual display or vi editor is an interactive editor that you can use to
create and modify text files. You can use the vi editor when the desktop
environment window system is not available. The vi editor is also the
only text editor that you can use to edit certain system files without
changing the permissions of the files.
All text editing with the vi editor takes place in a buffer. You can either
write the changes to the disk, or discard them.
The vi Editor Modes of Operation
The vi editor is a command-line editor that has three basic modes of
operation:
l Command mode
l Edit mode
l Last line mode
Command Mode
The command mode is the default mode for the vi editor. In this mode,
you can perform commands to delete, change, copy, and move text. You
can also position the cursor, search for text strings, and exit the vi editor.
Edit Mode
You can enter text into a file in the edit mode. The vi editor interprets
everything you type in the edit mode as text. To enter the edit mode,
perform the commands:
l i – Inserts text before the cursor
l o – Opens a new blank line below the cursor
l a – Appends text after the cursor
Last Line Mode
You can use advanced editing commands in the last line mode. To access
the last line mode, enter a colon (:) while in the command mode. The
colon places your cursor at the bottom line of the screen.
Switching Between the Command and Edit Modes
The default mode for the vi editor is the command mode. When you
perform an i, o, or a command, the vi editor switches to the edit mode.
After editing a file, press Escape to return the vi editor to the command
mode. When in the command mode, you can save the file and quit the
vi editor.
The following example shows how to switch modes in the vi editor:
1. Perform the vi filename command to create a file. You are
automatically in the command mode.
2. Type the i command to insert text. The i command switches the
vi editor to the edit mode.
3. Press Escape to return to the command mode.
4. Perform the :wq command to save the file and exit the vi editor.
Using the vi Command
The vi command enables you to create, edit, and view files in the
vi editor.
The syntax for the vi command is:
vi
vi filename
vi options filename
If the system crashes while you are editing a file, you can use the
-r option to recover the file.
To recover a file, perform the command:
$ vi -r filename
The file opens so that you can edit it. You can then save the file and exit
the vi editor.
$ vi -R filename
The file opens in read-only mode to prevent accidental overwriting of the
contents of the file.
Modifying Files With the vi Editor
You can use the vi editor to view files in the read-only mode, or you can
edit files in the vi editor using the vi editing commands. When using the
vi editor, you can move the cursor using certain key sequences.
Viewing Files in the Read-Only Mode
The view command enables you to view files in the read-only mode. It
invokes the vi editor with the read-only option. Although most of the vi
commands are available, you cannot save changes to the file.
The syntaxfor the view command is:
view filename
To view the dante file in the read-only mode, perform the command:
$ cd
$ view dante
The dante file appears. Perform the :q command to exit the file and the
vi editor.
Inserting and Appending Text
Table 5-1 describes the commands that you can use to insert and append
text to a file using the vi editor. These commands switch the system to the
edit mode. To return to the command mode, press Escape.
Note – The vi editor is case sensitive. Use the appropriate case for the
input commands.
Also, most of the editing commands and cursor movement can be
preceded by a number to repeat the command that number of times.
Table 5-1 Input Commands for the vi Editor
Command Function
a Appends text after the cursor
A Appends text at the end of the line
i Inserts text before the cursor
I Inserts text at the beginning of the line
o Opens a new line below the cursor
O Opens a new line above the cursor
:r filename Inserts text from another file into the current file
Moving the Cursor Within the vi Editor
Table 5-2 shows the key sequences that move the cursor in the vi editor.
Table 5-2 Key Sequences for the vi Editor
Key Sequence Cursor Movement
h, left arrow, or
Backspace
Left one character
j or down arrow Down one line
k or up arrow Up one line
l, right arrow, or
space bar
Right (forward) one character
w Forward one word
b Back one word
e To the end of the current word
$ To the end of the line
0 (zero) To the beginning of the line
^ To the first non-white space character on the line
Return Down to the beginning of the next line
G Goes to the last line of the file
1G Goes to the first line of the file
:n Goes to Line n
nG Goes to Line n
Control-F Pages forward one screen
Control-D Scrolls down one-half screen
Control-B Pages back one screen
Control-U Scrolls up one-half screen
Control-L Refreshes the screen
Control-G Displays current buffer information
Editing Files by Using the vi Editing Commands
You can use numerous commands to edit files using the vi editor. The
following sections describe basic operations for deleting, changing,
replacing, copying, and pasting. Remember that the vi editor is case
sensitive.
Using the Text-Deletion Commands
Table 5-3 shows the commands that delete text in the vi editor.
Note – Output from the delete command writes to a buffer from which
text can be retrieved.
Table 5-3 Text-Deletion Commands for the vi Editor
Command Function
R Overwrites or replaces characters on the line at and to the
right of the cursor. To terminate this operation, press
Escape.
C Changes or overwrites characters from cursor to the end
of the line.
s Substitutes a string for a character at the cursor.
x Deletes a character at the cursor.
dw Deletes a word or part of the word to the right of the
cursor.
dd Deletes the line containing the cursor.
D Deletes the line from the cursor to the right end of the
line.
:n,nd Deletes Lines n –n (for example, :5,10d deletes
Lines 5–10).
Using the Text-Changing Commands
Table 5-4 describes the commands that you can use to change text, undo a
change, and repeat an edit function in the vi editor. Many of these
commands change the vi editor to edit mode. To return to command
mode, press Escape.
Table 5-4 Edit Commands for the vi Editor
Command Function
cw Changes or overwrites characters at the cursor location
to the end of that word
r Replaces the character at the cursor with one other
character
J Joins the current line and the line below
xp Transposes the character at the cursor and the character
to the right of the cursor
~ Changes the case of the letter, either uppercase or
lowercase, at the cursor
u Undoes the previous command
U Undoes all changes to the current line
. Repeats the previous command
Using the Text-Replacing Commands
Table 5-5 shows the commands that search for and replace text in the vi
editor.
Using the Text-Copying and Text-Pasting Commands
The yy command yanks lines of text and holds a copy of the lines in a
temporary buffer. The put commands (p, P) inserts the lines of text from
the temporary buffer and writes the text into the current document at the
specified location. The copy (co) and move (m) commands copy or move
specified lines to a requested location within the file.
Table 5-6 shows the commands that yank (yy) and put (p, P) text in the vi
editor.
Table 5-5 Search and Replace Commands
Command Function
/string Searches forward for the string.
?string Searches backward for the string.
n Searches for the next occurrence of the string. Use
this command after searching for a string.
N Searches for the previous occurrence of the string.
Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it with the
new string globally.
Table 5-6 Copy and Paste Commands
Command Function
yy Yanks a copy of a line
p Puts yanked or deleted text under the line containing
the cursor
P Puts yanked or deleted text before the line containing
the cursor
Using the File Save and Quit Commands
Table 5-8 describes the commands that save the file and quit the vi editor.
Table 5-7 Additional Copy and Paste Commands
Command Function
:n,n co n Copies Lines n –n and puts them after Line n. For
example, :1,3 co 5 copies Lines 1–3 and puts them
after Line 5.
:n,n m n Moves Lines n –n to Line n. For example, :4,6 m 8
moves Lines 4–6 to Line 8. Line 6 becomes Line 8.
Line 5 becomes Line 7. Line 4 becomes Line 6.
Table 5-8 Save and Quit Commands
Command Function
:w Saves the file with changes by writing to the disk
:w new_filename Writes the contents of the buffer to new_filename
:wq Saves the file with changes and quits the vi editor
:x Saves the file with changes and quits the vi editor
ZZ Saves the file with changes and quits the vi editor
:q! Quits without saving changes
Customizing a vi Session
You can customize a vi session by setting variables for the session. When
you set a vi variable, you enable a feature that is not activated by default.
You can use the set command to enable and disable variables.
Table 5-9 describes some of the variables of the set command, including
displaying line numbers and invisible characters, such as the Tab and the
end-of-line characters.
To create an automatic customization for all of your vi sessions, complete
the following steps:
1. Create a file in your home directory named .exrc.
2. Enter the set variables into the .exrc file.
3. Enter set variable without the preceding colon.
4. Perform one command on one line.
The vi editor reads the .exrc file located in your home directory each
time you open a vi session, regardless of your current working directory.
Table 5-9 Edit Session Customization Commands
Command Function
:set nu Shows line numbers
:set nonu Hides line numbers
:set ic Instructs searches to ignore case
:set noic Instructs searches to be case sensitive
:set list Displays invisible characters, such as ^I for a Tab
and $ for end-of-line characters
:set nolist Turns off the display of invisible characters
:set showmode Displays the current mode of operation
:set noshowmode Turns off the mode of operation display
:set Displays all the vi variables that are set
:set all Displays all set vi variables and their current
values
Exercise: Using the vi Editor
In this exercise, you practice performing vi editor commands in the
tutor.vi tutorial. Use Figure 5-3 on page 5-14 as a reference to complete
the exercise.
Preparation
No special preparation is required for this exercise.
RLCD
In addition to being able to use local classroom equipment, this lab was
designed to also use equipment located in a remote lab data center.
Directions for accessing and using this resource can be found at:
http://fn1.brom.suned.com/
Ask your instructor for the particular SSH configuration file that you
should use to access the appropriate remote equipment for this exercise.
Tasks
To learn how to use the vi editor, complete the following steps.
1. Make sure that you are in your home directory. To open the
tutor.vi tutorial file, perform the following command:
$ vi tutor.vi
2. Complete the lessons outlined in this tutorial.
Search Functions
/exp Go forward to exp
?exp Go backward to exp
Move and Insert Text
:3,8d Delete line 3-8
:4,9m 12 Move lines 4-9 to 12
:2,5t 13 Copy lines 2-5 to 13
:5,9w file Write lines 5-9 to file
Save Files and Exit
:w Write to disk
:w newfile Write to newfile
:w! file Write absolutely
:wq Write and quit
:q Quit editor
:q! Quit and discard
:e! Re-edit current file,
Discard buffer
Control Edit Session
:set nu Display line number
:set nonu Turn off line number
:set all Show all settings
:set list Display invisible
Characters
:set wm=5 Wrap lines 5 spaces
From right margin
Screen/Line Movement
j Move cursor down
k Move cursor up
h Move cursor left
l Move cursor right
0 Go to line start (zero)
$ Go to line end
G Go to last file line
Word Movement
w Go forward 1 word
b Go backward 1 word
Search Functions
n Repeat previous search
N Reverse previous search
Delete Text
x Delete 1 character
dw Delete 1 word
dd Delete 1 line
D Delete to end of line
d0 Delete to start of line
dG Delete to end of file
Cancel Edit Function
u Undo last change
. Do last change again
Copy and Insert Text
Y Yank a copy
5Y Yank a copy of 5 lines
p Put below cursor
P Put above cursor
How do I use the vi text editor?
The vi text editor has three modes: command mode, input mode, and ex mode.
Command mode
When starting, vi begins in command mode. If you are ever unsure which mode you're in, press Esc to return to command mode. In command mode, you can move around with the arrow keys, or by using the vi movement keys, as follows:
h left
j down
k up
l right
Several vi commands are listed in the table below:
Command Action
Ctrl-b Go back one page
Ctrl-f Go forward one page
x Delete the character the cursor is on
Shift-x Delete the character before the cursor
dd Delete the current line
Shift-d Delete everything from the cursor to the end of the line
u Undelete a line you just deleted
Shift-u Undo all changes to the current line
Shift-z-z Save your file and exit the vi editor
Note: In command mode, you can type a number before pressing a command key to repeat the command multiple times. For example, to delete eight lines from the cursor position, you could press 8 and then type dd .
Input mode
The input mode lets you insert or append text. To insert text before the cursor's current position, in command mode, press i . Similarly, to append after the cursor, you can type a . Remember that you can't move around with the cursor keys in this mode. When you're done entering text, press Esc to go back to command mode.
Ex mode
The ex mode is an extension of command mode. To get into it, press Esc and then : (the colon). The cursor will go to the bottom of the screen at a colon prompt. Write your file by entering :w and quit by entering :q . You can combine these to save and exit by entering :wq . However, if you're finished with your file, it's generally more convenient to type Shift-z-z from command mode.
Command mode
When starting, vi begins in command mode. If you are ever unsure which mode you're in, press Esc to return to command mode. In command mode, you can move around with the arrow keys, or by using the vi movement keys, as follows:
h left
j down
k up
l right
Several vi commands are listed in the table below:
Command Action
Ctrl-b Go back one page
Ctrl-f Go forward one page
x Delete the character the cursor is on
Shift-x Delete the character before the cursor
dd Delete the current line
Shift-d Delete everything from the cursor to the end of the line
u Undelete a line you just deleted
Shift-u Undo all changes to the current line
Shift-z-z Save your file and exit the vi editor
Note: In command mode, you can type a number before pressing a command key to repeat the command multiple times. For example, to delete eight lines from the cursor position, you could press 8 and then type dd .
Input mode
The input mode lets you insert or append text. To insert text before the cursor's current position, in command mode, press i . Similarly, to append after the cursor, you can type a . Remember that you can't move around with the cursor keys in this mode. When you're done entering text, press Esc to go back to command mode.
Ex mode
The ex mode is an extension of command mode. To get into it, press Esc and then : (the colon). The cursor will go to the bottom of the screen at a colon prompt. Write your file by entering :w and quit by entering :q . You can combine these to save and exit by entering :wq . However, if you're finished with your file, it's generally more convenient to type Shift-z-z from command mode.
In Unix, how do I determine my current working directory?
To find out what directory you are currently in, at the Unix prompt, enter:
pwd
Many Unix users find it useful to put the name of the current directory in the prompt.
pwd
Many Unix users find it useful to put the name of the current directory in the prompt.
In Unix, what do the output fields of the ps command mean?
The ps command varies significantly among Unix implementations. Each vendor incorporates its own flags and outputs the results differently. However, most ps variants are rooted enough in either the System V or BSD syntax that entering ps -elf (System V) or ps alx (BSD) will produce something like the following:
F S UID ID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME COMD
1 R obiwan 792 779 22 183 20 10ec5f80 29 - 12:52:24 pts/2 0:00 ps -elf
1 S root 24621 560 0 154 20 13603f80 11 4697c0 Jun 16 ttyp2 0:00 telnetd
1 S dvader 1162 1153 0 154 20 110a1f80 77 452be4 11:25:41 pts/3 0:00 ssh deathstar
This particular example is from HP-UX, whose output is basically vanilla System V. The following table describes the meanings of the columns that commonly appear in ps outputs. No version of ps will display all of these fields, however.
Column Header Contents
--------------------------------------------------------------------------------
%CPU How much of the CPU the process is using
%MEM How much memory the process is using
ADDR Memory address of the process
C or CP CPU usage and scheduling information
COMMAND* Name of the process, including arguments, if any
NI nice value
F Flags
PID Process ID number
PPID ID number of the process's parent process
PRI Priority of the process
RSS Real memory usage
S or STAT Process status code
START or STIME Time when the process started
SZ Virtual memory usage
TIME Total CPU usage
TT or TTY Terminal associated with the process
UID or USER Username of the process's owner
WCHAN Memory address of the event the process is waiting for
* = Often abbreviated
For information specific to your Unix implementation, consult the ps man page
F S UID ID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME COMD
1 R obiwan 792 779 22 183 20 10ec5f80 29 - 12:52:24 pts/2 0:00 ps -elf
1 S root 24621 560 0 154 20 13603f80 11 4697c0 Jun 16 ttyp2 0:00 telnetd
1 S dvader 1162 1153 0 154 20 110a1f80 77 452be4 11:25:41 pts/3 0:00 ssh deathstar
This particular example is from HP-UX, whose output is basically vanilla System V. The following table describes the meanings of the columns that commonly appear in ps outputs. No version of ps will display all of these fields, however.
Column Header Contents
--------------------------------------------------------------------------------
%CPU How much of the CPU the process is using
%MEM How much memory the process is using
ADDR Memory address of the process
C or CP CPU usage and scheduling information
COMMAND* Name of the process, including arguments, if any
NI nice value
F Flags
PID Process ID number
PPID ID number of the process's parent process
PRI Priority of the process
RSS Real memory usage
S or STAT Process status code
START or STIME Time when the process started
SZ Virtual memory usage
TIME Total CPU usage
TT or TTY Terminal associated with the process
UID or USER Username of the process's owner
WCHAN Memory address of the event the process is waiting for
* = Often abbreviated
For information specific to your Unix implementation, consult the ps man page
In Unix, what is the man command, and how do I use it to read manual pages?
In Unix, most programs, and many protocols, functions, and file formats, have accompanying manuals. With the man command, you can retrieve the information in the manual and display it as text output on your screen. To use the man command, at the Unix prompt, enter:
man topic
Replace topic with the name of the manual item about which you want more information. For example, to find out more about the FTP command, at the Unix prompt, enter:
man ftp If you are unsure which manual item you want to read, you can do a keyword search. At the Unix prompt, enter:
man -k keyword | more
On some systems, you need to replace man -k with apropos . For example, at the Unix prompt, enter:
apropos keyword | more
In both of the examples above, replace keyword with a specific topic (e.g., ftp , mail ).
For more information about the man command, at the Unix prompt, enter:
man man
man topic
Replace topic with the name of the manual item about which you want more information. For example, to find out more about the FTP command, at the Unix prompt, enter:
man ftp If you are unsure which manual item you want to read, you can do a keyword search. At the Unix prompt, enter:
man -k keyword | more
On some systems, you need to replace man -k with apropos . For example, at the Unix prompt, enter:
apropos keyword | more
In both of the examples above, replace keyword with a specific topic (e.g., ftp , mail ).
For more information about the man command, at the Unix prompt, enter:
man man
In Unix, how do I list the files in a directory?
You can use the ls command to list the files in any directory to which you have access. For a simple directory listing, at the Unix prompt, enter:
ls
This command will list the names of all the files and directories in the current working directory.
You can limit the files that are described by using fragments of filenames and wildcards. Examples of this are:
ls hello Lists files whose complete name is hello; if hello is a directory, displays the contents of the hello directory.
ls hel* Lists all files in the directory that begin with the characters hel (e.g., files named hel, hello, and hello.officer).
ls hell? Lists files that begin with hell followed by one character, such as helli, hello, and hell1.
The * represents any number of unknown characters, while ? represents only one unknown character. You can use * and ? anywhere in the filename fragment.
If you would like to list files in another directory, use the ls command along with the path to the directory. For example, if you are in your home directory and want to list the contents of the /etc directory, enter:
ls /etc
This will list the contents of the /etc directory in columns.
Several options control the way in which the information you get is displayed. Options are used in this format:
ls -option filename
Neither the options nor the filename are required (you may use ls by itself to see all the files in a directory). You may have multiple options and multiple filenames on a line.
The options available with ls are far too numerous to list here, but you can see them all in the online manual (man) pages.
Some of the more helpful options for ls are:
-a Shows all files, including those beginning with . (a period). The dot is special in the Unix file system.
-d Shows directory names, but not contents
-F Marks special files with symbols to indicate what they are: / for directories, @ for symbolic links, * for executable programs
-l Shows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (The l stands for "long".)
-R Recursively lists subdirectories
The options can be combined. To list all the files in a directory in the long format, with marks for the types of files, you would enter:
ls -Flg
As with many other Unix commands, you can redirect the output from ls to a file, or pipe it to another command. If you want to save a list of the files in your directory to a file named foo, you would use the following command combination:
ls > foo
If you want to mail a list of the files in your directory to a user named tom, you would use the following combination:
ls | Mail tom
For a more complete discussion of the ls command, see the online manual pages. At the Unix prompt, enter:
man ls
ls
This command will list the names of all the files and directories in the current working directory.
You can limit the files that are described by using fragments of filenames and wildcards. Examples of this are:
ls hello Lists files whose complete name is hello; if hello is a directory, displays the contents of the hello directory.
ls hel* Lists all files in the directory that begin with the characters hel (e.g., files named hel, hello, and hello.officer).
ls hell? Lists files that begin with hell followed by one character, such as helli, hello, and hell1.
The * represents any number of unknown characters, while ? represents only one unknown character. You can use * and ? anywhere in the filename fragment.
If you would like to list files in another directory, use the ls command along with the path to the directory. For example, if you are in your home directory and want to list the contents of the /etc directory, enter:
ls /etc
This will list the contents of the /etc directory in columns.
Several options control the way in which the information you get is displayed. Options are used in this format:
ls -option filename
Neither the options nor the filename are required (you may use ls by itself to see all the files in a directory). You may have multiple options and multiple filenames on a line.
The options available with ls are far too numerous to list here, but you can see them all in the online manual (man) pages.
Some of the more helpful options for ls are:
-a Shows all files, including those beginning with . (a period). The dot is special in the Unix file system.
-d Shows directory names, but not contents
-F Marks special files with symbols to indicate what they are: / for directories, @ for symbolic links, * for executable programs
-l Shows the rights to the file, the owner, the size in bytes, and the time of the last modification made to the file. (The l stands for "long".)
-R Recursively lists subdirectories
The options can be combined. To list all the files in a directory in the long format, with marks for the types of files, you would enter:
ls -Flg
As with many other Unix commands, you can redirect the output from ls to a file, or pipe it to another command. If you want to save a list of the files in your directory to a file named foo, you would use the following command combination:
ls > foo
If you want to mail a list of the files in your directory to a user named tom, you would use the following combination:
ls | Mail tom
For a more complete discussion of the ls command, see the online manual pages. At the Unix prompt, enter:
man ls
In Unix, how do I print files and list or remove print jobs?
The various implementations of Unix handle printing tasks in different ways. Most systems, however, understand either the BSD or System V commands, or in some cases, both. The UITS central servers at Indiana University, in particular, support both varieties. AIX, the operating system on the Research Database Complex (RDC), has its own print commands, but it also supports BSD and System V commands. This document will not cover AIX print commands.
The following sections explain how to do a number of printing tasks. Be sure to pay attention to what kind of printing commands (BSD or System V) your system uses. If your system uses both flavors of printing commands, experiment to see which commands work the best for you.
At IU, the laser printers in the Student Technology Centers and the Residential Technology Centers, as well as printers in offices, cannot print using normal Unix print commands. Instead, try the ansiprt command. For more, see In Unix, how do I print a file to my local printer?
Using BSD commands
Printing
To print a file from a BSD-compatible system, use the lpr command. The general syntax for BSD is:
lpr [switches] filename
lpr [switches]
Replace [switches] with optional command line switches. Switches allow you to specify certain options, such as a particular printer or output format. Replace filename with the name of the file you want to print.
For example, to print a file named myfile.txt to the default printer in a system that uses the BSD-compatible commands, at the Unix prompt, enter:
lpr myfile.txt
For information on designating a default printer, see In Unix, how do I designate my default printer?
To send a print job to a printer other than the default printer, you need to specify its name. With BSD commands, do this using the -P flag. For example, to print myfile.txt to a printer named ps99, enter:
lpr -Pps99 myfile.txt
Note: Do not include a space between the -P and the printer's name.
BSD also allows you to suppress the printing of a banner page with the -h option; to do this, enter:
lpr -Pps99 -h myfile.txt
You can also use the lpr command in a pipeline to print the output from other Unix commands. For example, to print out a long listing of the contents of the current directory with BSD, enter:
ls -la | lpr -Pps99 -h
For BSD, to see a listing of available printers and their names, read the file /etc/printcap. Public printers will often be tagged with their names.
Note: As configured on most systems, the lpr command takes text (straight ASCII) or PostScript input without requiring any switches. If you are printing a PostScript file, make sure that the printer you're using is PostScript compatible before you issue the command. On some BSD-compatible operating systems, lpr can also print .dvi files if you specify the -d switch, but this works inconsistently and UITS doesn't recommend it.
Listing print jobs
With BSD printing commands, use lpq to list the jobs (files waiting to be printed) for a particular printer. To find what jobs are queued on the default printer, at the Unix prompt, enter:
lpq
To check what jobs are queued to print on a printer named ps99, you would enter:
lpq -Pps99
The listing returned by lpq reports the rank, owner, job number, filename, and file size of each print job in the queue or actually printing.
Removing print jobs
With BSD, the lprm command lets you cancel the printing of a file. To remove a print job, first use the lpq command to find the job number of the print request you want to cancel. Then use the lprm command to kill the job by specifying the job number. For example, if you want to kill job number 11042 on printer ps99, enter:
lprm -Pps99 11042
Note: Unless you have system administration privileges, you can remove only print jobs that you have submitted, not jobs submitted by other users.
Using System V commands
Printing
To print in System V, use the lp command. The syntax is as follows:
lp [switches] filename
lp [switches] Replace [switches] with optional command line switches. Switches allow you to specify certain options, such as a particular printer or output format. Replace filename with the name of the file you want to print.
For example, to print a file named myfile.txt to the default printer in a system that uses System V commands, enter:
lp myfile.txt
For information on designating a default printer, see In Unix, how do I designate my default printer?
To send a print job to a printer other than the default printer, you need to specify its name. In System V, use the -d flag to do this. For example, to print myfile.txt to a printer named ps99, enter:
lp -d ps99 myfile.txt
You can also use the lp command in a pipeline to print the output from other Unix commands. For example, to print out a long listing of the contents of the current directory with System V, enter:
ls -la | lp -dps99
To see a list of printers available for the lp command, enter:
lpstat -a
Note: As configured on most systems, the lp command takes text (straight ASCII) or PostScript input without requiring any switches. If you are printing a PostScript file, make sure that the printer you're using is PostScript compatible before you issue the command.
Listing print jobs
With System V, use the lpstat command to list the jobs for a particular printer. Using lpstat prints the status of all your print requests made by lp to the default or designated printer. To see the status of print jobs you have sent to the queue of the default printer that are still waiting, enter:
lpstat
To check what jobs are pending for a particular printer, use the lpstat -P command. For example, to view the queue for printer ps99, enter:
lpstat -Pps99
Removing print jobs
With System V, use the cancel command to stop the printing of a file. To cancel a job, even if it's already printing, use the lpstat command to find the job number. Then use the cancel command to kill that job. For example, if you want to kill the job ps99-4, enter:
cancel ps99-4
Note: Unless you have system administration privileges, you can remove only print jobs that you have submitted, not jobs submitted by other users.
The following sections explain how to do a number of printing tasks. Be sure to pay attention to what kind of printing commands (BSD or System V) your system uses. If your system uses both flavors of printing commands, experiment to see which commands work the best for you.
At IU, the laser printers in the Student Technology Centers and the Residential Technology Centers, as well as printers in offices, cannot print using normal Unix print commands. Instead, try the ansiprt command. For more, see In Unix, how do I print a file to my local printer?
Using BSD commands
Printing
To print a file from a BSD-compatible system, use the lpr command. The general syntax for BSD is:
lpr [switches] filename
lpr [switches]
Replace [switches] with optional command line switches. Switches allow you to specify certain options, such as a particular printer or output format. Replace filename with the name of the file you want to print.
For example, to print a file named myfile.txt to the default printer in a system that uses the BSD-compatible commands, at the Unix prompt, enter:
lpr myfile.txt
For information on designating a default printer, see In Unix, how do I designate my default printer?
To send a print job to a printer other than the default printer, you need to specify its name. With BSD commands, do this using the -P flag. For example, to print myfile.txt to a printer named ps99, enter:
lpr -Pps99 myfile.txt
Note: Do not include a space between the -P and the printer's name.
BSD also allows you to suppress the printing of a banner page with the -h option; to do this, enter:
lpr -Pps99 -h myfile.txt
You can also use the lpr command in a pipeline to print the output from other Unix commands. For example, to print out a long listing of the contents of the current directory with BSD, enter:
ls -la | lpr -Pps99 -h
For BSD, to see a listing of available printers and their names, read the file /etc/printcap. Public printers will often be tagged with their names.
Note: As configured on most systems, the lpr command takes text (straight ASCII) or PostScript input without requiring any switches. If you are printing a PostScript file, make sure that the printer you're using is PostScript compatible before you issue the command. On some BSD-compatible operating systems, lpr can also print .dvi files if you specify the -d switch, but this works inconsistently and UITS doesn't recommend it.
Listing print jobs
With BSD printing commands, use lpq to list the jobs (files waiting to be printed) for a particular printer. To find what jobs are queued on the default printer, at the Unix prompt, enter:
lpq
To check what jobs are queued to print on a printer named ps99, you would enter:
lpq -Pps99
The listing returned by lpq reports the rank, owner, job number, filename, and file size of each print job in the queue or actually printing.
Removing print jobs
With BSD, the lprm command lets you cancel the printing of a file. To remove a print job, first use the lpq command to find the job number of the print request you want to cancel. Then use the lprm command to kill the job by specifying the job number. For example, if you want to kill job number 11042 on printer ps99, enter:
lprm -Pps99 11042
Note: Unless you have system administration privileges, you can remove only print jobs that you have submitted, not jobs submitted by other users.
Using System V commands
Printing
To print in System V, use the lp command. The syntax is as follows:
lp [switches] filename
lp [switches] Replace [switches] with optional command line switches. Switches allow you to specify certain options, such as a particular printer or output format. Replace filename with the name of the file you want to print.
For example, to print a file named myfile.txt to the default printer in a system that uses System V commands, enter:
lp myfile.txt
For information on designating a default printer, see In Unix, how do I designate my default printer?
To send a print job to a printer other than the default printer, you need to specify its name. In System V, use the -d flag to do this. For example, to print myfile.txt to a printer named ps99, enter:
lp -d ps99 myfile.txt
You can also use the lp command in a pipeline to print the output from other Unix commands. For example, to print out a long listing of the contents of the current directory with System V, enter:
ls -la | lp -dps99
To see a list of printers available for the lp command, enter:
lpstat -a
Note: As configured on most systems, the lp command takes text (straight ASCII) or PostScript input without requiring any switches. If you are printing a PostScript file, make sure that the printer you're using is PostScript compatible before you issue the command.
Listing print jobs
With System V, use the lpstat command to list the jobs for a particular printer. Using lpstat prints the status of all your print requests made by lp to the default or designated printer. To see the status of print jobs you have sent to the queue of the default printer that are still waiting, enter:
lpstat
To check what jobs are pending for a particular printer, use the lpstat -P command. For example, to view the queue for printer ps99, enter:
lpstat -Pps99
Removing print jobs
With System V, use the cancel command to stop the printing of a file. To cancel a job, even if it's already printing, use the lpstat command to find the job number. Then use the cancel command to kill that job. For example, if you want to kill the job ps99-4, enter:
cancel ps99-4
Note: Unless you have system administration privileges, you can remove only print jobs that you have submitted, not jobs submitted by other users.
In Unix, what is the find command, and how do I use it to search through directories for files?
To use the find command, at the Unix prompt, enter:
find . -name "pattern" -print
Replace "pattern" with a filename or matching expression, such as "*.txt" . (Leave the double quotes in.)
Options
The general form of the command is:
find (starting directory) (matching criteria and actions)
The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.
You have several options for matching criteria:
-atime n File was accessed n days ago
-mtime n File was modified n days ago
-size n File is n blocks big (a block is 512 bytes)
-type c Specifies file type: f=plain text, d=directory
-fstype typ Specifies file system type: 4.2 or nfs
-name nam The filename is nam
-user usr The file's owner is usr
-group grp The file's group owner is grp
-perm p The file's access mode is p (where p is an integer)
You can use + (plus) and - (minus) modifiers with the atime, mtime, and size criteria to increase their usefulness, for example:
-mtime +7 Matches files modified more than seven days ago
-atime -2 Matches files accessed less than two days ago
-size +100 Matches files larger than 100 blocks (50KB)
By default, multiple options are joined by "and". You may specify "or" with the -o flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:
\( -mtime +7 -atime +30 \)
To match all files modified more than 7 days ago or accessed more than 30 days ago, use:
\( -mtime +7 -o -atime +30 \)
You may specify "not" with an exclamation point. To match all files ending in .txt except the file notme.txt, use:
\! -name notme.txt -name \*.txt
You can specify the following actions for the list of files that the find command locates:
-print Display pathnames of matching files.
-exec cmd Execute command cmd on a file.
-ok cmd Prompt before executing the command cmd on a file.
-mount (System V) Restrict to file system of starting directory.
-xdev (BSD) Restrict to file system of starting directory.
-prune (BSD) Don't descend into subdirectories.
Executed commands must end with \; (a backslash and semi-colon) and may use {} (curly braces) as a placeholder for each file that the find command locates. For example, for a long listing of each file found, use:
-exec ls -l {} \;
Matching criteria and actions may appear in any order and are evaluated from left to right.
Full examples
To find and report all C language source code files starting at the current directory, enter:
find . -name \*.c -print
To report all files starting in the directories /mydir1 and /mydir2 larger than 2,000 blocks (about 1,000KB) and that have not been accessed in over 30 days, enter:
find /mydir1 /mydir2 -size +2000 -atime +30 -print
To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days, enter:
find /mydir -atime +100 -ok rm {} \;
To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days, enter:
find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;
To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter:
find /prog -type f -size +1000 -print -name core -exec rm {} \; Note: On some systems, the name of the starting directory must end with a / (slash), or the find command will return nothing. Thus, the starting directory in the previous example would be designated as /prog/, with a trailing slash. On other systems, a trailing slash does not affect the command. A trailing slash is never needed when searching in / (the root directory), . (the current directory), or .. (the parent directory).
For more information, consult the Unix manual page by entering at the Unix prompt:
man find
find . -name "pattern" -print
Replace "pattern" with a filename or matching expression, such as "*.txt" . (Leave the double quotes in.)
Options
The general form of the command is:
find (starting directory) (matching criteria and actions)
The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.
You have several options for matching criteria:
-atime n File was accessed n days ago
-mtime n File was modified n days ago
-size n File is n blocks big (a block is 512 bytes)
-type c Specifies file type: f=plain text, d=directory
-fstype typ Specifies file system type: 4.2 or nfs
-name nam The filename is nam
-user usr The file's owner is usr
-group grp The file's group owner is grp
-perm p The file's access mode is p (where p is an integer)
You can use + (plus) and - (minus) modifiers with the atime, mtime, and size criteria to increase their usefulness, for example:
-mtime +7 Matches files modified more than seven days ago
-atime -2 Matches files accessed less than two days ago
-size +100 Matches files larger than 100 blocks (50KB)
By default, multiple options are joined by "and". You may specify "or" with the -o flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:
\( -mtime +7 -atime +30 \)
To match all files modified more than 7 days ago or accessed more than 30 days ago, use:
\( -mtime +7 -o -atime +30 \)
You may specify "not" with an exclamation point. To match all files ending in .txt except the file notme.txt, use:
\! -name notme.txt -name \*.txt
You can specify the following actions for the list of files that the find command locates:
-print Display pathnames of matching files.
-exec cmd Execute command cmd on a file.
-ok cmd Prompt before executing the command cmd on a file.
-mount (System V) Restrict to file system of starting directory.
-xdev (BSD) Restrict to file system of starting directory.
-prune (BSD) Don't descend into subdirectories.
Executed commands must end with \; (a backslash and semi-colon) and may use {} (curly braces) as a placeholder for each file that the find command locates. For example, for a long listing of each file found, use:
-exec ls -l {} \;
Matching criteria and actions may appear in any order and are evaluated from left to right.
Full examples
To find and report all C language source code files starting at the current directory, enter:
find . -name \*.c -print
To report all files starting in the directories /mydir1 and /mydir2 larger than 2,000 blocks (about 1,000KB) and that have not been accessed in over 30 days, enter:
find /mydir1 /mydir2 -size +2000 -atime +30 -print
To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days, enter:
find /mydir -atime +100 -ok rm {} \;
To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days, enter:
find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;
To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter:
find /prog -type f -size +1000 -print -name core -exec rm {} \; Note: On some systems, the name of the starting directory must end with a / (slash), or the find command will return nothing. Thus, the starting directory in the previous example would be designated as /prog/, with a trailing slash. On other systems, a trailing slash does not affect the command. A trailing slash is never needed when searching in / (the root directory), . (the current directory), or .. (the parent directory).
For more information, consult the Unix manual page by entering at the Unix prompt:
man find
In Unix, how do I change the permissions for a file?
In Unix, how do I change the permissions for a file?
You can change file permissions with the chmod command. In Unix, file permissions, which establish who may have different types of access to a file, are specified by both access classes and access types. Access classes are groups of users, and each may be assigned specific access types. The access classes are "user", "group", "other", and "all". These refer, respectively, to the user who owns the file, a specific group of users, the other remaining users who are not in the group, and all three sets of users. Access types (read, write, and execute) determine what may be done with the file by each access class.
There are two basic ways of using chmod to change file permissions:
Symbolic method
The first and probably easiest way is the relative (or symbolic) method, which lets you specify access classes and types with single letter abbreviations. A chmod command with this form of syntax consists of at least three parts from the following lists:
Access Class Operator Access Type
u (user) + (add access) r (read)
g (group) - (remove access) w (write)
o (other) = (set exact access) x (execute)
a (all: u, g, and o)
For example, to add permission for everyone to read a file in the current directory named myfile, at the Unix prompt, you would enter:
chmod a+r myfile
The a stands for "all", the + for "add", and the r for "read".
Note: This assumes that everyone already has access to the directory where myfile is located and its parent directories; that is, you must set the directory permissions separately.
If you omit the access class, it's assumed to be all, so you could also enter the previous example as:
chmod +r myfile
You can also specify multiple classes and types with a single command. For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) on a file named myfile, you would enter:
chmod go-rw myfile
You can also specify that different permissions be added and removed in the same command. For example, to remove write permission and add execute for all users on myfile, you would enter:
chmod a-w+x myfile
In each of these examples, the access types that aren't specified are unchanged. The previous command, for example, doesn't change any existing settings specifying whether users besides yourself may have read ( r ) access to myfile. You could also use the exact form to explicitly state that group and other users' access is set only to read with the = operator:
chmod go=r myfile
The chmod command also operates on directories. For example, to remove write permission for other users on a subdirectory named mydir, you would enter:
chmod o-w mydir
To do the same for the current directory, you would enter:
chmod o-w Be careful when setting the permissions of directories, particularly your home directory; you don't want to lock yourself out by removing your own access. Also, you must have execute permission on a directory to switch ( cd ) to it.
Absolute form
The other way to use the chmod command is the absolute form. In this case, you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file's permissions.
The three numbers are specified in the order: user (or owner), group, other. Each number is the sum of values that specify read (4), write (2), and execute (1) access, with 0 (zero) meaning no access. For example, if you wanted to give yourself read, write, and execute permissions on myfile; give users in your group read and execute permissions; and give others only execute permission, the appropriate number would be calculated as (4+2+1)(4+0+1)(0+0+1) for the three digits 751. You would then enter the command as:
chmod 751 myfile
As another example, to give only yourself read, write, and execute permission on the current directory, you would calculate the digits as (4+2+1)(0+0+0)(0+0+0) for the sequence 700, and enter the command:
chmod 700 If it seems clearer to you, you can also think of the three digit sequence as the sum of attributes you select from the following table:
400 read by owner
200 write by owner
100 execute by owner
040 read by group
020 write by group
010 execute by group
004 read by others
002 write by others
001 execute by others
To create an access mode, sum all the accesses you wish to permit. For example, to give read privileges to all, and write and execute privileges to the owner only for a file, you would sum: 400+200+100+040+004 = 744. Then, at the Unix prompt, you would enter:
chmod 744 myfile.ext
Some other frequently used examples are:
777 anyone can do anything (read, write, or execute)
755 you can do anything; others can only read and execute
711 you can do anything; others can only execute
644 you can read and write; others can only read
More information
For more information about chmod, consult the manual page. At the Unix prompt, enter:
man chmod
You can change file permissions with the chmod command. In Unix, file permissions, which establish who may have different types of access to a file, are specified by both access classes and access types. Access classes are groups of users, and each may be assigned specific access types. The access classes are "user", "group", "other", and "all". These refer, respectively, to the user who owns the file, a specific group of users, the other remaining users who are not in the group, and all three sets of users. Access types (read, write, and execute) determine what may be done with the file by each access class.
There are two basic ways of using chmod to change file permissions:
Symbolic method
The first and probably easiest way is the relative (or symbolic) method, which lets you specify access classes and types with single letter abbreviations. A chmod command with this form of syntax consists of at least three parts from the following lists:
Access Class Operator Access Type
u (user) + (add access) r (read)
g (group) - (remove access) w (write)
o (other) = (set exact access) x (execute)
a (all: u, g, and o)
For example, to add permission for everyone to read a file in the current directory named myfile, at the Unix prompt, you would enter:
chmod a+r myfile
The a stands for "all", the + for "add", and the r for "read".
Note: This assumes that everyone already has access to the directory where myfile is located and its parent directories; that is, you must set the directory permissions separately.
If you omit the access class, it's assumed to be all, so you could also enter the previous example as:
chmod +r myfile
You can also specify multiple classes and types with a single command. For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) on a file named myfile, you would enter:
chmod go-rw myfile
You can also specify that different permissions be added and removed in the same command. For example, to remove write permission and add execute for all users on myfile, you would enter:
chmod a-w+x myfile
In each of these examples, the access types that aren't specified are unchanged. The previous command, for example, doesn't change any existing settings specifying whether users besides yourself may have read ( r ) access to myfile. You could also use the exact form to explicitly state that group and other users' access is set only to read with the = operator:
chmod go=r myfile
The chmod command also operates on directories. For example, to remove write permission for other users on a subdirectory named mydir, you would enter:
chmod o-w mydir
To do the same for the current directory, you would enter:
chmod o-w Be careful when setting the permissions of directories, particularly your home directory; you don't want to lock yourself out by removing your own access. Also, you must have execute permission on a directory to switch ( cd ) to it.
Absolute form
The other way to use the chmod command is the absolute form. In this case, you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file's permissions.
The three numbers are specified in the order: user (or owner), group, other. Each number is the sum of values that specify read (4), write (2), and execute (1) access, with 0 (zero) meaning no access. For example, if you wanted to give yourself read, write, and execute permissions on myfile; give users in your group read and execute permissions; and give others only execute permission, the appropriate number would be calculated as (4+2+1)(4+0+1)(0+0+1) for the three digits 751. You would then enter the command as:
chmod 751 myfile
As another example, to give only yourself read, write, and execute permission on the current directory, you would calculate the digits as (4+2+1)(0+0+0)(0+0+0) for the sequence 700, and enter the command:
chmod 700 If it seems clearer to you, you can also think of the three digit sequence as the sum of attributes you select from the following table:
400 read by owner
200 write by owner
100 execute by owner
040 read by group
020 write by group
010 execute by group
004 read by others
002 write by others
001 execute by others
To create an access mode, sum all the accesses you wish to permit. For example, to give read privileges to all, and write and execute privileges to the owner only for a file, you would sum: 400+200+100+040+004 = 744. Then, at the Unix prompt, you would enter:
chmod 744 myfile.ext
Some other frequently used examples are:
777 anyone can do anything (read, write, or execute)
755 you can do anything; others can only read and execute
711 you can do anything; others can only execute
644 you can read and write; others can only read
More information
For more information about chmod, consult the manual page. At the Unix prompt, enter:
man chmod
In Unix, what is a group?
In Unix, a group is a logical collection of users on a system, the primary use of which is to assign "group ownership" of files and directories. As a result, certain groups of users on a system can all have the same access rights to the designated files and directories. Each group is independent of other groups and there is no specific relationship between groups. For a list of the groups to which you belong, at the Unix prompt, enter:
groups
To change the group ownership of a file, use the chgrp command. Typically, you can only change the file's group to a group to which you belong. For more information, read the chgrp man page by entering:
man chgrp
groups
To change the group ownership of a file, use the chgrp command. Typically, you can only change the file's group to a group to which you belong. For more information, read the chgrp man page by entering:
man chgrp
In Unix, how do I combine several text files into a single file?
To combine several text files into a single file in Unix, use the cat command:
cat file1 file2 file3 > newfile
Replace file1, file2, and file3 with the names of the files you wish to combine, in the order you want them to appear in the combined document. Replace newfile with a name for your newly combined single file.
If you want to add one or more files to an existing document, use the format:
cat file1 file2 file3 >> destfile
This command will add file1, file2, and file3 (in that order) to the end of destfile.
Note: If you use > instead of >>, you will overwrite destfile rather than add to it.
cat file1 file2 file3 > newfile
Replace file1, file2, and file3 with the names of the files you wish to combine, in the order you want them to appear in the combined document. Replace newfile with a name for your newly combined single file.
If you want to add one or more files to an existing document, use the format:
cat file1 file2 file3 >> destfile
This command will add file1, file2, and file3 (in that order) to the end of destfile.
Note: If you use > instead of >>, you will overwrite destfile rather than add to it.
Introduction to Unix commands
This is a very brief introduction to some useful Unix commands, including examples of how to use each command. For more extensive information about any of these commands, use the man command as described below.
Commands
cal cat cd chmod
cp date df du
find jobs kill less and more
lpr and lp ls man mkdir
mv ps pwd rm
rmdir set vi w and who
cal
This command will print a calendar for a specified month and/or year.
To show this month's calendar, enter:
cal To show a twelve-month calendar for 2008, enter:
cal 2008
To show a calendar for just the month of June 1970, enter:
cal 6 1970
cat
This command outputs the contents of a text file. You can use it to read brief files or to concatenate files together.
To append file1 onto the end of file2, enter:
cat file1 >> file2 To view the contents of a file named myfile, enter:
cat myfile
Because cat displays text without pausing, its output may quickly scroll off your screen. Use the less command (described below) or an editor for reading longer text files.
cd
This command changes your current directory location. By default, your Unix login session begins in your home directory.
To switch to a subdirectory (of the current directory) named myfiles, enter:
cd myfiles
To switch to a directory named /home/dvader/empire_docs, enter:
cd /home/dvader/empire_docs To move to the parent directory of the current directory, enter:
cd .. To move to the root directory, enter:
cd /
To return to your home directory, enter:
cd
chmod
This command changes the permission information associated with a file. Every file (including directories, which Unix treats as files) on a Unix system is stored with records indicating who has permission to read, write, or execute the file, abbreviated as r, w, and x. These permissions are broken down for three categories of user: first, the owner of the file; second, a group with which both the user and the file may be associated; and third, all other users. These categories are abbreviated as u for owner (or user), g for group, and o for other.
To allow yourself to execute a file that you own named myfile, enter:
chmod u+x myfile To allow anyone who has access to the directory in which myfile is stored to read or execute myfile, enter:
chmod o+rx myfile You can view the permission settings of a file using the ls command, described below.
Note: Be careful with the chmod command. If you tamper with the directory permissions of your home directory, for example, you could lock yourself out or allow others unrestricted access to your account and its contents.
cp
This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's safest to always add -i after the cp command, to force the system to ask for your approval before it destroys any files. The general syntax for cp is:
cp -i oldfile newfile To copy a file named meeting1 in the directory /home/dvader/notes to your current directory, enter:
cp -i /home/dvader/notes/meeting1 . The . (period) indicates the current directory as destination, and the -i ensures that if there is another file named meeting1 in the current directory, you will not overwrite it by accident.
To copy a file named oldfile in the current directory to the new name newfile in the mystuff subdirectory of your home directory, enter:
cp -i oldfile ~/mystuff/newfile The ~ character (tilde) is interpreted as the path of your home directory.
Note: You must have permission to read a file in order to copy it.
date
The date command displays the current day, date, time, and year.
To see this information, enter:
date df
This command reports file system disk usage (i.e., the amount of space taken up on mounted file systems). For each mounted file system, df reports the file system device, the number of blocks used, the number of blocks available, and the directory where the file system is mounted.
To find out how much disk space is used on each file system, enter the following command:
df
If the df command is not configured to show blocks in kilobytes by default, you can issue the following command:
df -k
du
This command reports disk usage (i.e., the amount of space taken up by a group of files). The du command descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it finds.
To find out how much disk space your files take up, switch to your home directory with the cd command, and enter:
du The numbers reported are the sizes of the files; on different systems, these sizes will be in units of either 512 byte blocks or kilobytes. To learn which is the case, use the man command, described below. On most systems, du -k will give sizes in kilobytes.
find
The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.
To find all of the files named myfile.txt in your current directory and all of its subdirectories, enter:
find . -name myfile.txt -print To look in your current directory and its subdirectories for all of the files that end in the extension .txt , enter:
find . -name "*.txt" -print In these examples, the . (period) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, enter:
find /home/user/myusername/ -name myfile.txt -print On some systems, omitting the final / (slash) after the directory name can cause find to fail to return any results.
As a shortcut for searching in your home directory, enter:
find "$HOME/" -name myfile.txt -print For more, see In Unix, what is the find command, and how do I use it to search through directories for files?
jobs
This command reports any programs that you suspended and still have running or waiting in the background (if you had pressed Ctrl-z to suspend an editing session, for example). For a list of suspended jobs, enter:
jobs Each job will be listed with a number; to resume a job, enter % (percent sign) followed by the number of the job. To restart job number two, for example, enter:
%2 This command is only available in the csh, bash, tcsh, and ksh shells.
kill
Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job number three, for example, enter:
kill %3 Now check the jobs command again. If the job has not been cancelled, harsher measures may be necessary. Enter:
kill -9 %3
less and more
Both less and more display the contents of a file one screen at a time, waiting for you to press the Spacebar between screens. This lets you read text without it scrolling quickly off your screen. The less utility is generally more flexible and powerful than more, but more is available on all Unix systems while less may not be.
To read the contents of a file named textfile in the current directory, enter:
less textfile The less utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:
ls -la | less In both examples, you could substitute more for less with similar results. To exit either less or more, press q . To exit less after viewing the file, press q .
Note: Do not use less or more with executables (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal.
lpr and lp
These commands print a file on a printer connected to the computer network. The lpr command is used on BSD systems, and the lp command is used in System V. Both commands may be used on the UITS systems.
To print a file named myfile on a printer named lp1 with lpr, enter:
lpr -Plp1 myfile
To print the same file to the same printer with lp, enter:
lp -dlp1 myfile
Note: Do not print to a printer whose name or location is unfamiliar to you.
ls
This command will list the files stored in a directory. To see a brief, multi-column list of the files in the current directory, enter:
ls To also see "dot" files (configuration files that begin with a period, such as .login ), enter:
ls -a To see the file permissions, owners, and sizes of all files, enter:
ls -la If the listing is long and scrolls off your screen before you can read it, combine ls with the less utility, for example:
ls -la | less For more, see In Unix, how do I list the files in a directory?
man
This command displays the manual page for a particular command. If you are unsure how to use a command or want to find out all its options, you might want to try using man to view the manual page.
For example, to learn more about the ls command, enter:
man ls To learn more about man, enter:
man man If you are not sure of the exact command name, you can use man with the -k option to help you find the command you need. To see one line summaries of each reference page that contains the keyword you specify, enter:
man -k keyword Replace keyword in the above example with the keyword which you want to reference. Also see In Unix, what is the man command, and how do I use it to read manual pages?
mkdir
This command will make a new subdirectory.
To create a subdirectory named mystuff in the current directory, enter:
mkdir mystuff To create a subdirectory named morestuff in the existing directory named /tmp, enter:
mkdir /tmp/morestuff Note: To make a subdirectory in a particular directory, you must have permission to write to that directory.
mv
This command will move a file. You can use mv not only to change the directory location of a file, but also to rename files. Unlike the cp command, mv will not preserve the original file.
Note: As with the cp command, you should always use -i to make sure you do not overwrite an existing file.
To rename a file named oldname in the current directory to the new name newname, enter:
mv -i oldname newname To move a file named hw1 from a subdirectory named newhw to another subdirectory named oldhw (both subdirectories of the current directory), enter:
mv -i newhw/hw1 oldhw
If, in this last operation, you also wanted to give the file a new name, such as firsthw, you would enter:
mv -i newhw/hw1 oldhw/firsthw ps
The ps command displays information about programs (i.e., processes) that are currently running. Entered without arguments, it lists basic information about interactive processes you own. However, it also has many options for determining what processes to display, as well as the amount of information about each. Like lp and lpr, the options available differ between BSD and System V implementations. For example, to view detailed information about all running processes, in a BSD system, you would use ps with the following arguments:
ps -alxww
To display similar information in System V, use the arguments:
ps -elf
For more information about ps refer to the ps man page on your system. Also see In Unix, what do the output fields of the ps command mean?
pwd
This command reports the current directory path. Enter the command by itself:
pwd
For more, see In Unix, how do I determine my current working directory?
rm
This command will remove (destroy) a file. You should enter this command with the -i option, so that you'll be asked to confirm each file deletion. To remove a file named junk, enter:
rm -i junk
Note: Using rm will remove a file permanently, so be sure you really want to delete a file before you use rm.
To remove a non-empty subdirectory, rm accepts the -r option. On most systems this will prompt you to confirm the removal of each file. This behavior can be prevented by adding the -f option. To remove an entire subdirectory named oldstuff and all of its contents, enter:
rm -rf oldstuff
Note: Using this command will cause rm to descend into each subdirectory within the specified subdirectory and remove all files without prompting you. Use this command with caution, as it is very easy to accidently delete important files. As a precaution, use the ls command to list the files within the subdirectory you wish to remove. To browse through a subdirectory named oldstuff, enter:
ls -R oldstuff | less
rmdir
This command will remove a subdirectory. To remove a subdirectory named oldstuff, enter:
rmdir oldstuff Note: The directory you specify for removal must be empty. To clean it out, switch to the directory and use the ls and rm commands to inspect and delete files.
set
This command displays or changes various settings and options associated with your Unix session.
To see the status of all settings, enter the command without options:
set If the output scrolls off your screen, combine set with less:
set | less
The syntax used for changing settings is different for the various kinds of Unix shells; see the man entries for set and the references listed at the end of this document for more information.
vi
This command starts the vi text editor. To edit a file named myfile in the current directory, enter:
vi myfile The vi editor works fairly differently from other text editors. If you have not used it before, you should probably look at a tutorial, such as How do I use the vi text editor? Another helpful document for getting started with vi is A quick reference list of vi editor commands.
The very least you need to know to start using vi is that in order to enter text, you need to switch the program from command mode to insert mode by pressing i . To navigate around the document with the cursor keys, you must switch back to command mode by pressing Esc. To execute any of the following commands, you must switch from command mode to ex mode by pressing : (the colon key): Enter w to save; wq to save and quit; q! to quit without saving.
w and who
The w and who commands are similar programs that list all users logged into the computer. If you use w, you also get a list of what they are doing. If you use who, you also get the IP numbers or computer names of the terminals they are using.
Commands
cal cat cd chmod
cp date df du
find jobs kill less and more
lpr and lp ls man mkdir
mv ps pwd rm
rmdir set vi w and who
cal
This command will print a calendar for a specified month and/or year.
To show this month's calendar, enter:
cal To show a twelve-month calendar for 2008, enter:
cal 2008
To show a calendar for just the month of June 1970, enter:
cal 6 1970
cat
This command outputs the contents of a text file. You can use it to read brief files or to concatenate files together.
To append file1 onto the end of file2, enter:
cat file1 >> file2 To view the contents of a file named myfile, enter:
cat myfile
Because cat displays text without pausing, its output may quickly scroll off your screen. Use the less command (described below) or an editor for reading longer text files.
cd
This command changes your current directory location. By default, your Unix login session begins in your home directory.
To switch to a subdirectory (of the current directory) named myfiles, enter:
cd myfiles
To switch to a directory named /home/dvader/empire_docs, enter:
cd /home/dvader/empire_docs To move to the parent directory of the current directory, enter:
cd .. To move to the root directory, enter:
cd /
To return to your home directory, enter:
cd
chmod
This command changes the permission information associated with a file. Every file (including directories, which Unix treats as files) on a Unix system is stored with records indicating who has permission to read, write, or execute the file, abbreviated as r, w, and x. These permissions are broken down for three categories of user: first, the owner of the file; second, a group with which both the user and the file may be associated; and third, all other users. These categories are abbreviated as u for owner (or user), g for group, and o for other.
To allow yourself to execute a file that you own named myfile, enter:
chmod u+x myfile To allow anyone who has access to the directory in which myfile is stored to read or execute myfile, enter:
chmod o+rx myfile You can view the permission settings of a file using the ls command, described below.
Note: Be careful with the chmod command. If you tamper with the directory permissions of your home directory, for example, you could lock yourself out or allow others unrestricted access to your account and its contents.
cp
This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's safest to always add -i after the cp command, to force the system to ask for your approval before it destroys any files. The general syntax for cp is:
cp -i oldfile newfile To copy a file named meeting1 in the directory /home/dvader/notes to your current directory, enter:
cp -i /home/dvader/notes/meeting1 . The . (period) indicates the current directory as destination, and the -i ensures that if there is another file named meeting1 in the current directory, you will not overwrite it by accident.
To copy a file named oldfile in the current directory to the new name newfile in the mystuff subdirectory of your home directory, enter:
cp -i oldfile ~/mystuff/newfile The ~ character (tilde) is interpreted as the path of your home directory.
Note: You must have permission to read a file in order to copy it.
date
The date command displays the current day, date, time, and year.
To see this information, enter:
date df
This command reports file system disk usage (i.e., the amount of space taken up on mounted file systems). For each mounted file system, df reports the file system device, the number of blocks used, the number of blocks available, and the directory where the file system is mounted.
To find out how much disk space is used on each file system, enter the following command:
df
If the df command is not configured to show blocks in kilobytes by default, you can issue the following command:
df -k
du
This command reports disk usage (i.e., the amount of space taken up by a group of files). The du command descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it finds.
To find out how much disk space your files take up, switch to your home directory with the cd command, and enter:
du The numbers reported are the sizes of the files; on different systems, these sizes will be in units of either 512 byte blocks or kilobytes. To learn which is the case, use the man command, described below. On most systems, du -k will give sizes in kilobytes.
find
The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.
To find all of the files named myfile.txt in your current directory and all of its subdirectories, enter:
find . -name myfile.txt -print To look in your current directory and its subdirectories for all of the files that end in the extension .txt , enter:
find . -name "*.txt" -print In these examples, the . (period) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, enter:
find /home/user/myusername/ -name myfile.txt -print On some systems, omitting the final / (slash) after the directory name can cause find to fail to return any results.
As a shortcut for searching in your home directory, enter:
find "$HOME/" -name myfile.txt -print For more, see In Unix, what is the find command, and how do I use it to search through directories for files?
jobs
This command reports any programs that you suspended and still have running or waiting in the background (if you had pressed Ctrl-z to suspend an editing session, for example). For a list of suspended jobs, enter:
jobs Each job will be listed with a number; to resume a job, enter % (percent sign) followed by the number of the job. To restart job number two, for example, enter:
%2 This command is only available in the csh, bash, tcsh, and ksh shells.
kill
Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job number three, for example, enter:
kill %3 Now check the jobs command again. If the job has not been cancelled, harsher measures may be necessary. Enter:
kill -9 %3
less and more
Both less and more display the contents of a file one screen at a time, waiting for you to press the Spacebar between screens. This lets you read text without it scrolling quickly off your screen. The less utility is generally more flexible and powerful than more, but more is available on all Unix systems while less may not be.
To read the contents of a file named textfile in the current directory, enter:
less textfile The less utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:
ls -la | less In both examples, you could substitute more for less with similar results. To exit either less or more, press q . To exit less after viewing the file, press q .
Note: Do not use less or more with executables (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal.
lpr and lp
These commands print a file on a printer connected to the computer network. The lpr command is used on BSD systems, and the lp command is used in System V. Both commands may be used on the UITS systems.
To print a file named myfile on a printer named lp1 with lpr, enter:
lpr -Plp1 myfile
To print the same file to the same printer with lp, enter:
lp -dlp1 myfile
Note: Do not print to a printer whose name or location is unfamiliar to you.
ls
This command will list the files stored in a directory. To see a brief, multi-column list of the files in the current directory, enter:
ls To also see "dot" files (configuration files that begin with a period, such as .login ), enter:
ls -a To see the file permissions, owners, and sizes of all files, enter:
ls -la If the listing is long and scrolls off your screen before you can read it, combine ls with the less utility, for example:
ls -la | less For more, see In Unix, how do I list the files in a directory?
man
This command displays the manual page for a particular command. If you are unsure how to use a command or want to find out all its options, you might want to try using man to view the manual page.
For example, to learn more about the ls command, enter:
man ls To learn more about man, enter:
man man If you are not sure of the exact command name, you can use man with the -k option to help you find the command you need. To see one line summaries of each reference page that contains the keyword you specify, enter:
man -k keyword Replace keyword in the above example with the keyword which you want to reference. Also see In Unix, what is the man command, and how do I use it to read manual pages?
mkdir
This command will make a new subdirectory.
To create a subdirectory named mystuff in the current directory, enter:
mkdir mystuff To create a subdirectory named morestuff in the existing directory named /tmp, enter:
mkdir /tmp/morestuff Note: To make a subdirectory in a particular directory, you must have permission to write to that directory.
mv
This command will move a file. You can use mv not only to change the directory location of a file, but also to rename files. Unlike the cp command, mv will not preserve the original file.
Note: As with the cp command, you should always use -i to make sure you do not overwrite an existing file.
To rename a file named oldname in the current directory to the new name newname, enter:
mv -i oldname newname To move a file named hw1 from a subdirectory named newhw to another subdirectory named oldhw (both subdirectories of the current directory), enter:
mv -i newhw/hw1 oldhw
If, in this last operation, you also wanted to give the file a new name, such as firsthw, you would enter:
mv -i newhw/hw1 oldhw/firsthw ps
The ps command displays information about programs (i.e., processes) that are currently running. Entered without arguments, it lists basic information about interactive processes you own. However, it also has many options for determining what processes to display, as well as the amount of information about each. Like lp and lpr, the options available differ between BSD and System V implementations. For example, to view detailed information about all running processes, in a BSD system, you would use ps with the following arguments:
ps -alxww
To display similar information in System V, use the arguments:
ps -elf
For more information about ps refer to the ps man page on your system. Also see In Unix, what do the output fields of the ps command mean?
pwd
This command reports the current directory path. Enter the command by itself:
pwd
For more, see In Unix, how do I determine my current working directory?
rm
This command will remove (destroy) a file. You should enter this command with the -i option, so that you'll be asked to confirm each file deletion. To remove a file named junk, enter:
rm -i junk
Note: Using rm will remove a file permanently, so be sure you really want to delete a file before you use rm.
To remove a non-empty subdirectory, rm accepts the -r option. On most systems this will prompt you to confirm the removal of each file. This behavior can be prevented by adding the -f option. To remove an entire subdirectory named oldstuff and all of its contents, enter:
rm -rf oldstuff
Note: Using this command will cause rm to descend into each subdirectory within the specified subdirectory and remove all files without prompting you. Use this command with caution, as it is very easy to accidently delete important files. As a precaution, use the ls command to list the files within the subdirectory you wish to remove. To browse through a subdirectory named oldstuff, enter:
ls -R oldstuff | less
rmdir
This command will remove a subdirectory. To remove a subdirectory named oldstuff, enter:
rmdir oldstuff Note: The directory you specify for removal must be empty. To clean it out, switch to the directory and use the ls and rm commands to inspect and delete files.
set
This command displays or changes various settings and options associated with your Unix session.
To see the status of all settings, enter the command without options:
set If the output scrolls off your screen, combine set with less:
set | less
The syntax used for changing settings is different for the various kinds of Unix shells; see the man entries for set and the references listed at the end of this document for more information.
vi
This command starts the vi text editor. To edit a file named myfile in the current directory, enter:
vi myfile The vi editor works fairly differently from other text editors. If you have not used it before, you should probably look at a tutorial, such as How do I use the vi text editor? Another helpful document for getting started with vi is A quick reference list of vi editor commands.
The very least you need to know to start using vi is that in order to enter text, you need to switch the program from command mode to insert mode by pressing i . To navigate around the document with the cursor keys, you must switch back to command mode by pressing Esc. To execute any of the following commands, you must switch from command mode to ex mode by pressing : (the colon key): Enter w to save; wq to save and quit; q! to quit without saving.
w and who
The w and who commands are similar programs that list all users logged into the computer. If you use w, you also get a list of what they are doing. If you use who, you also get the IP numbers or computer names of the terminals they are using.
Subscribe to:
Posts (Atom)