UNIX BASIC COMMANDS: LS
The ls command lists all files in the directory that match the name. If name is left blank, it will list all
of the files in the directory.
SYNTAX
The syntax for the ls command is:
ls [options] [names]
OPTIONS
Option
Description
-a
Displays all files.
-b
Displays nonprinting characters in octal.
-c
Displays files by file timestamp.
-C
Displays files in a columnar format (default)
-d
Displays only directories.
-f
Interprets each name as a directory, not a file.
-F
Flags filenames.
-g
Displays the long format listing, but exclude the owner name.
-i
Displays the inode for each file.
-l
Displays the long format listing.
-L
Displays the file or directory referenced by a symbolic link.
-m
Displays the names as a comma-separated list.
-n
Displays the long format listing, with GID and UID numbers.
-o
Displays the long format listing, but excludes group name.
-p
Displays directories with /
-q
Displays all nonprinting characters as ?
-r
Displays files in reverse order.
-R
Displays subdirectories as well.
-t
Displays newest files first. (based on timestamp)
-u
Displays files by the file access time.
-x
Displays files as rows across the screen.
-1
Displays each entry on a line.
EXAMPLE
ls -la
UNIX BASIC COMMANDS: CAT
The cat command reads one or more files and prints them to standard output. The operator > can be
used to combine multiple files into one. The operator >> can be used to append to an existing file.
SYNTAX
The syntax for the cat command is:
cat [options] [files]
OPTIONS
Option
Description
-e
$ is printed at the end of each line. This option must be used with -v.
-s
Suppress messages pertaining to files that do not exist.
-t
Each tab will display as ^I and each form feed will display as ^L. This option must be used
with -v.
-u
Output is printed as unbuffered.
-v
Display control characters and nonprinting characters
EXAMPLE
cat file1
cat file1 file2 > all
cat file1 >> file2
UNIX BASIC COMMANDS: CD
The cd command changes directory.
SYNTAX
The syntax for the cd command is:
cd [dir]
EXAMPLE
cd tech
cd /tech
UNIX BASIC COMMANDS: MKDIR
The mkdir command creates a single directories or multiple directories.
SYNTAX
The syntax for the mkdir command is:
mkdir [options] directories
OPTIONS
Option
Description
-m mode
Sets the access mode for the new directory.
-p
If the parent directories don't exist, this command creates them.
EXAMPLE
mkdir -m 444 tech
mkdir -p tech/net/faqs
UNIX BASIC COMMANDS: MV
The mv command allows you to move and rename files.
SYNTAX
The syntax for the mv command is:
mv [options] sources target
OPTIONS
Option
Description
-f
Forces the move.
-i
Prompt for a confirmation before overwriting any files.
EXAMPLE
mv -f tech /usr
UNIX BASIC COMMANDS: TELNET
The telnet command allows you to communicate to another host using the TELNET protocol.
SYNTAX
The syntax for the telnet command is:
telnet [host [port]]
UNIX BASIC COMMANDS: FTP
The ftp command allows you to transfer files to and from a remote server.
SYNTAX
The syntax for the ftp command is:
ftp [options] [hostname]
OPTIONS
Option
Description
-d
Debugging is enabled.
-g
Filename globbing is disabled.
-i
Interactive prompting is disabled.
-n
When you are initially connecting, auto-login is disabled.
-v
Display all responses from the server.
EXAMPLE
ftp tech
ftp -v tech
UNIX BASIC COMMANDS: KILL
The kill command allows you to kill one process ID or multiple process IDs.
SYNTAX
The syntax for the kill command is:
kill [options] IDs
OPTIONS
Option
Description
-l
Lists the signal names.
-signal
The signal number of name. Using the signal number of 9, means that the kill is absolute.
EXAMPLE
kill -9 78689
UNIX BASIC COMMANDS: PS
The ps command displays active processes.
SYNTAX
The syntax for the ps command is:
ps [options]
OPTIONS
Option
Description
-a
Displays all processes on a terminal, with the exception of group leaders.
-c
Displays scheduler data.
-d
Displays all processes with the exception of session leaders.
-e
Displays all processes.
-f
Displays a full listing.
-glist
Displays data for the list of group leader IDs.
-j
Displays the process group ID and session ID.
-l
Displays a long listing
-plist
Displays data for the list of process IDs.
-slist
Displays data for the list of session leader IDs.
-tlist
Displays data for the list of terminals.
-ulist
Displays data for the list of usernames.
EXAMPLE
ps -ef
ps -aux
UNIX BASIC COMMANDS: GREP
The grep command allows you to search one file or multiple files for lines that contain a pattern. Exit
status is 0 if matches were found, 1 if no matches were found, and 2 if errors occurred.
SYNTAX
The syntax for the grep command is:
grep [options] pattern [files]
OPTIONS
Option
Description
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.
EXAMPLE
grep -c tech file1
dd commands:
he dd command copies a file, converting the format of the data in the
process, according to the operands specified.
Syntax
dd [OPERAND]...
dd OPTION
Operands
bs=BYTES
read and write BYTES bytes at a time (also see ibs=,obs=)
cbs=BYTES convert BYTES bytes at a time
conv=CONV convert the file as per the comma separated symbol list. Each symbol may b
S
following, and represents a specific type of conversion:
ascii
from EBCDIC to ASCII
ebcdic
from ASCII to EBCDIC
ibm
from ASCII to alternate EBCDIC
block
pad newline-terminated records with spaces to cbs-size
unblock
replace trailing spaces in cbs-size records with newline
lcase
change upper case to lower case
nocreat
do not create the output file
excl
fail if the output file already exists
notrunc
do not truncate the output file
ucase
change lower case to upper case
swab
swap every pair of input bytes
noerror
continue after read errors
sync
pad every input block with NULs to ibs-size; when used with block or unbloc
spaces rather than NULs
fdatasync
physically write output file data before finishing
fsync
likewise, but also write metadata.
count=BLOC copy only BLOCKS input blocks
KS
ibs=BYTES
read BYTES bytes at a time (default: 512)
if=FILE
read from FILE instead of stdin
iflag=FLAGS read as per the comma separated symbol list. Each symbol may be one of th
append
append mode (makes sense only for output; conv=notrunc suggested)
direct
use direct I/O for data
directory
fail unless a directory
dsync
use synchronized I/O for data
sync
likewise, but also for metadata
fullblock
accumulate full blocks of input (iflag only)
nonblock
use non-blocking I/O
noatime
do not update access time
noctty
do not assign controlling terminal from file
nofollow
do not follow symlinks.
obs=BYTES write BYTES bytes at a time (default: 512)
of=FILE
write to FILE instead of stdout
oflag=FLAG write as per the comma separated symbol list
S
seek=BLOC skip BLOCKS obs-sized blocks at start of output
KS
skip=BLOCK skip BLOCKS ibs-sized blocks at start of input
S
status=noxf suppress transfer statistics
er
Flow chart
A flowchart is a visual representation of the sequence of steps and decisions needed to
perform a process. Each step in the sequence is noted within a diagram shape. Steps are
linked by connecting lines and directional arrows. This allows anyone to view the flowchart
and logically follow the process from beginning to end
Unix - The vi Editor
There are many ways to edit files in Unix and for me one of the best ways is
using screen-oriented text editor vi. This editor enable you to edit lines in
context with other lines in the file.
Now a days you would find an improved version of vi editor which is
called VIM. Here VIM stands for Vi IMproved.
The vi is generally considered the de facto standard in Unix editors because
It's usually available on all the flavors of Unix system.
Its implementations are very similar across the board.
It requires very few resources.
It is more user friendly than any other editors like ed or ex.
You can use vi editor to edit an existing file or to create a new file from
scratch. You can also use this editor to just read a text file.
Starting the vi Editor
There are following way you can start using vi editor
Command
Description
vi filename
Creates a new file if it already does not exist, otherwise opens
existing file.
vi -R filename
Opens an existing file in read only mode.
view filename
Opens an existing file in read only mode.
Following is the example to create a new file testfile if it already does not
exist in the current working directory
$vi testfile
As a result you would see a screen something like as follows
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
You will notice a tilde (~) on each line following the cursor. A tilde represents an
unused line. If a line does not begin with a tilde and appears to be blank, there
is a space, tab, newline, or some other nonviewable character present.
Operation Modes
While working with vi editor you would come across following two modes
Command mode This mode enables you to perform administrative tasks
such as saving files, executing commands, moving the cursor, cutting (yanking)
and pasting lines or words, and finding and replacing. In this mode, whatever
you type is interpreted as a command.
Insert mode This mode enables you to insert text into the file. Everything
that's typed in this mode is interpreted as input and finally it is put in the file .
The vi always starts in command mode. To enter text, you must be in insert
mode. To come in insert mode you simply type i. To get out of insert mode,
press the Esc key, which will put you back into command mode.
Hint If you are not sure which mode you are in, press the Esc key twice, and
then you'll be in command mode. You open a file using vi editor and start type
some characters and then come in command mode to understand the
difference.
Getting Out of vi
The command to quit out of vi is :q. Once in command mode, type colon,
and 'q', followed by return. If your file has been modified in any way, the
editor will warn you of this, and not let you quit. To ignore this message,
the command to quit out of vi without saving is :q!. This lets you exit vi
without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the
above command with the quit command, or :wq and return.
The easiest way to save your changes and exit out of vi is the ZZ command.
When you are in command mode, type ZZ and it will do the equivalent of
:wq.
You can specify a different file name to save to by specifying the name after
the :w. For example, if you wanted to save the file you were working as
another filename called filename2, you would type :w filename2 and
return. Try it once.
Moving within a File
To move around within a file without affecting your text, you must be in
command mode (press Esc twice). Here are some of the commands you can
use to move around one character at a time
Command
Description
Moves the cursor up one line.
Moves the cursor down one line.
Moves the cursor to the left one character position.
Moves the cursor to the right one character position.
There are following two important points to be noted
The vi is case-sensitive, so you need to pay special attention to capitalization
when using commands.
Most commands in vi can be prefaced by the number of times you want the
action to occur. For example, 2j moves cursor two lines down the cursor
location.
There are many other ways to move within a file in vi. Remember that you must
be in command mode (press Esc twice). Here are some more commands you
can use to move around the file
Command
Description
0 or |
Positions cursor at beginning of line.
Positions cursor at end of line.
Positions cursor to the next word.
Positions cursor to previous word.
Positions cursor to beginning of current sentence.
Positions cursor to beginning of next sentence.
Move to the end of Blank delimited word
Move a paragraph back
Move a paragraph forward
[[
Move a section back
]]
Move a section forward
n|
Moves to the column n in the current line
1G
Move to the first line of the file
Move to the last line of the file
nG
Move to nth line of the file
:n
Move to nth line of the file
fc
Move forward to c
Fc
Move back to c
Move to top of screen
nH
Moves to nth line from the top of the screen
Move to middle of screen
Move to botton of screen
nL
Moves to nth line from the bottom of the screen
:x
Colon followed by a number would position the cursor on line
number represented by x
Control Commands
There are following useful command which you can use along with Control
Key
Command
Description
CTRL+d
Move forward 1/2 screen
CTRL+d
Move forward 1/2 screen
CTRL+f
Move forward one full screen
CTRL+u
Move backward 1/2 screen
CTRL+b
Move backward one full screen
CTRL+e
Moves screen up one line
CTRL+y
Moves screen down one line
CTRL+u
Moves screen up 1/2 page
CTRL+d
Moves screen down 1/2 page
CTRL+b
Moves screen up one page
CTRL+f
Moves screen down one page
CTRL+I
Redraws screen
Editing Files
To edit the file, you need to be in the insert mode. There are many ways to
enter insert mode from the command mode
Command
Description
Inserts text before current cursor location.
Inserts text at beginning of current line.
Inserts text after current cursor location.
Inserts text at end of current line.
Creates a new line for text entry below cursor location.
Creates a new line for text entry above cursor location.
Deleting Characters
Here is the list of important commands which can be used to delete
characters and lines in an opened file
Command
Description
Deletes the character under the cursor location.
Deletes the character before the cursor location.
dw
Deletes from the current cursor location to the next word.
d^
Deletes from current cursor position to the beginning of the
line.
d$
Deletes from current cursor position to the end of the line.
Deletes from the cursor position to the end of the current
line.
dd
Deletes the line the cursor is on.
As mentioned above, most commands in vi can be prefaced by the number
of times you want the action to occur. For example, 2x deletes two
character under the cursor location and 2dd deletes two lines the cursor is
on.
I would highly recommend to exercise all the above commands properly
before proceeding further.
Change Commands
You also have the capability to change characters, words, or lines in vi
without deleting them. Here are the relevant commands
Command
Description
cc
Removes contents of the line, leaving you in insert mode.
cw
Changes the word the cursor is on from the cursor to the
lowercase w end of the word.
Replaces the character under the cursor. vi returns to command
mode after the replacement is entered.
Overwrites multiple characters beginning with the character
currently under the cursor. You must use Esc to stop the
overwriting.
Replaces the current character with the character you type.
Afterward, you are left in insert mode.
Deletes the line the cursor is on and replaces with new text. After
the new text is entered, vi remains in insert mode.
Copy and Past Commands
You can copy lines or words from one place and then you can past them at
another place using following commands
Command
Description
yy
Copies the current line.
yw
Copies the current word from the character the lowercase w
cursor is on until the end of the word.
Puts the copied text after the cursor.
Puts the yanked text before the cursor.
Advanced Commands
There are some advanced commands that simplify day-to-day editing and
allow for more efficient use of vi
Command
Description
Join the current line with the next one. A count joins that many
lines.
<<
Shifts the current line to the left by one shift width.
>>
Shifts the current line to the right by one shift width.
Switch the case of the character under the cursor.
^G
Press CNTRL and G keys at the same time to show the current
filename and the status.
Restore the current line to the state it was in before the cursor
entered the line.
Undo the last change to the file. Typing 'u' again will re-do the
change.
Join the current line with the next one. A count joins that many
lines.
:f
Displays current position in the file in % and file name, total
number of file.
:f filename
Renames current file to filename.
:w filename
Write to file filename.
:e filename
Opens another file with filename.
:cd dirname
Changes current working directory to dirname.
:e #
Use to toggle between two opened files.
:n
In case you open multiple files using vi, use :n to go to next file
in the series.
:p
In case you open multiple files using vi, use :p to go to previous
file in the series.
:N
In case you open multiple files using vi, use :N to go to
previous file in the series.
:r file
Reads file and inserts it after current line
:nr file
Reads file and inserts it after line n.
Word and Character Searching
The vi editor has two kinds of searches: string and character. For a string
search, the / and ? commands are used. When you start these commands,
the command just typed will be shown on the bottom line, where you type
the particular string to look for.
These two commands differ only in the direction where the search takes
place
The / command searches forwards (downwards) in the file.
The ? command searches backwards (upwards) in the file.
The n and N commands repeat the previous search command in the same
or opposite direction, respectively. Some characters have special meanings
while using in search command and preceded by a backslash (\) to be
included as part of the search expression.
Character
Description
Search at the beginning of the line. (Use at the beginning of a
search expression.)
Matches a single character.
Matches zero or more of the previous character.
End of the line (Use at the end of the search expression.)
Starts a set of matching, or non-matching expressions.
<
Put in an expression escaped with the backslash to find the
ending or beginning of a word.
>
See the '<' character description above.
The character search searches within one line to find a character entered
after the command. The f and F commands search for a character on the
current line only. f searches forwards and F searches backwards and the
cursor moves to the position of the found character.
The t and T commands search for a character on the current line only, but
for t, the cursor moves to the position before the character, and T searches
the line backwards to the position after the character.
Set Commands
You can change the look and feel of your vi screen using the
following :setcommands. To use these commands you have to come in
command mode then type :set followed by any of the following options
Command
Description
:set ic
Ignores case when searching
:set ai
Sets autoindent
:set noai
To unset autoindent.
:set nu
Displays lines with line numbers on the left side.
:set sw
Sets the width of a software tabstop. For example you would set a
shift width of 4 with this command: :set sw=4
:set ws
If wrapscan is set, if the word is not found at the bottom of the
file, it will try to search for it at the beginning.
:set wm
If this option has a value greater than zero, the editor will
automatically "word wrap". For example, to set the wrap margin to
two characters, you would type this: :set wm=2
:set ro
Changes file type to "read only"
:set term
Prints terminal type
:set bf
Discards control characters from input
Running Commands
The vi has the capability to run commands from within the editor. To run a
command, you only need to go into command mode and type :! command.
For example, if you want to check whether a file exists before you try to
save your file to that filename, you can type :! ls and you will see the
output of ls on the screen.
When you press any key (or the command's escape sequence), you are
returned to your vi session.
Replacing Text
The substitution command (:s/) enables you to quickly replace words or
groups of words within your files. Here is the simple syntax
:s/search/replace/g
The g stands for globally. The result of this command is that all occurrences
on the cursor's line are changed.
IMPORTANT
Here are the key points to your success with vi
You must be in command mode to use commands. (Press Esc twice at any time
to ensure that you are in command mode.)
You must be careful to use the proper case (capitalization) for all commands.
You must be in insert mode to enter text.
ls --- lists your files
ls -l --- lists your files in 'long format', which contains lots of useful
information, e.g. the exact size of the file, who owns the file and who
has the right to look at it, and when it was last modified.
ls -a --- lists all files, including the ones whose filenames begin in a
dot, which you do not always want to see.
mv filename1 filename2 --- moves a file (i.e. gives it a
different name, or moves it into a different directory (see
below)
cp filename1 filename2 --- copies a file
rm filename --- removes a file. It is wise to use the option rm
-i, which will ask you for confirmation before actually deleting
anything. You can make this your default by making an alias in
your .cshrc file.
mkdir dirname --- make a new directory
cd dirname --- change directory. You basically 'go' to another directory, and
you will see the files in that directory when you do 'ls'. You always start out in
your 'home directory', and you can get back there by typing 'cd' without
arguments. 'cd ..' will get you one level up from your current position
grep string filename(s) --- looks for the string in the files. This can be useful a
lot of purposes, e.g. finding the right file among many, figuring out which is the
right version of something, and even doing serious corpus work. grep comes in
several varieties (grep, egrep, and fgrep) and has a lot of very flexible options.
Check out the man pages if this sounds good to you.
kill PID --- kills (ends) the processes with the ID you gave. This works only for
your own processes, of course. Get the ID by using ps. If the process doesn't
'die' properly, use the option -9. But attempt without that option first, because it
doesn't give the process a chance to finish possibly important business before
dying. You may need to kill processes for example if your modem connection
was interrupted and you didn't get logged out properly, which sometimes
happens.
ps -u yourusername --- lists your processes. Contains lots of information about
them, including the process ID, which you need if you have to kill a process.
Normally, when you have been kicked out of a dialin session or have otherwise
managed to get yourself disconnected abruptly, this list will contain the
processes you need to kill. Those may include the shell (tcsh or whatever you're
using), and anything you were running, for example emacs or elm. Be careful
not to kill your current shell - the one with the number closer to the one of the
ps command you're currently running. But if it happens, don't panic. Just try
again :) If you're using an X-display you may have to kill some X processes
before you can start them again. These will show only when you use ps -efl,
because they're root processes.
telnet hostname --- also lets you connect to a remote host.
Use rlogin whenever possible.
ftp hostname --- lets you download files from a remote host which
is set up as an ftp-server. This is a common method for exchanging
academic papers and drafts. If you need to make a paper of yours
available in this way,
the name of the tar command is short for tape archiving, the storing of entire file
systems onto magnetic tape, which is one use for the command. However, a more
common use for tar is to simply combine a few files into a single file, for easy storage
and distribution.
To combine multiple files and/or directories into a single file, use the following command:
tar -cvf file.tar inputfile1 inputfile2
Replace inputfile1 and inputfile2 with the files and/or directories you want to combine.
You can use any name in place of file.tar, though you should keep the .tar extension. If
you don't use the f option, tar assumes you really do want to create a tape archive
instead of joining up a number of files. The v option tells tar to be verbose, which
reports all files as they are added.
To separate an archive created by tar into separate files, at the shell prompt, enter:
tar -xvf file.tar
dd
Delete current line and put in buffer
cat mytext.txt > newfile.txt