Linux Commands: Difference between revisions

From miki
Jump to navigation Jump to search
(Merge Network commands)
Line 1: Line 1:
== dd ==
Convert and copy a file.

'''dd''' returns current copy status when sent an HUP signal. Type in another shell:
<source lang="bash">while :; sleep 5; do killall -s SIGHUP1 dd; done</source>
This will force '''dd''' to update its status every 5sec.

== grep ==
== grep ==
<source lang="bash">
<source lang="bash">
Line 70: Line 77:
|}
|}
<small>When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis '''( )''', or braces '''{ }''') must be quoted with '''backslash \'''.</small>
<small>When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis '''( )''', or braces '''{ }''') must be quoted with '''backslash \'''.</small>

== [http://www.dest-unreach.org/socat/doc/socat.html socat] ==
Command-line utility that establishes two bidirectional byte streams and transfers data between them ([http://wiki.yobi.be/wiki/Bypass_Proxy#Client_side:_using_socat]).
''socat'' is the more powerful version of ''netcat''.
<source lang="bash">socat -ly 'TCP4-LISTEN:143,reuseaddr,fork' PROXY:ton.imap.server:143|TCP:134.27.168.36:8080
ProxyCommand socat - 'PROXY:%h:%p,proxyauth=user:pass|SSL,verify=0|PROXY:my.server:443,proxyauth=user:pass|TCP:big.brother.proxy:8080' #Using v2.0.0 beta
</source>
''socat'' can be easily used as a replacement of ''telnet'':
<source lang="bash">socat tcp:<host>:<port> - #<port> can be a port number or service name (telnet,imap...)</source>

== [http://netcat.sourceforge.net/ netcat] ==
TCP-IP swiss army knife
''(equivalent of the telnet program. Check [[wikipedia:netcat]]. Also known as command '''nc''')''.

== [http://en.wikipedia.org/wiki/Netstat netstat] ==
Print network connections, routing tables, interface statistics, masqurade connections, and multicast memberships
<source lang="bash">
netstat -atpn #All, tcp, socket program PID, numeric
netstat -rn #Kernel route table, numberic
</source>
When listing sockets (default output), you'll get an output like:
{|
|-
|
% netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:time *:* LISTEN
tcp 0 0 localhost:mysql *:* LISTEN
tcp 0 0 andLinux.local:43449 windows-host:x11 ESTABLISHED
|
% netstat -atn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 192.168.11.150:43449 192.168.11.1:6000 ESTABLISHED
|}
:;Local Address
::'''*''' or '''0.0.0.0''' means that the process accepts connection from any interface.
::'''127.0.0.1''' means it only accepts connection on localhost loopback (and so only connection that originates from local PC as well).
::Any other IP address means that the process listen on the given port at the given IP address


== Miscellaneous ==
== Miscellaneous ==
Line 76: Line 127:
;strace
;strace
:trace system calls and signals
:trace system calls and signals
;tee
:read from standard input and write to standard output and files
;mkfifo
:make FIFOs (named pipes)
;pv, pipeview, pipebench
:monitor the progress of data through a pipe
;watch
:Execute a program periodically, showing output full screen

Revision as of 11:07, 3 November 2008

dd

Convert and copy a file.

dd returns current copy status when sent an HUP signal. Type in another shell:

while :; sleep 5; do killall -s SIGHUP1 dd; done

This will force dd to update its status every 5sec.

grep

grep -Rsl PATTERN [FILE]            # Recursive, no error output, only list filename
grep BASIC-REG-EXP-PATTERN [FILE]   # Use classic regexp (like "dma\|DMA")
egrep EXT-REG-EXP-PATTERN [FILE]    # Same as grep -E. Use extended regexp (like "dma|DMA")
fgrep FIXED-STRINGS-REG-EXP [FILE]  # Same as grep -F. Pattern is a list of strings to match.
grep -n PATTERN [FILE]              # Print matched line numbers.
grep -- "-s" [FILE]                 # Search for text "-s"
grep -e "-s" [FILE]                 # Search for text "-s" - alternative solution
grep -R -include=*.in PATTERN *     # Search recursively through folders, limiting to files matching pattern "*.in"
grep -R PATTERN *.in                # Idem, but matching pattern "*.in" also applies to folders.

rpm

rpm -ivh package                    # Installing a package - verbose and progress bars
rpm -iv -nodeps package             # Installing a package (verbose), ignore dependencies
  • Query commands
rpm -ql package                     # List files provided by a package
  • To query a package that has not been installed, add -p option to the command:
rpm -qpl package                    # List files provided by a package

sed

sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed -n                              # Silent - suppress automatic printing of pattern space
sed -r                              # Use extended regular expression
sed -i "s/foo/bar/" *.txt           # In-place file modification

References

Regular expressions

The information below is only illustrative. See e.g. Wikipedia page for reference information. The list below is actually for extended regular expressions, which can be obtained in sed using option -r (sed -r).

Regexp Description
. Match any character
gray|grey Match gray or grey
gr(a|e)y Match gray or grey
gr[ae]y Match gray or grey
file[^0-2] Match file3 or file4, but not file0, file1, file2.
colou?r (zero or one) - Match Color or Colour.
ab*c (zero or more) - Match ac, abc, abbc, ....
ab+c (one or more) - Match abc, abbc, abbbc, ....
a{3,5} (at least m and not more than n times) - Match aaa, aaaa, aaaaa.
^on single line$ (start and end of line) - Match on single line on a single line.

When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis ( ), or braces { }) must be quoted with backslash \.

socat

Command-line utility that establishes two bidirectional byte streams and transfers data between them ([1]). socat is the more powerful version of netcat.

socat -ly 'TCP4-LISTEN:143,reuseaddr,fork' PROXY:ton.imap.server:143|TCP:134.27.168.36:8080
ProxyCommand socat - 'PROXY:%h:%p,proxyauth=user:pass|SSL,verify=0|PROXY:my.server:443,proxyauth=user:pass|TCP:big.brother.proxy:8080'     #Using v2.0.0 beta

socat can be easily used as a replacement of telnet:

socat tcp:<host>:<port> -       #<port> can be a port number or service name (telnet,imap...)

netcat

TCP-IP swiss army knife (equivalent of the telnet program. Check wikipedia:netcat. Also known as command nc).

netstat

Print network connections, routing tables, interface statistics, masqurade connections, and multicast memberships

netstat -atpn      #All, tcp, socket program PID, numeric
netstat -rn        #Kernel route table, numberic

When listing sockets (default output), you'll get an output like:

% netstat -at

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:time                  *:*                     LISTEN
tcp        0      0 localhost:mysql         *:*                     LISTEN
tcp        0      0 andLinux.local:43449    windows-host:x11        ESTABLISHED
% netstat -atn

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:37              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
tcp        0      0 192.168.11.150:43449    192.168.11.1:6000       ESTABLISHED
Local Address
* or 0.0.0.0 means that the process accepts connection from any interface.
127.0.0.1 means it only accepts connection on localhost loopback (and so only connection that originates from local PC as well).
Any other IP address means that the process listen on the given port at the given IP address

Miscellaneous

htop
an improved top command (http://htop.sourceforge.net/index.php)
strace
trace system calls and signals
tee
read from standard input and write to standard output and files
mkfifo
make FIFOs (named pipes)
pv, pipeview, pipebench
monitor the progress of data through a pipe
watch
Execute a program periodically, showing output full screen