Sie sind auf Seite 1von 11

COMMAND LINE EXPANSION

Tilde (~)
cat ~/.bash_profile
Refer to root's home directory
ls ~ <uname>
Refer to another user's home directory
$ or ``
echo "This system's name is $(hostname)"
This system's name is server1.example.com
echo "This system's name is `hostname`"
This system's name is server1.example.com
COMMAND EDITING TRICKS
Ctrl-a moves to beginning of line
Ctrl-e moves to end of line
Ctrl-u deletes to beginning of line
Ctrl-k deletes to end of line
Ctrl-arrow moves left or right by word
GNOME-TERMINAL OPTIONS
Ctrl-Shift-t creates a new tab
Ctrl-PgUp/PgDn switches to next/prev tab
Ctrl-Shift-c copies selected text
Ctrl-Shift-v pastes text to the prompt
SHELL SCRIPTING
vim shell.sh
#!/bin/bash
# This script displays some information about your environment
echo "Greetings. The date and time are $(date)"
echo "Your working directory is: $(pwd)"
chmod u+x shell.sh

SHEBANG USE
#!/bin/bash

used for Bash scripts

TO EXECUTE THE SHELL USE ANY ONE OF THE FOLLOWING


- ./shell.sh
- sh shell.sh
- bash shell.sh

FOR LOOP SCRIPTING


vim for.sh
#!/bin/bash
for USER in one two three four five six
do
useradd $USER
echo redhat | passwd --stdin $USER
done
STANDARD I/O AND PIPES
Linux provide three I/O channels to processes

Standard input - keyboard is default

Standard output - terminal window is default

Standard error - terminal window is default


File descriptor - 0 STDIN
File descriptor - 1 STDOUT
File descriptor - 2 STDERR

The above standard input,output and error can be redirected

Direct standard output of command to a file


command > filename
Ex:

find /etc -name passwd > <f ilename>


find /etc -name passwd > output

Append standard output of command to file


command >> filename

Ex:

find /etc -name shadow >> output

Send file as input to command


command < filename
Ex:

tr 'a-z' 'A-Z' < <filename>


tr 'a-z' A-Z < /etc/passwd

Redirect error messages from command to file


command 2> filename
Ex :

find /etc -name passwd 2> usererror

Note: Execute the above command using a normal user

Append error messages from command to file


command 2>> f ilename
Ex :

find /etc -name shadow 2>> usererror

Redirect output and error messages to file


command &> filename
Ex:

find /etc -name passwd &> f ind.all

Redirect output and error messages to the less cmd


command 2>&1 | less
Ex:

find /etc -name passwd | less

* only STDOUT is displayed through less


* pipe only redirects STDOUT
* if we want both then, we need to redirect STDERR to STDOUT and then
forward STDOUT over a pipe
* if we simply redirected STDERR to 1, This would redirect to a file called
"1" rather than to a file descriptor 1(STDOUT)
* Prepend the '&' character to the destination number specifies that it is a
file descriptor
Ex:

find /etc -name passwd 2>&1 | less

Pipes (|) is used to redirect output from one command to become the
input to another command.
Ex:

cat /etc/passwd | less


(cal 2007 ; cal 2008) | less

REDIRECTING TO MULTIPLE TARGETS


command1 | tee filename | command2
Ex:

cat /etc/passwd | tee backup | less

CHANGE CASE
tr 'a-z' 'A-Z' < .bash_prof ile
(or)
cat .bash_profile | tr 'a-z' 'A-Z'

SENDING MULTIPLE LINES TO STDIN


mail -s "Please Call" root@example.com <<END
> Hi root,
>
> Please give me a call when you get in. We may need
> to do some maintenance on server1.
>
> Details when you're on-site,
> END
HEAD
Displays first 10 lines of text in a file.
Ex:

head /etc/passwd
head -n 5 /etc/passwd

TAIL
Displays last 10 lines of text in a file.
Ex:

tail /etc/passwd
tail -n 5 /etc/passwd
tail -f /var/log/secure (follow new additions)

GREP
Prints lines of files or STDIN where a pattern is matched
Ex:

grep 'john' /etc/passwd


date --help | grep year

Use -i to search case-insensitively


Use -n to print line numbers of matches
Use -v to print lines not containing pattern
Use -AX to include the X lines after each match
Use -BX to include the X lines before each match
CUT
Display specific columns of file or ST DIN data
Ex:

cut -d: -f1 /etc/passwd


grep root /etc/passwd | cut -d: -f7

Use -d to specify the column delimiter (default is TAB)


Use -f to specify the column to print
Use -c to cut by characters
Ex:

cut -c2-5 /usr/share/dict/words

WC
Counts words, lines, bytes and characters.
Ex:

wc /etc/passwd
wc -l /etc/passwd (lines)
wc -w /etc/passwd (word)
wc -m /etc/passwd (character)

SORT
Sorts text to stdout
Ex:

sort /etc/passwd
sort -r /etc/passwd (reverse)
sort -n /<file name> (numeric)
sort -u <filename> (removes duplicate lines)

UNIQ
Removes successive,duplicates lines in file
Ex:

uniq <f ilename>

DIFF
Compares two files for differences
diff <filename1> <f ilename2>
diff output stored in a file is called a "patchfile"
Use -u for "unif ied" diff, best in patchfiles
patch duplicates changes in other files (use with care!)
Use -b to automatically back up changed files
TO CREATE PATCH FILE
diff -u <file1> <file2> > name.patch
TO APPLY THE PATCH TO A NEW FILE
patch -b <newfile> <patchfile>
ASPELL-CHECK
Ex:

aspell check <filename>


aspell list < <filename>

SED

Ex:

Stream editor

Performing search and replace


sed 's/one/two/g' <filename>
sed 's/[Oo]ne/two/g' <f ilename>
sed 's/one/& and three/g' <f ilename>
sed '1,5s/one/two/g' <f ilename>
sed -e 's/dog/cat/' -e 's/hai/example/' <filename>

FINDING THE FILES

find <path> -name <f ilename>

Ex:

find /etc -name passwd

locate <filename>

locate -i <file name>


i performs a case-insensitive search

locate -n 15 <f ile name>


-n 15 lists only first 15 matches
Ex:

locate passwd

Note: updatedb command is used before running the locate comma nd.

EDITORS
vim, gvim, nano, gedit.
Note: gvim Editor is provides by vim-X11 package
VI AND VIM EDITOR
- vi (visual editor) , standard linux and unix editor
- vim (vi improved) , standard RedHat editor
- vim <f ile name>

THREE MODES
COMMAND MODE:

change, delete, yank, search


Esc + 5yy

==> yank or copy 5 lines

Esc + p

==> paste the lines below cursor

Esc + 10yy

==> delete 10 lines

search
/<text >

search and replace


:1,5s/cat/dog/
:%s/cat/dog/g

u undo most recent change

U undo all changes to the current line since the cursor

Ctrl-r redo last "undone" change

Right Arrow moves right one character

5, Right Arrow moves right five characters

Move by character: Arrow Keys, h, j, k, l

Move by word: w, b

Move by sentence: ), (

Move by paragraph: }, {

Jump to line x: xG

Jump to end: G

landed on the line

INSERT MODE:

insert data before the cursor

place in insert mode, and allowing to append after cursor

append to end of line

insert at beginning of line

insert new a line (below)

insert new line (above)

EX MODE:
:w

writes (saves) the file to disk

:wq

writes and quits

:q!

quits, even if changes are lost

VISUAL MODE:
Allows selection of blocks of text
v starts character-oriented highlighting
V starts line-oriented highlighting
Visual keys can be used in conjunction with movement keys:
w, ), }, arrows, etc

Note: Highlighted text can be deleted, yanked, changed, filtered, search/replaced, etc.
Multiple documents can be viewed in a single vim screen.
o

Ctrl-w, s splits the screen horizontally

Ctrl-w, v splits the screen vertically

Ctrl-w, Arrow moves between windows

CONFIGURING VI AND VIM


Configuring on the fly
:set or :set all
Configuring permanently
~/.vimrc or ~/.exrc
A few common configuration items
:set number (:se nu) line numbers to be displayed in the left margin
:set nonumber (:se nonu)turns number off
:set autoindent (:se ai) new lines to inherit the indentation level of the
previous line
:set textwidth=65 (vim only) text to wrap when the text exceeds 65
characters
:set textwidth=0 turns textwidth off
:set wrapmargin=15 (:se w m=15) text to wrap when it reaches 15
characters from the right margin
:set wrapmargin=0 turns w m off
:set ignorecase (:se ic) searches to be case-insensitive
:set noignorecase (:se noic) turns ic off
Note: These entries are temporary, if we want to permanent these settings,
Create a file /root/.vimrc and enter the setting do you need
vim /root/.vimrc
:set number

SYSTEM CONFIGURATION:

ifconfig eth0

-View interface configuration

ifup eth0

-Enable eth0 interface

ifdown eth0

-Disable eth0 interface

system-config-network (or) neat

-Graphical tool to configure ipaddress

system-config-network-tui (or) neat-tui

-Text based tool to configure ipaddress

netconfig

-Text based tool to configure ipaddress

DEVICE CONFIGURATION IS STORED IN TEXT FILES


/etc/sysconfig/network-scripts/ifcfg-ethX
DEVICE=eth0
ONBOOT =yes
BOOTPROTO=static
IPADDR=192.168.0.X
NETMASK=255.255.255.0
GATEWAY =192.168.0.254

DEVICE

- Specifies the device alias (Ex eth0, eth1, eth2.....)

ONBOOT

- Whether to bring the device up automatically when the system


boots, by default the value is no

BOOTPROTO

- Where IP settings should retrived from.


if we set dhcp then IP is going to get from DHCP server
if we set static then manually change desired IP address

IPADDR

- IP address of the system


CLASS A

0-127

CLASS B

128-191 (MEDIUM)

CLASS C

192-223 (SMALL)

CLASS D

224-239 (MULTICASTING (1 TO MANY))

CLASS E

240-255 (RESEARCH)

NETWORK ID

(LARGE)

LOOPBACK IP ADDR 127 (self pinging)

10

PRIVATE IPADDR
10.0.0.0

- 10.255.255.255

172.16.0.0

- 172.31.255.255

192.168.0.0 - 192.168.255.255

NETMASK

Ex:

- netmask value for the class used


CLASS A

255.0.0.0

(or)

CLASS B

255.255.0.0

(or)

16

CLASS C

255.255.255.0 (or)

24

192.168.0.0/255.255.255.0 (or) 192.168.0.0/24

GATEWAY

- The IP address of the system or devic e to send messages


destined for hosts on another network
- The responsibility of Gateway is to determine how to contact the
destination host.

NETWORK CONF IGURATION IS STORED IN


/etc/sysconfig/network
HOSTNAME=stationX.example.com
NETWORKING=yes
GATEWAY =<ip address>

HOSTNAME

- The system's hostname

NETWORKING - enable or disable networking

GATEWAY

- The IP address of the system or device to send messages


destined for hosts on another network
- The responsibility of Gateway is to determine how to contact
the destination host.

PDF VIEWER
evince <f ilename.pdf >

11

Das könnte Ihnen auch gefallen