Basic of Unix Basic of Unix
Basic of Unix Basic of Unix
BASIC OF UNIX
This is a list of UNIX utilities as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX
Specification (SUS). These utilities can be found on UNIX Operating systems and most UNIX-like operating
systems.
[edit]List
awk Text processing Pattern scanning and processing language Version 7 AT&T UNIX
comm Text Processing Select or reject lines common to two files Version 4 AT&T UNIX
cut Shell Programming Cut out selected fields of each line of a file
date Misc Write the date and time Version 1 AT&T UNIX
fuser Process Management List process IDs of all processes that have one or more files open
head Text processing Copy the first part of files PWB UNIX[citation needed]
ipcrm Misc Remove a message queue, semaphore set, or shared memory segment identifier
nice Process Management Invoke a utility with an altered nice value Version 4 AT&T UNIX
paste Text processing Merge corresponding or subsequent lines of files Version 32V AT&T UNIX
sh Shell programming Shell, the standard command language interpreter Version 7 AT&T
UNIX (in earlier versions, sh was either the Thompson shell or the PWB shell)
sleep Shell programming Suspend execution for an interval Version 4 AT&T UNIX
BASIC OF UNIX
BASIC OF UNIX
tail Text processing Copy the last part of a file PWB UNIX[citation needed]
touch Filesystem Change file access and modification times Version 7 AT&T UNIX
uniq Text Processing Report or filter out repeated lines in a file Version 3 AT&T UNIX
wc Text processing Line, word and byte or character count Version 1 AT&T UNIX
who System Administration Display who is on the system Version 1 AT&T UNIX
xargs Shell Programming Construct argument lists and invoke utility PWB UNIX
grep
From Wikipedia, the free encyclopedia
grep is a command line text search utility originally written for Unix. The name is taken from the first
BASIC OF UNIX
BASIC OF UNIX
letters in global / regular expression / print, a series of instructions in text editors such as ed.[1] A
backronym of the unusual name also exists in the form of Generalized Regular Expression Parser. The
grep command searches files or standard input globally for lines matching a given regular expression,
and prints them to the program's standard output.
Contents [hide]
1 History
2 Usage
3 Variations
5 See also
6 Notes
7 References
8 External links
[edit]History
Grep was created by Ken Thompson as a standalone application adapted from the regular expression
parser he had written for the ed editor (which he also created). The name grep comes from the ed editor
command it simulated, g/re/p (global regular expression print).[2] Its official date of creation is given as
March 3, 1973 in the Manual for Unix Version 4.
[edit]Usage
In this case, grep prints all lines containing apple from the file fruitlist.txt, regardless of word boundaries;
therefore lines containing pineapple or apples are also printed. The grep command is case sensitive by
default, so this example's output does not include lines containing Apple (with a capital A) unless they
also contain apple.
BASIC OF UNIX
BASIC OF UNIX
To search all .txt files in a directory for apple in a shell that supports globbing, use an asterisk in place of
the file name:
Regular expressions can be used to match more complicated queries. The following prints all lines in the
file that begin with the letter a, followed by any one character, then the letters ple.
As noted above, the term "grep" derives from a usage in ed and related text editors. Before grep existed
as a separate command, the same effect might have been achieved by doing:
ed fruitlist.txt
g/^a.ple/p
where the second line is the command given to ed to print the relevant lines, and the third line is the
command to exit from ed.
Like most Unix commands, grep accepts options in the form of command-line arguments, to change
many of its behaviors. For example:
This prints all lines containing apple regardless of capitalization. The -i argument tells grep to be case
insensitive, or to ignore case.
To print all lines containing apple as a word (pineapple and apples will not match):
But if fruitlist.txt contains apple word followed by hyphen (-) character, it will also get matched.
cat fruitlist.txt
apple
apples
pineapple
apple-
BASIC OF UNIX
BASIC OF UNIX
apple-fruit
fruit-apple
apple
apple-
apple-fruit
fruit-apple
the -v (lower-case v) prints all lines that do NOT contain apple in this example.
banana
pear
peach
orange
The shell doesn't start a separate process to run internal commands. External
commands require the shell to fork and exec ( 1.11 ) a new subprocess ( 38.3 ) ; this
BASIC OF UNIX
BASIC OF UNIX
When you type the name of a command, the shell first checks to see if it is a built-in
command and, if so, executes it. If the command name is an absolute pathname
( 1.21 ) beginning with / , like /bin/ls , there is no problem: the command is likewise
executed. If the command is neither built-in, nor specified with an absolute pathname,
the shell looks in its search path (8.7 ) for an executable program or script with the
given name.
The search path is exactly what its name implies: a list of directories that the shell
should look through for a command whose name matches what is typed.
The search path isn't built into the shell; it's something you specify in your shell setup
files ( 2.2 ) .
The path is searched in order, so if there are two commands with the same name, the
one that is found first in the path will be executed.
You can add new directories to your search path ( 8.7 ) on the fly, but the path is
usually set in shell setup files.
- TOR
BASIC OF UNIX
BASIC OF UNIX
It's possible to customize the prompt ( 7.1 ) so that it displays additional information,
but most users and system administrators will keep the convention of ending the
prompt with the original prompt character.
To be certain, type one of these commands (the second is for systems that use NIS,
Sun's Network Information Service, to manage network-wide files):
%
grep
yourloginname
/etc/passwd
yourloginname
You should get back the contents of your entry in the system password file. [2] For
example:
BASIC OF UNIX
BASIC OF UNIX
[2] That may match more than one entry. Searching for tim could also find a user
named timothy or fatima . A more accurate regular expression
( 26.1 ) is '^yourloginname :' .
tim::23:10:Tim O'Reilly:/home/tim:/bin/csh
The fields are separated by colons. The last field should show the shell you are
using. /bin/csh (or /usr/bin/csh ) is the C shell, /bin/sh is the Bourne shell (or Korn
shell), and so forth. An empty last field defaults to the Bourne shell.
In case you're interested in the rest of the line, the first field shows your login name.
The second stores your encrypted password, if any; this may be kept in a separate
"shadow password" file. The third and fourth fields show your user ID or UID
( 38.3 ) and group ID or GID ( 38.3 ) , respectively. The fifth field often contains
information about you, and the sixth your home directory.
- TOR
To get around this problem, programs that want to stick around first copy themselves
with the fork system call. Then the copied program exec s the new program,
terminating itself in the process.
You don't really need to know this little tidbit about what goes on behind the scenes,
but it sure helps to know about fork and exec when reading some UNIX manuals.
Article 38.2 has more information.
- TOR
These are symbols that you can use at the Unix shell prompt as part of a command.
Symbol Meaning
| a Unix pipe
> redirect standard output
< redirect standard input
>> redirect and append standard output
; separate commands on same line
() group commands on same line
/ separator in a pathname
~ (tilde) your home directory
~john the home directory of the user john
. present working directory
.. parent of present working directory
BASIC OF UNIX
BASIC OF UNIX
Introduction
This appendix gives several examples of Things and their tasks in a Unix C-Shell Environment. I
don’t know if the current breakdown of each set of tasks into (1) tasks that give information, and (2)
all other tasks, is one of the best, meaning one that would be preferred by a majority of Unix users.
That could only be determined by testing on a large population of the users themselves. It is, at
present, simply the breakdown that has proven most natural to me in the course of using Unix over
several years. I am sure there are omissions in each list of tasks, but let there be no doubt about the
goal: for each Thing, to list all the tasks that a user can perform on the Thing. No user should ever
have to figure out if a task he or she has in mind can be performed on a Thing. (I have excluded tasks
reserved for the superuser from this appendix. In the actual Environment, of course, these are
included, and clearly marked as such.) The examples make the reasonable assumption that the Unix
system has man (manual) pages, i.e., on-line information on each command (and a few other terms)
which can be displayed by entering “man” followed by the name of the command or the term, e.g.,
man rm. A portion of the Introduction to the Unix Environment (see Appendix A for a complete
example) is also given, this portion providing a few more details about using the Things and the tasks
listings. Not all referenced sections are included in this appendix. Those not included are marked by
“[not shown]” following the reference. A sequence of vertical dots denotes omitted material.
Appendix B — A Unix Environment (Partial) 126
BASIC OF UNIX
BASIC OF UNIX
Unix Environment
Introduction
About Things and their Tasks ... ...
Body of Environment Things is a term for important Unix entities like files, directories, shells,
scripts — yes, even users! Each such Thing has associated with it a set of tasks. In this Envi-ronment,
every task which can be performed on each Thing is given under the name of the Thing. If you do not
find the task you are looking for in the list of tasks, then it cannot be performed by a user for whom
this Environment is intended! The following conventions have been used in the lists of tasks: •
Commonly used forms of the command(s) implementing each task are given using normal syntax
conventions (see above in this Introduction under “Conventions” [not shown]). For example, cp
<current file name> <new file name>. If you need more information on the command, then simply
enter man <name of command>. • In cases where it was difficult to decide on a commonly used
form, a refer-ence to the man pages is given. A word or phrase in double quotes means that the
definition can be looked up if the user desires. • In cases where an explanation of more than a line or
two is required, a refer-ence to the relevant topic in this Environment is given. • At the end of each
list of tasks, under “Related,” is a list of topics (not neces-sarily just the topics called Things) which
are closely related to the Thing covered by the tasks. ...
.. Appendix B — A Unix Environment (Partial) 127 .
BASIC OF UNIX
BASIC OF UNIX
command
Definition of “command” in Unix: See “command, definition of” [not shown]. Tasks to find
information about commands Given the name of a command, display information about the
command syntax and semantics: man <name of command> Display a list of recently executed
commands: history Display a list of the currently active abbreviations of commands and what they
stand for: more .cshrc Other tasks on commands Execute a command so that it runs in the
“foreground:” Following the prompt, type the command name, then press Enter. Execute a command
so that it runs in the “background:” Same as for running command in “foreground,” except add a &
before pressing Enter. Stop a command from continuing execution, i.e., abort a command: For a
command executing in the foreground, press Ctrl-C. For a command executing in the background,
see under “process”: “Abort a process.” Re-execute the previously entered command: !! For other
options in executing previously-entered commands, see “command, re-execute a” [not
shown].Appendix B — A Unix Environment (Partial) 128 Make a command begin execution after a
specified time: See “sleep” in man pages. Make a command take its input from a file: <command
name> < <file name> Put the output of a command into a file, erasing existing contents of file if file
already exists: <command name> > <file name> For more details, see under “command, redirecting
output of a” [not shown]. Make the output of a command be the input of the next command:
<command name> | <next command name> For more details, see under “commands, pipelining of”
[not shown]. Make a command continue executing even after you log out: See “nohup” in man
pages. Change the priority of execution of a command: See “nice” in man pages. Abbreviate a
(typically long) command: alias <abbreviation> <command> Related See: “event” “process” “script”
BASIC OF UNIX
BASIC OF UNIX
directory
Definition of “directory”: See “directory, definition of” [not shown]. Appendix B — A Unix
Environment (Partial) 129 Tasks to find information about directories Display the name of the
current directory: pwd Display properties of current directory, including “permission”s: ls -l -d For
explanation of results, see “ls” in man pages. Display names of all directories below the current
directory having a given partial “pathname:” find . -name ’* <partial file name>* ’ -type d -print
Other tasks on directories Connect to a specified directory, i.e., make it the current directory: cd
<directory name> Connect to your login name’s “home directory”: cd Create a directory: mkdir
<directory name> Delete a directory: 1. Delete all files in the directory. 2. Enter: rmdir <directory
name> Copy a directory to floppy disk(s): See “bar”, “tar” commands in man pages. Copy a
directory from floppy disk(s) to hard disk: See “bar”, “tar” commands in man pages. Copy a
directory to another directory:Appendix B — A Unix Environment (Partial) 130 See “mount” in man
pages. Change permissions on a directory: See “chmod” in man pages. Related “file” “file system”
[not shown]
BASIC OF UNIX
BASIC OF UNIX
disk, hard
Definition of “hard disk”: See “disk, hard, definition of” [not shown]. Tasks to find information
about hard disks Display a listing of id’s of all hard disks you currently have access to: See “df” in
man pages. Find out memory usage on hard disk x du Other tasks on hard disks Copy a file to floppy
disk(s): See “bar”, “tar” commands in man pages. Copy a file from floppy disk(s) to hard disk: See
“bar”, “tar” commands in man pages. Related “main memory” [not shown]Appendix B — A Unix
Environment (Partial) 131 “RAM” [not shown] “ROM” [not shown]
BASIC OF UNIX
BASIC OF UNIX
file
Definition of “file”: See “file, definition of” [not shown]: Tasks to find information about files List
names of each file in current directory having string x in its name: ls * x* Display properties of a file:
ls -l <file name(s)> Display type of a file: See “file” in man pages. Display permissions for a file
named x: ls -l x Count number of lines, words, characters, in a file named x: wc x Display all lines in
a file which contain a specified string: grep <string> <file name> See also “awk” in man pages.
Search for information, patterns in a file See “grep”, “diff” in man pages. Other tasks on files Create
a file:Appendix B — A Unix Environment (Partial) 132 See “cat” in man pages, or use editor, e.g.,
vi. Copy a file within a given machine, server: cp <current file name> <new file name> Copy a file
across a network: See “cpio”, “ftp”, “bar”, “tar” in man pages. Copy a file to, from, floppy disks: For
file(s) occupying more than one disk, use “bar” command. (See man pages.) See also “tar” command
in man pages. Delete file(s): rm <file name(s)> Move file within a given machine, server: mv
<current file name> <new file name> Concatenate two or more files into a new file which contains
the originals in the order file1, file2, file3, ..., filen: cat file1, file2, file3, ..., filen > newfile Merge two
sorted files into a new, sorted file: See “sort” in man pages. Find a file: E.g., find, in any directory in
or below the present directory, all files whose name contains the string x, and print out the full name
of each such file: find . -name ’* *x* ’ -print Rename a file: mv <current name of file> <new name of
file> Change permissions on a file: chmod <new permissions mask> <file name>Appendix B — A
Unix Environment (Partial) 133 Display contents of a file named x: Use more x, or cat x, or head x,
or tail x or use editor, e.g., vi. Compare contents of two files: See “diff”, “cmp”, “comm” in man
pages. Find a string or other pattern in a file: grep <string> <file name> See also “awk” in man
pages. Change contents of a file: Use an editor, e.g., vi. See also “awk”, “sort” in man pages.
Compress a file. See “compress” in man pages. Related: “uncompress”. Display classification of a
file. See “file” in man pages. Display contents of a file. See “edit a file” below. Edit a file. See
“Emacs” [not shown], “FrameMaker” [not shown], “sed” [not shown], “Text Editor” [not shown],
“vi” [not shown]. Encode a binary file as ASCII for, e.g., transmission via e-mail See “uuencode” in
man pages. .Convert format of a file to Postscript format: See “enscript” in man pages. Establish file
creation permissions mask: See “umask” in man pages. Change permissions on a file:Appendix B —
A Unix Environment (Partial) 134 See “chmod” in man pages. Compress a file. See “compress” in
man pages. Related: “uncompress”. Rename file. mv <current file> <file of new name> Modify time-
related parameters of a file: See “touch” in man pages. Uncompress a file. See “uncompress” in man
pages. Related: “Compress” Related “directory” “file system” [not shown]
BASIC OF UNIX
BASIC OF UNIX
memory
process See “CD-ROM” [not shown], “disk”, “main memory” [not shown], “RAM” [not shown],
“ROM” [not shown], “floppy disk”.
Definition of “process”: See “process, definition of” [not shown]. Appendix B — A Unix
Environment (Partial) 135 Tasks to find information about processes Get a list of all current
processes: ps -auxww Other tasks on processes Abort a process: 1. Find the “PID” of the process by
entering: ps 2. Kill the process by entering: kill PID Related “command” “event” [not shown] “shell”
BASIC OF UNIX
BASIC OF UNIX
screen
Definition of “screen”: See “screen, definition of” [not shown]. Other tasks on screens Make a record
of everything that appears on the screen: Enter: script <name of file in which to record screen
contents> To terminate record, press: Ctrl-d Erase screen .Appendix B — A Unix Environment
(Partial) 136 .. Refresh screen ... Related “terminal” “windowing systems” [not shown]
BASIC OF UNIX
BASIC OF UNIX
script
Definition of “script”: See “script, definition of” [not shown]. Other tasks on scripts Create a script:
See “script, create a”. Edit a script: Use any editor, e.g., vi. View an existing script: See “script, view
a”. Set execute permissions on a script: See “script, set execute permissions on a” [not shown]. Run a
script: See “script, run a”. Debug a script: See “script, debug a”.Appendix B — A Unix Environment
(Partial) 137 Related “program”
BASIC OF UNIX
BASIC OF UNIX
shell
space Definition of “shell”: See “shell, definition of” [not shown]. Tasks to find information about
shells Display symbol identifying current shell: ps In result, “csh” stands for “C Shell” [not shown],
“sh” stands for “Bourne Shell” [not shown], “ksh” stands for “Korn Shell” [not shown]. Other tasks
on shells Write a program using shell commands: See “script”. Related “Bourne Shell” [not shown]
“C Shell” [not shown] “Korn Shell” [not shown]
See “disk”, “main memory” [not shown], “RAM” [not shown], “ROM” [not shown]. Appendix B —
A Unix Environment (Partial) 138
BASIC OF UNIX
BASIC OF UNIX
terminal
user Definition of “terminal” See “terminal, definition of” [not shown]. Tasks to find information
about terminals Display a list of names or id’s of all terminals on system: ... Find out who owns a
given terminal: ... Find out what terminals are assigned to a given user: ... Find out name, id,
properties of your current terminal ... Sign on to another user’s terminal: ... Related “screen”
“workstation” [not shown]
Definition of “user”, i.e., assumed minimum vocabulary and skills of any user of this Unix
Environment: Appendix B — A Unix Environment (Partial) 139 See “user, definition of”. Tasks to
find information about users Display list of names of other users ... Other tasks on users Send
message to other user(s): See “e-mail” [not shown], “mail” [not shown]. Make up a mailing list of
users: See “alias” in man pages. Sign on to another user’s terminal rlogin <name of user’s terminal>
Related “superuser” [not shown] “system administrator” [not shown]
BASIC OF UNIX
BASIC OF UNIX
variable, Environment
Definition of Environment variable: See “variable, Environment, definition of” [not shown]. Tasks to
find information about Environment variables List all current Environment variables:
printenvAppendix B — A Unix Environment (Partial) 140 Other tasks on Environment variables
Create an Environment variable setenv <Environment variable name> <value> Change the value of
an Environment variable: setenv <Environment variable name> <value> Make recently entered
values of Environment variables take effect throughout current shell: source Related “script” “script
variables” “shell”