Learn a shell a day, master Unix one day!

Pipe

Process read previous process’s output as its input and generate output to next process’s input. Use symbol | to create a pipeline, which a powerful skill in Unix.

1
2
// count the numbers of text file in current directory.
ls -la | grep *.txt | wc -l

Xargs

Many programs, takes its arguments from the command line (char *argv[]) but ignores standard input fd 0. If you want to pipe the output of a program to input of another program that ignores standard input, like rm, you need to use xargs.

xargs - build and execute command lines from standard input

1
2
ls | grep trash | rm            // this won't work, because rm ignores standard input
ls | grep trash | xargs rm 

Top

top - display Linux processes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
top - 22:05:26 up 2 days,  2:48,  2 users,  load average: 0.00, 0.00, 0.00       
//     uptime                               over the last 1,    5,    15 minutes 

Tasks: 355 total,   1 running, 354 sleeping,   0 stopped,   0 zombie             
// task states

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st  
// CPU states

MiB Mem :  16004.5 total,  12332.1 free,   2134.6 used,   1537.8 buff/cache      
// physical memory usage: total, free, used and buff/cache
MiB Swap:   7660.0 total,   7660.0 free,      0.0 used.  13560.5 avail Mem       
// virtual memory usage: total, free, used and avail (physical memory)

As a default, percentages for these individual categories are displayed. Where two labels are shown below, those for more recent kernel versions are shown first.

1
2
3
4
5
6
7
8
us, user    : time running un-niced user processes
sy, system  : time running kernel processes
ni, nice    : time running niced user processes
id, idle    : time spent in the kernel idle handler
wa, IO-wait : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor

Use man top to check out more infomation.

Strace

strace - trace system calls and signals

In the simplest case strace runs the specified command until it exits. It intercepts and records the system calls which are called by a process and the signals which are received by a process. The name of each system call, its arguments and its return value are printed on standard error or to the file specified with the -o option.

Nohup

nohup - run a command immune to hangups, with output to a non-tty

Ps

ps - report a snapshot of the current processes

Grep

grep, egrep, fgrep, rgrep - print lines that match patterns

Netstat

netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

By default, netstat displays a list of open sockets. If you don’t specify any address families, then the active sockets of all configured address families will be printed.

This program is mostly obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s link. Replacement for netstat -g is ip maddr.

[–tcp|-t] [–udp|-u] [–listening|-l] [–all|-a] [–numberic|n]

1
2
// count how many ESTABLISHED TCP v4 sockets
netstat -altu4 | grep tcp |grep -i established | wc -l

Ss

ss - another utility to investigate sockets

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
ss -t -a
        Display all TCP sockets.

ss -t -a -Z
        Display all TCP sockets with process SELinux security contexts.

ss -u -a
        Display all UDP sockets.

ss -o state established '( dport = :ssh or sport = :ssh )'
        Display all established ssh connections.

ss -x src /tmp/.X11-unix/*
        Find all local processes connected to X server.

ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24
        List all the tcp sockets in state FIN-WAIT-1 for our apache to network 193.233.7/24 and look at their timers.

ss -a -A 'all,!tcp'
        List sockets in all states from all socket tables but TCP.

Ip

ip - show / manipulate routing, network devices, interfaces and tunnels

1
2
// Show localhost's private IP address
ip addr show dev eth0 | grep inet | grep -v inet6 | awk '{print $2}' | awk -F'/' '{print $1}'

Proc

proc - process information pseudo-filesystem

The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at /proc/[pid]

Awk

mawk - pattern scanning and text processing language

mawk is an interpreter for the AWK Programming Language.

1
ss -t | awk '{print $4}' | awk -F: '{print $1}'         // list current connceted tcp Local IP address

Sed

sed - stream editor for filtering and transforming text

sed [OPTION]… {script-only-if-no-other-script} [input-file]…

1
sed 's/old/new/' config.yml     // replace old to new in file config.yml

Some tasks can be solved by using combination of a few shell.

Reference