Linux: Basic Command Introduction V

Part I: Network Related Command

1) Command Name: write

    Command Derived From: write

    Command Path: /usr/bin/write

    Command Auth: all users

    Command Function: Send instant message to another user.

    Command Syntax: write [username]

    Example:

write administrator              --> Write im to administrator, Ctrl+d as the EOF command.

 

2) Command Name: wall

    Command Derived From: write all

    Command Path: /usr/bin/wall

    Command Auth: all users

    Command Function: Send instant message to all users.

    Command Syntax: wall

    Example:

wall              --> Write im to all logined user, Ctrl+d as the EOF command.

 

3) Command Name: ping

    Command Derived From: ping

    Command Path: /usr/bin/ping

    Command Auth: all users

    Command Function: Test network connection.

    Command Syntax: ping [ip-address]

    Example:

ping 192.168.1.1              --> Send icmp request packet to test network connection. Ping will not terminate as long as we didn't terminate this command.ping -c 3 192.168.1.1       --> Sent 3 icmp request packets to test network connection. Ping will terminate after all 3 packets sent. c stands for count.

ping -s 6000 192.168.1.1 --> Sent icmp request packets with the size of 6000Bytes. s stands for size. 

     Comment:

1) Even if we got reply from the target ip address, that doesn't mean that we have a stable connection with the destination.

    We have to pay attention to the ping statistics, such as packet loss rate and errors count.

 

4) Command Name: ifconfig

    Command Derived From: interface config

    Command Path: /usr/bin/ifconfig

    Command Auth: all users

    Command Function: Get info of network interface including wired interface(eth0) and wireless interface(wlan0).

    Command Syntax: ifconfig [eth0 OR wlan0 OR lo]

    Example:

ifconfig eth0              --> Get the information of wired network interface.ifconfig wlan0           --> Get the information of wireless network interface.

ifconfig lo                  --> Local loopback network interface address.

ifconfig etho0 192.168.1.99  --> Modify current ip address to 192.168.1.99.

     Comment:

1) When we modify the ip address of current machine. This new ip address will only apply for current conversation.

    When we restart the machine, the new ip address will not be reserved.

    In linux, if we need to permanently modify the configuration, we have to modify the config file.

    If not, the modification will be only apply for current conversation.

 

Part II: Shell Command Techniques

1) Shell Intro

    Example:

cat /etc/shells    --> This file listed all the installed shells in current system.

    Comment:

1) There are a lot of shells in linux, such as bash, ksh, sh, dash...

2) Different kind of shells have different syntax.

3) If we use bash, we can press <TAB> to auto complete current command.

4) We can use Ctrl+l to clear current screen and use Ctrl+u to clear input in current line before current cursor .

    We can use Shift+Ctrl+r to find previous typed command.

 

2) Command Alias

    Example:

alias                            --> Get all the predefined alias.

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

alias copy=cp           --> Create an alias for cp.

alias drm="rm -rf"     --> Create an alias for command "rm -rf".

unalias copy             --> Delete the alias copy.

unalias drm              --> Delete the alias drm.

     Comment:

1) Caution the difference of defining alias for command without options and command with options.

    We have to use "" to enclose the command with options.

 2) There is no predefined alias in UNIX OS.

 

3) I/O Redirection

    Introduction:

1) Shell predefined 0 for standard input (STDIN). Standard input in linux is keyboard input by default.

    Shell predefined 1 for standard output (STDOUT). Standard output in linux is screen output by default.

    Shell predefined 2 for standard error (STDERR). Standard error in linux is screen output by default.

2) I/O redirection means that we redirected the default I/O source.

    Input source may be redirected to file or network stream.

    Output source may be redirected to file or network stream.

    Example:

ls -ltr /etc > /temp/info.log    --> The listed results will not be displayed in screen. They will be written into /temp/info.log.

                                                   The reason is that our output is redirected to /temp/info.log.

                                                   The info.log is override by current info.

                                                   The '>' means output redirection. That equals "1>"

ls -ltr /etc >> /temp/info.log  --> The listed results will be appended into /temp/info.log instead of override it.

wall < /test/info.log               --> The content of /test/info.log will be send to all users. Here we redirect the input source from keyboard to file.

cp -R /usr /backup/usr.bak 2> /backup/error.log   --> If there is any error occurs, write the error message into /backup/error.log.

ls -ltr /nothing 2> /error.log  --> The /nothing dir doesn't exists. If we don't redirect the error output, the error message will be shown in screen.

                                                   Here we redirect the error message into /error.log file.

    Comment:

1) Imagine that we are administrator of a dozens of servers.

    We have to check the server's states such as cpu and memory usage every day.

    Instead of login servers one by one and type the command. We can redirect the output into a specific log file.

    And schedule a job send an email to us with these files as attachment.

2) /usr dir just like system32/ dir. It's very important and contains all the core system program.

4) Pipe

    Introduction:

Pipe: Send the output of previous command as input for post command.

    Example:

ls -ltr /etc | more    --> The output of command ls -ltr /etc would be so long that overflow the screen.

                              --> We use the output of previous command as the input of "more" so that we can view all

                                    the output by typing space to next page and enter to next line.

ls -ltr /etc | grep init --> The output of command ls -ltr /etc would be so long that make it difficult to find the entry contains 'init'

                               --> We use the output as input for grep init and we can easily find the interested entry.

ls -ltr /etc | grep init | wc -l

                               --> Count the number of entry that contains keyword 'init'.

    Comment:

1) Pipe combines simple commands and makes a difference.

 

5) Command Connector

    Introduction:

";"          --> The commands connected by ; means execute by the command order.

"&&"      --> The commands connected by && means the command after && will only excute after the successful execution of the command before &&.

"||"         --> The commands connected by || means the command after || will only excute after the failed execution of the command before ||

    Example:

pwd ; ls -ltr; date    --> First, we print out the current directory.

                               --> Second, we list out the file details in current directory.

                               --> Third, we print out the date of command execution.

write Mary < /home/davy/love.txt && rm /home/davy/love.txt

                               --> Send im to Mary.

                               --> If send successful, remove the input source.

write Mary < /home/davy/love.txt || mail Mary < /home/davy/love.txt

                               --> Send im to Mary.

                               --> If send failed, send mail to Mary.

ls /nothing || pwd

                               --> List files in /nothing directory

                               --> If list failed, print current working directory.

ls /nothing || touch /home/davy/error.log; ls /nothing 2> /home/davy/error.log

                               --> List files in /nothing directory.

                               --> If list failed, create a new log file. And re-execute list command, log error message into error.log file.

    Comment:

1) A traditional usage of ; is that if we want to compile the linux kernel, we have to type several command by order.

    The time consumption of each command would be very huge. Instead of type first command and wait and type second command and wait...

    We can simply use ; to connect all the command and output the error log.

   

猜你喜欢

转载自davyjones2010.iteye.com/blog/1954616
今日推荐