sesion 3 admin linux
TRANSCRIPT
-
8/14/2019 Sesion 3 Admin Linux
1/48
Editing Files
-
8/14/2019 Sesion 3 Admin Linux
2/48
Objectives
After completing this unit, you should be able to:Determine the type of file using file
Edit text files with vi
Discuss other text file editors such as kedit
Discuss the ways non-text files can be edited
-
8/14/2019 Sesion 3 Admin Linux
3/48
Editing Files
Use file command to determine the content of a file
To edit text files, use an editor
Non-text files can only be changed using the applicationthat created them, or with a "hex editor"
But most configuration files under Linux are text files!
$ file /etc/passwd
/etc/passwd: ASCII text
$ file /usr/bin/passwd
/usr/bin/passwd: ELF 32-bit LSB executable
-
8/14/2019 Sesion 3 Admin Linux
4/48
The "vi" text editor
Default editor in all UNIX operating systemsUsually the only editor available in emergencies
Relatively hard to learn, but really powerful
As a Linux user, you should be able to use vi for basicediting tasks
But it's OK if you prefer another editor for daily work
vi in Linux is usually vim (vi improved):Syntax highlightingArrow keys, Del, BS work in insert modeMulti level undoMouse support
-
8/14/2019 Sesion 3 Admin Linux
5/48
vi Modes
vi knows three modes of operationCommand mode (for simple, one-letter commands)Edit mode (insert text)ex mode (for complicated commands)
Can easily change between modes
Command mode Edit mode
ex mode
Allows you to type simple,
one-letter commandsAllows you to insert text
Allows you to execute
complex commands
-
8/14/2019 Sesion 3 Admin Linux
6/48
Starting vi
~
~
~
~~
~
~
~~
~
~
~~
~
~
"myfile.txt" [New File] 0,0-1 All
$ vi myfile.txt
-
8/14/2019 Sesion 3 Admin Linux
7/48
-
8/14/2019 Sesion 3 Admin Linux
8/48
Editing Text in Command Mode
To delete a single character under cursor xTo delete a single character left of cursor X
To replace a single character r
Undo the last change u
To repeat last command .
To join two lines together J
command mode
-
8/14/2019 Sesion 3 Admin Linux
9/48
Switching to Edit Mode
To insert text at begin of line ITo insert text before cursor i
To append text after cursor a
To append text at end of line A
To go back to command mode
Command mode Edit mode
IiAa
ESC
-
8/14/2019 Sesion 3 Admin Linux
10/48
Adding Text in Edit Mode
This file contains some lines.
Line 2.
And this is line 3.
Line 4 follows line 3.The last line is line 5.
~
~
~~
~
-- INSERT -- 3,8 All
Keystroke "i" switches vi to edit mode. New characters
can be inserted at the current position of the cursor
E iti th Edit M d
-
8/14/2019 Sesion 3 Admin Linux
11/48
Exiting the Edit Mode
This file contains some lines.
Line 2.
And this for example is line 3.
Line 4 follows line 3.The last line is line 5.
~
~
~~
~
~
3,8 All
Keystroke "ESC" leaves the edit mode.
S hi f P tt
-
8/14/2019 Sesion 3 Admin Linux
12/48
Searching for Patterns
To search for a pattern (in command mode): /To repeat the previous search: n
This file contains some lines.
Line 2.
And that for example is line 3.
Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
/line
R l i P tt
-
8/14/2019 Sesion 3 Admin Linux
13/48
Replacing Patterns
Advanced search and replace can be done in exmode:To replace old with new: :1,$s /old/new/g
This file contains some lines.
Line 2.
And that for example is line 3.
Line 4 follows line 3.The last line is line 5.
~
~
~~
~
~
: 1,$s/this/that/g
C t C d P t
-
8/14/2019 Sesion 3 Admin Linux
14/48
To cut a whole line into buffer: ddTo copy a whole line into buffer: yy
To cut a word from the current cursor
position to its end: dw
To paste contents of buffers here: p
To cut, copy multiple lines, proceed command bynumber: 3dd, 8yy
Cut, Copy and Paste
Cut and Paste
-
8/14/2019 Sesion 3 Admin Linux
15/48
Cut and Paste
This file contains some lines.
Line 2.
And that for example is line 3.
Line 4 follows line 3.The last line is line 5.
This file contains some lines.
Line 2.
Line 4 follows line 3.
The last line is line 5.And that for example is line 3.
cut line 3 by pressing "dd"
...and insert it afterline 4 by pressing "p"
Copy and Paste
-
8/14/2019 Sesion 3 Admin Linux
16/48
Copy and Paste
This file contains some lines.
Line 2.
And that for example is line 3.
Line 4 follows line 3.The last line is line 5.
This file contains some lines.
Line 2.
And that for example is line 3
Line 4 follows line 3.The last line is line 5.
And that for example is line 3.
copy line 3 by pressing "yy"
...and insert it afterline 5 by pressing "p"
vi Options
-
8/14/2019 Sesion 3 Admin Linux
17/48
vi Options
Options entered in ex mode change the behavior of the vieditor::set all:set autoindent/noautoindent
:set number/nonumber:set list/nolist:set showmode/noshowmode
:set tabstop=x:set ignorecase/noignorecase:set wrapmargin=x:set tx/notx
:set hlsearch/nohlsearch:syntax on/off
To make this options available to all vi sessions,put it into a .exrc or.vimrc file in your HOME-directory
Exiting vi
-
8/14/2019 Sesion 3 Admin Linux
18/48
Exiting vi
To save and exit in command mode ZZTo save in ex mode :w
To forcefully save file in ex mode :w!
To quit without saving in ex mode :q
To forcefully exit in ex mode :q!
To save and exit in ex mode (recommended) :wq
To save and exit in ex mode, shorter :x
vi Cheat Sheet
-
8/14/2019 Sesion 3 Admin Linux
19/48
vi Cheat SheetEdit mode
Ex mode
kh
j
l
cursor move
save&exit
ZZex mode
:
or
search, repeat
/ nundo, redo
u .
Command mode
I i a A To edit mode
cut, copy, paste line
dd yy p
join lines
J
$
G
1G
^
search and replace
:%s /old/new/g
change settings
:set ...save, exit
:w :w!:q :q!
Can now type
text. Note: in
vim arrow keys,Del, Backspace
will work!delete char, word
x X dw
:wq:x
Other Editors
-
8/14/2019 Sesion 3 Admin Linux
20/48
Other Editors
A typical Linux distribution comes with a large number ofeditors. Examples:
Text mode editors
pico (really simple)Originalviemacs (even more powerful and complicated than vi)
Graphical mode editorskedit, kwritegedit
Hex editors allow you to change non-text files if you knowthe internal structurekhexedit
Unit Summary
-
8/14/2019 Sesion 3 Admin Linux
21/48
Unit Summary
The most common editor on any UNIX is vivi has three modes of operation: command mode, editmode and ex mode
vi makes a copy of the file you are editing in an editbuffer. The contents are not changed until you save thechanges
A typical Linux distribution comes with a lot of othereditors as well
-
8/14/2019 Sesion 3 Admin Linux
22/48
Linux Utilities
Objectives
-
8/14/2019 Sesion 3 Admin Linux
23/48
Objectives
After completing this unit, you should be able to:
Use the find and locate command to search for files
Use the cut command to list specific columns of a file
Use the grep command to search text files for patternsUse the head and tail commands to view specific lines ina file
Use the sort command to sort the contents of a file
Use the type,which and whereis commands to findcommands
Use the file command to find out the content of a file
Use thejoin and paste commands to combine files
Manipulate files with gzip,gunzip and zcat
The find Command
-
8/14/2019 Sesion 3 Admin Linux
24/48
The find Command
Search one or more directory structures for files thatmeet certain specified criteria
Display the names of matching files or execute
commands against those files
Syntax:
$ find path expression
Sample Directory Structure
-
8/14/2019 Sesion 3 Admin Linux
25/48
Sample Directory Structure
/
home
joe
sizeshapecolor
blue
brown
green
big
giant
little
small
box
circle
phone
square
blues
phonephone1
phone2
Using find
-
8/14/2019 Sesion 3 Admin Linux
26/48
Using find
Generally, you want to search a directory structure forfiles with certain names and list the names found.
$ cd /home/joe
$ find . -name phone./shape/phone
./phone
On many other UNIX systems, with find you have to tellit specifically to print the names using -print
$ find . -name phone -print./shape/phone
./phone
-
8/14/2019 Sesion 3 Admin Linux
27/48
Interactive Command Execution
-
8/14/2019 Sesion 3 Admin Linux
28/48
The -ok option also causes command execution but onan interactive basis:
$ find . -name b\* -ok rm {} \;< rm ... ./color/blue > ? y
< rm ... ./color/brown > ? y
< rm ... ./shape/box > ? y
< rm ... ./size/big > ? y< rm ... ./blues > ? y
Additional find Options
-
8/14/2019 Sesion 3 Admin Linux
29/48
p
-type fd
ordinary filedirectory
-size +n-nnc
larger than n blockssmaller than n blocksequal to n characters
-mtime +x-x
x
modified more than x days agomodified less than x days ago
modified x days ago-perm onum
modeaccess permissions match onumaccess permissions match mode
-user user finds files owned by user-newer ref.file file was modified more recently
than ref.file-o-a Logical ORLogical AND
find Examples
-
8/14/2019 Sesion 3 Admin Linux
30/48
p
$ find . -perm 777
./size/little
File matches expr 1 and expr 2:$ find . -name 's*' -type f -a -size +2\
>-exec ls -i {} \;
187791 ./shape/square
202086 ./size/small
File matches expr 1 or expr 2:
$ find . -name big -o -name 'c*'
./color
./shape/circle
./size/big
locate Command
-
8/14/2019 Sesion 3 Admin Linux
31/48
locate allows you to quickly find a file on the system,based on simple criteria
Requires that the superuser runs updatedb regularly
Most distributions run updatedb automaticallySuSE does not install locate/updatedb by default
$ locate passwd
/usr/share/man/man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz
/etc/passwd
/usr/bin/passwd
-
8/14/2019 Sesion 3 Admin Linux
32/48
cut Example (1)
-
8/14/2019 Sesion 3 Admin Linux
33/48
$ cat /etc/passwd
root:x:0:0:Big Brother:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
tux1:x:500:500::/home/tux1:/bin/bashtux2:x:501:501::/home/tux2:/bin/bash
$ cut -f1,6,7 -d: /etc/passwd
root:/root:/bin/bashshutdown:/sbin:/sbin/shutdown
tux1:/home/tux1:/bin/bash
tux2:/home/tux2:/bin/bash
cut Example (2)
-
8/14/2019 Sesion 3 Admin Linux
34/48
$ ps
PID TTY STAT TIME COMMAND
374 p0 S 0:00 -bash460 p0 R 0:00 ps
$ ps | cut -c-5,20-
PID COMMAND374 -bash
471 ps
The grep Command
-
8/14/2019 Sesion 3 Admin Linux
35/48
Searches one or more files or standard input for linesmatching pattern
Simple match or Regular Expression
Syntax
grep [options] pattern [file1 ...]
grep Sample Data Files
-
8/14/2019 Sesion 3 Admin Linux
36/48
Phone 1:
Phone 2:
Allet 10300 intern
Judith 20500 intern
Kees 30500 externLeo 40599 extern
Nannie 50599 extern
Peter 60300 intern
Allet 1342 intern
Judith 2083 internKees 3139 extern
Leo 4200 intern
Nannie 5200 intern
Peter 6342 extern
Basic grep
-
8/14/2019 Sesion 3 Admin Linux
37/48
$ grep 20 phone1Judith 20500 intern
$ grep 20 phone*
phone1:Judith 20500 intern
phone2:Judith 2083 intern
phone2:Leo 4200 intern
phone2:Nannie 5200 intern
$ grep -v Judith phone2
Allet 1342 intern
Kees 3139 extern
Leo 4200 internNannie 5200 intern
Peter 6342 extern
grep with Regular Expressions
-
8/14/2019 Sesion 3 Admin Linux
38/48
Patterns with metacharacters should be in single quotes(' ') so that the shell will leave it alone
Valid metacharacters with grep: $ . * ^ [ - ]. Any single character* Zero or more occurrences of the
preceding character[a-f] Any ONE of the characters in the
range a through f^a Any line that starts with a
z$ Any line that ends with z
grep Options
-
8/14/2019 Sesion 3 Admin Linux
39/48
-v Print lines that do not match
-c Print only a count of matching lines
-l Print only the names of the files with
matching lines
-n Number the matching lines
-i Ignore the case of letters when makingcomparisons
-w Do a whole word search
-f Read expressions from file instead of
command line
Other greps
-
8/14/2019 Sesion 3 Admin Linux
40/48
fgrep allows only fixed strings (no regular expressions)
egrep allows for multiple (alternate) patterns
What does the following command do?
$ egrep '20500|40599|50599' phone1
Judith 20500 intern
Leo 40599 extern
Nannie 50599 extern
$ grep 30 phone1 | grep intern
????????
The sort Command
-
8/14/2019 Sesion 3 Admin Linux
41/48
The sort command sorts the lines in the file specified andwrites the result to standard output
sort -t(delimiter) +field -options file
$ cat animals
dog.2
cat.4penguin.10
$ sort animals
cat.4dog.2
penguin.10
sort Examples
-
8/14/2019 Sesion 3 Admin Linux
42/48
$ sort +0.1 animals
cat.4
penguin.10
dog.2
$ sort -t. +1 animals
penguin.10
dog.2
cat.4
$ sort -t. -n +1 animals
dog.2
cat.4
penguin.10
Options:
-d sorts in dictionary order. Only letters, digits and spaces are
considered in comparisons
-r reverses the order of the specified sort-n sorts numeric fields in arithmetic value
The head and tail Commands
-
8/14/2019 Sesion 3 Admin Linux
43/48
The head command can be used to view the first fewlines of a file or files. The command syntax is:$ head [-lines] file(s)
The tailcommand displays the last few lines of a file or
files. The command syntax is:$ tail [{-lines|+lines|-f}] file(s)
$ head -5 myfile
$ ls -l | head -12
$ tail -20 file
$ tail +20 file
$ tail -f file
The type, which and whereis Commands
-
8/14/2019 Sesion 3 Admin Linux
44/48
To find out what the path to a command is, use type
To find out where the binary is located, use which
To locate the binary, source and manual page files of a
command, use whereis
$ type find echo
find is /usr/bin/find
echo is a shell builtin
$ whereis find echo
find: /usr/bin/find /usr/man/man1/find.1
echo: /bin/echo /usr/man/man1/echo.1
$ which find echo
/usr/bin/find
/bin/echo
The file Command
-
8/14/2019 Sesion 3 Admin Linux
45/48
With the file command, you can find out what the type ofdata in the file is.
$ file /etc/passwd /bin/ls /home/peter /tmp/fake.jpg/etc/passwd: ASCII text
/bin/ls: ELF 32-bit LSB executable, Intel 80386,version 1, dynamically linked, stripped
/home/peter: directory/tmp/fake.jpg: PDF document, version 1.3
The gzip, gunzip and zcat Commands
-
8/14/2019 Sesion 3 Admin Linux
46/48
$ ls -l file1
-rw-rw-r-- 1 team01 team01 32031 Apr 6 23:40 file1
$ gzip -v file1file1: 89.9% -- replaced with file1.gz
$ ls -l file1.gz
-rw-rw-r-- 1 team01 team01 3265 Apr 6 23:40 file1.gz
$ zcat file1
(output is the same as the output of the cat command with
the uncompressed file)
$ gunzip file1
$ ls -l file1
-rw-rw-r-- 1 team01 team01 32031 Apr 6 23:40 file1
To compress or uncompress files use gzip, gunzip orzcat
The join and paste Commands
-
8/14/2019 Sesion 3 Admin Linux
47/48
$ cat one
a apple another
b bee beast
$ cat two
a ape
b broken
$ join one two
a apple another ape
b bee beast broken
$ paste one two
a apple another a ape
b bee beast b broken
join and paste combine files
Unit Summary
-
8/14/2019 Sesion 3 Admin Linux
48/48
The following commands were considered:
The find command is used to recursively searchdirectories for files with particular characteristics
The grep command is used to select entire linescontaining a particular pattern
The head and tail commands are used to view specific
lines in a fileThe sort command sorts the contents of a file by theoptions specified
Find out where you can find commands with type, whereand whereis
The gzip, zcat and gunzip commands can be used to
create and work with compressed files