Вы находитесь на странице: 1из 12

HISTORY TRICKS

history - Displays the history of commands used in the system


!!

- repeats last command

!char

- repeats last command ther started with char

!num - repeats a command by its number in history output


!-n

- repeats a command entered n commands back

Esc + . - to recall argument of the last command, ie., recently used

command

Hold Alt Key + . - to recall arguments of previous commands


(reverse-i-search)`': <start typing the command> - search for a command in
Command history
history -c

- to clear history

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
- command > filename -->Direct standard output of command to a file
Ex: find /etc -name passwd > <filename>
- command >> filename --> Append standard output of command to file
- command < filename ---> send file as input to command
Ex: tr 'A-Z' 'a-z' < <filename>:
- command 2> filename --> Redirect error messages from command to file.
- command 2>> filename --> Append error messages from command to file
- command &> filename --> Redirect output and error messages to file.
Ex: find /etc -name passwd &> find.all
- command 2>&1 | less --> Redirect output and error messages to the less cmd
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
cat /etc/passwd | tee backup
CHANGE CASE
tr 'A-Z' 'a-z' < .bash_profile
(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 STDIN 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
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 <filename>
DIFF
Compares two files for differences
diff <filename1> <filename2>
diff output stored in a file is called a "patchfile"
Use -u for "unified" 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
- stream editor
- performing search and replace
Ex:
sed 's/one/two/g' <filename>

sed 's/[Oo]ne/two/g' <filename>


sed 's/one/& and three/g' <filename>
sed '1,5s/one/two/g' <filename>
sed -e 's/dog/cat/' -e 's/hai/example/' <filename>
FINDING THE FILES
- find <path> -name <filename>
ex: find /etc -name passwd
- locate <filename>
- locate -i <file name>
i performs a case-insensitive search
- locate -n 15 <file name>
-n 15 lists only first 15 matches
ex: locate passwd
Note: updatedb command is used before running the locate command.
EDITORS
vim, gvim, nano, gedit.
VI AND VIM EDITOR
- vi (visual editor) , standard linux and unix editor
- vim (vi improved) , standard RedHat editor
- vim <file 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:

i insert data before the cursor

a place in insert mode, and allowing to append after cursor

A append to end of line

I insert at beginning of line

insert new a line (below)

insert new line (above)

EX MODE:
search, replace, save, exit
: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.
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 wm=15) text to wrap when it reaches 15
characters from the right margin

:set wrapmargin=0 turns wm off


:set ignorecase(:se ic) searches to be case-insensitive
:set noignorecase(:se noic) turns ic off
Note: These entries are temperorary, if we want to permanent these settings,
create a file /root/.vimrc and enter the setting do you need
vim /root/.vimrc
:set number
:wq
SYSTEM CONFIGURATION:
ifconfig eth0

-View interface configuration

ifup eth0

-Enable eth0 interface

ifdown eth0

-Disable eth0 interface

system-config-network

-graphical tool to configure ipaddr

netconfig

- text based tool to configure ipaddr

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

(LARGE)

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

==> 0

LOOPBACK IP ADDR ==> 127 (self pinging)


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

- 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

Ex: 192.168.0.0/255.255.255.0 (or) 192.168.0.0/24


-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.

NETWORK CONFIGURATION 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 <filename.pdf>
SETTING DATE AND TIME
GUI:

system-config-date
- Adjust the date and time manually
- On Time zone tab the system clock can be set for localtime or enable
UTC (Greenwich Mean Time)

TEXT:

date [MMDDhhmm[[CC]YY][.ss]]

Ex :date 12312359
31 of Dec, at 11:59pm. No change to the year
date 1231231592007
As above, but also sets the year to 2007
date 0101010107.01
1st of jan 07, at 01:01:01am

Вам также может понравиться