UNIX Commands: CTRL+D - Possible Completer CTRL+C - Cancel Foreground Job CTRL+Z - Stop (Interrupted) A Foreground Job
UNIX Commands: CTRL+D - Possible Completer CTRL+C - Cancel Foreground Job CTRL+Z - Stop (Interrupted) A Foreground Job
UNIX Commands
To Create a File:
Syntax: $ pwd
Ex: $ pwd
REMOVE COMMANDS:
To Remove a file
To Remove Directory
LIST COMMANDS
$ ls list a directory
$ ls -l list a directory in long (detailed) format
for example:
$ ls -l
drwxr-xr-x 4 vijay user 1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r--r-- 1 kiran user 767392 Jun 6 14:28 scanlib.tar.gz
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | | |
| | | | | owner group size date time name
| | | | number of links to file or directory contents
| | | permissions for world(others)
| | permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no
permission
COPY COMMANDS
1. File to a File
2. File to a Dictionary
3. Directory to Directory
File to File
2. File to Directory
Ex : $ cp -b venkat satya
Note : It copys the venkat file to Satya Directory with backup
3. Directory to Directory
Ex : $ cp -r Mody Venkat
Note : It copies the Directory to Directory (including all files and Directories)
Note : It copies the Directory to Directory (including with all files and directories with
interactive mode)
MOVE COMMANDS
This command is used to move the files and directories one place to another place
1. File to File
2. File to Directory
3. Directory to Directory
PATH IN UNIX
1. Absolute path
2. Relative path
Ex: $ cd /b1/b2/b3/b4/b5
: $ cp /usr/paramesh/a1/a2/a3/file <Directory>
-rwx------
drwxr--r--
-rwx-w--w-
d - Directory
w - Wrire
r - Read
x - Executable
Note: Without write permissions we can’t copy, remove, modify, move, crate
drwxrwxrwx
777
Directory – 777
File – 666
Default Directory Permissions – 755
Default File Permissions – 644
Read –4
Write –2
Executable –1
Total Permissions 7
1. Numeric Method
2. Symbolic Method
1. Numeric Method:
To change the permissions and check the permission using following command
Ex: $ chmod -c 750 Dir2
2. Symbolic Method
User – u
Group – g
Others – o
All –a
Read – r
Write – w
Execute – x
Directory File
777 666
022 022
755 644
WILD CARDS
^ - Carrot or Cap
? - Single Character
* - Multiple Characters
[ ] - Range of Characters
Remove Commands
Links
1. Hard link
2. Soft Link
FIND COMMANDS
Ex: $ find ~ -type f -name “file1” exce cat {}\ exce rm{}\;
It searches the absolute path of the filename and displays
the content of file, and removes at the same time.
Time
-a – Access
-c – Changed
-m – Modified
Vi is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain
text. It is especially useful for editing programs.
1. Esc mode
2. Insert mode
3. Colon mode
A, a
I, i
O, o
S With the help of these keys move esc mode to inert mode
Shift: with the help of shift: move to Esc mode to colon mode
By press
'L' - The cursor moves right direction
'h' - The cursor moves one character left
'k' - The cursor moves one character up
'j' - The cursor moves one character down
Paste Commands
Replace Commands
Special Commands
Search Command
:w - To save a file
:q - To quit a file without save
:wq! - To save and quit a file forcibly
:senu - To display record numbers
:se nonu - To remove display record numbers
Redirection
Ex: $ cat < file1 >file2
It redirects the file1 to file2
FILTERS
Filter is a command or program, which takes the input from the standard input and then
process data to give required output
More 1. grep
less 2. sed
pg 3. awk
head
tail
wc
tee
tr
sort
cut
uniq
1. More: More is a filter to display the paging through text page by page
2. less : Less is a program similar to more (1), but which allows backward movement in the
file as well as forward movement. Also, less does not have to read the entire input file
before starting, so with large input files it starts up faster than text editors like vi
q – Quit
In-between records
Records from 15 to 20
Syntax: $ wc <filename>
Ex: $ wc kiran
no of records to display
Ex: $ wc -l <filename>
Ex: $ wc -c <filename>
Ex: $ ls -l <filename>
$ ls -l | tee <filename>
$ ls --full-time | tee -a <filename> - appending
existing file
Note: without | (pipe symbol) tee filter don’t works
student filename
01 vijay 60 90
12 anil 70 75
32 sujan 65 40
04 hari 70 45
22 raju 85 50
Sorting priority
1. Blank space
2. Special character
3. Numeric
4. Uppercase
5. Lowercase
Numeric:
If u want permanent
Ex: $ sort -nr <filename> > new file
1. Character cutting
2. Field cutting
-d field separator
Ex: $ ls -l | tr -s “ “
Sequence space
Advance Filters
1. Grep
The grep utilities are a family of Unix tools, including grep, egrep, and fgrep, that perform
repetitive searching tasks. The tools in the grep family are very similar, and all are used for
searching the contents of files for information that matches particular criteria. For most
purposes, you'll want to use fgrep, since it's generally the fastest
You can use fgrep to find all the lines of a file that contain a particular word. For example,
to list all the lines of a file named my file in the current directory that contain the word
"dog", enter at the Unix prompt:
This will also return lines where "dog" is embedded in larger words, such as "dogma" or
"dogged". You can use the -w option with the grep command to return only lines where
"dog" is included as a separate word:
To search for several words separated by spaces, enclose the whole search string in quotes,
for example:
The fgrep command is case sensitive; specifying "dog" will not match "Dog" or "DOG".
You can use the -i option with the grep command to match both upper- and lowercase
letters:
To list the lines of myfile that do not contain "dog", use the -v option:
If you want to search for lines that contain any of several different words, you can create a
second file (named second file in the following example) that contains those words, and
then use the -f option:
You can also use wildcards to instruct fgrep to search any files that match a particular
pattern. For example, if you wanted to find lines containing "dog" in any of the files in your
directory with names beginning with "my", you could enter:
This command would search files with names such as my file, my.hw1, and my stuff in the
current directory. Each line returned would be prefaced with the name of the file where the
match was found.
By using pipes and/or redirection, you can use the output from any of these commands with
other Unix tools, such as more, sort, and cut. For example, to print the fifth word of every
line of my file containing "dog", sort the words alphabetically, and then filter the output
through the more command for easy reading, you would enter at the Unix prompt:
Ex: fgrep dog myfile | cut -f5 -d" " | sort | more
If you want to save the output in a file in the current directory named new file, enter:
Ex: fgrep dog my file | cut -f5 -d" " | sort > new file
$ grep –w <file> - To search for the exact match for the word
2. SED
Sed is a stream editor. A stream editor is used to perform basic text transformations on an
input stream (a file or input from a pipeline). While in some ways similar to an editor which
permits scripted edits (such as ed), sed works by making only one pass over the input(s),
and is consequently more efficient. But it is sed's ability to filter text in a pipeline, which
particularly distinguishes it from other types of editors.
Sed works as follows: it reads from the standard input, one line at a time. for each line, it
executes a series of editing commands, then the line is written to STDOUT. An example
that shows how it works: we use the s command. s means "substitute" or search and
replace. The format is
s/regular-expression/replacement text/{flags}
We won't discuss all the flags yet. The one we use below is g, which means, "replace all
matches"
>cat file
I have three dogs and two cats
>sed -e 's/dog/cat/g' -e 's/cat/elephant/g' file
I have three elephants and two elephants
>
OK. So what happened? Firstly, sed read in the line of the file and executed
s/dog/cat/g
and then the second command was performed on the edited line and the result was
We actually have a name for the "current text": it is called the pattern space. So a precise
definition of what sed does is as follows:
sed reads the standard input into the pattern space, performs a sequence of editing
commands on the pattern space, then writes the pattern space to STDOUT.
OR
Syntax: sed [option] ‘address/action’ <file>
so sed can read from a file or STDIN, and the commands can be specified in a file or on the
command line. Note the following:
That if the commands are read from a file, trailing white space can be fatal, in
particular, it will cause scripts to fail for no apparent reason. I recommend editing
sed scripts with an editor such as vim, which can show end of line characters so that
you can "see" trailing white space at the end of line.
Substitute
[address1[ ,address2]]s/pattern/replacement/[flags]
w file Write the pattern space to file if a successful substitution takes place
Delete
The delete command is very simple in its syntax: it goes like this
[address1[ , address2 ] ]d
And it deletes the content of the pattern space. All following commands are skipped
(after all, there's very little you can do with an empty pattern space), and a new line
is read into the pattern space.
Example 1
>cat file
http://www.foo.com/mypage.html
>sed -e 's@http://www.foo.com@http://www.bar.net@'
file
http://www.bar.net/mypage.html
Note that we used a different delimiter, @ for the substitution command. Sed permits
several delimiters for the s command including @%,;: these alternative delimiters are good
for substitutions which include strings such as filenames, as it makes your sed code much
more readable.
Example 2
>cat file
Example 3
>cat file
In this instance, the substitution is only applied to lines matching the regular expression
not. Hence it is not applied to the first line.
Example 4
>cat file
line 1 (one)
line 2 (two)
line 3 (three)
Example 4a
Example 4b
Example 4c
LINE 1 (one)
LINE 2 (two)
line 3 (three)
Example 4d
LINE 1 (one)
LINE 1 (one)
So now the second command is executed, but since the pattern space does not match the
regular expression line, the delete command is not executed.
Example 5
>cat file
hello
this text is wiped out
Wiped out
hello (also wiped out)
WiPed out TOO!
goodbye
(1) This text is not deleted
(2) neither is this ... ( goodbye )
(3) neither is this
hello
but this is
and so is this
and unless we find another g**dbye
every line to the end of the file gets deleted
This illustrates how the addressing works when two pattern addresses are specified. sed
finds the first match of the expression "hello", deleting every line read into the pattern
space until it gets to the first line after the expression "goodbye". It doesn't apply the delete
command to any more addresses until it comes across the expression "hello" again. Since
the expression "goodbye" is not on any subsequent line, the delete command is applied to
all remaining lines.
AWK
It was created in late 70th of the last century. The name was composed from the initial
letters of three original authors Alfred V. Aho, Brian W. Kernighan, and Peter J.
Weinberger. It is commonly used as a command-line filter in pipes to reformat the output
of other commands. It's the precursor and the main inspiration of Perl. Although originated
in Unix it is available and widely used in Windows environment too.
AWK takes two inputs: data file and command file. The command file can be absent and
necessary commands can be passed as augments. As Ronald P. Loui aptly noted awk is very
under appreciated language:
The main advantage of AWK is that unlike Perl and other "scripting monsters" that it is
very slim without feature creep so characteristic of Perl and thus it can be very efficiently
used with pipes. Also it has rather simple, clean syntax and like much heavier TCL can be
used with C for "dual-language" implementations.
awk's favor compared to perl:
The basic function of awk is to search files for lines (or other units of text) that contain
certain patterns. When a line matches one of the patterns, awk performs specified actions
on that line. awk keeps processing input lines in this way until it reaches the end of the
input files
Logical Operator
&& - AND
|| - OR
1. BIGIN
2. MIDDLE
3. END
Begin is keyword for the begin section the variable can be assign in begin section
All the operator in the middle sections, Middle is not keyword for the middle section
What ever u print every thing in the section, End is the keyword for the end section
$ awk ‘/ajay/{print}’<file>
It prints the all the records
$ awk ‘/ajay|ramu/{print}’<file>
To search for multiple expressions and print
$ awk ‘NR==3,NR==7{print}’<file>
To print range of records
The sole rule has a relational expression as its pattern and it has no action—so the
default action, printing the record, is used.
The input is processed by the expand utility to change tabs into spaces, so the
widths compared are actually the right-margin columns.
This is an easy way to delete blank lines from a file (or rather, to create a new file
similar to the old file but from which the blank lines have been removed).
If you use the expression `NR % 2 == 1' instead, the program would print the odd-
numbered lines
EXAMPLES
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
# Print hi 28 times
yes | head -28 | awk '{ print "hi" }'
4. Killing processes by name (in this example we kill the process called netscape):
kill `ps auxww | grep netscape | egrep -v grep | awk
'{print $2}'`
Environment Control
Command Description
cd d Change to directory d
mv d1 d2 Rename directory d1 as d2
Command Description
lpr -P printer f
or
lp -d printer f Output file f to line printer
Process Control
Command Description
Environment Status
Command Description
File Manipulation
Command Description
mv f1 f2 Rename file f1 as f2
Compiler
Command Description
d directory
env environment
f filename
n number
nd computer node
prtr printer
ptn pattern
var variable
[y/n] yes or no