Useful commands

Still under construction

sort and uniq
sort  -  Sort lines of text
     -r  Sort in reverse order
     -n  compare according to string numerical value
     -u  output only the first of an equal run (unique)

uniq  -  Find adjacent matching (equal) lines (so sorted) in FILE
     -c  prefix lines with number of occurrences
     -d  print only duplicated lines
     -u  print only unique lines
     -i  ignore case
     -s  no compare first N characters
     -f  no compare first N fields

sort random-numbers.txt | uniq -c | sort -n -r
cat table.csv | column -t -s ','

awk -F ',' '{printf("%-5s %-5s %-5s\n", $1, $2, $3)}' table.csv
Find palindromes
^(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\7\6\5\4\3\2\1$   --> max 15 chars
PS

In “top” press “H”

ps -ax   --> PID TTY STAT TIME COMMAND
ps -aux  --> USER PID %CPU %MEM  VSZ RSS TTY STAT START  TIME COMMAND
ps vga
ps -ef   --> UID PID PPID C STIME TTY TIME CMD
ps -efly --> S UID PID PPID C PRI NI RSS SZ WCHAN  STIME TTY TIME CMD  
ps -efT  --> PS with threads


will give the top 10 processes by memory usage.

ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -10

ps -efly --sort -rss| head -10

ps aux --sort -rss | head -10

ps aux --sort=-vsz,-rss | head -10
xargs

When “*” expansion does ot work (shell limits…), ´xargs´ is handy: ´ls | xargs -i{} rm {}´

find
-name  pattern     Base of file name matches shell pattern pattern
-regex pattern     File name matches regular expression pattern.  This is a match
                   on the whole path, not a search.  For example, to match a file
                   named './fubar3', you can use the regular expression '.*bar.'
                   or '.*b.*3', but not '.*r3'.
-xdev              Don't descend directories on other filesystems.
-prune             Don't traverse subdirectories (use 'find ' instead of 'find .' )
-print             Print the full file name on the standard output, followed by a newline
-print0            Print the full file name on the standard output, followed by a null
                   character. This allows file names that contain newlines or other types
                   of white space to be correctly interpreted by programs that process
                   the find output (corresponds to -0 option of xargs).

The find command with option ‘-print0’ action enables printing of the full directory path on the standard output, followed by a null character and ‘-0 xargs’ flag deals with space in filenames.

Very good samples –> http://bytefreaks.net/tag/find

grep
-E              Interpret PATTERN as an extended regular expression.

-i              Ignore case
-v              Invert match
-e PATTERN      Use PATTERN as the pattern.  If this option is used multiple times or is combined with  the  -f  (--file) option, search  for  all patterns given
-l              List files names that match
-L              List files names that NOT match
-h              Do not print filename prefixes
-o              Print only matching part
-c              Count matches
-r              Read  all  files  under each directory, recursively, following symbolic links only if they are on the command line.
-R              Like -r, but follows symbolic links
rename
Deleting a file

When rm is not enough, try

rm -- -todelete

or

rm \-todelete
rm ./-todelete

Also you can do

mycomputer $ ls -lai
total 28
7471806 drwxrwxr-x  2 ferran ferran  4096 May 10 19:32 .
7471105 drwxrwxrwt 20 root   root   24576 May 10 19:32 ..
7476239 -rw-rw-r--  1 ferran ferran     0 May 10 19:32 -todelete

find -inum 7476239 -delete
Catch signals in BASH

trap SIGNAL –> trap catches SIGNAL and runs command or function

    trap "echo pillada senyal SIGTSTP" SIGTSTP
    trap "echo pillada senyal SIGINT" SIGINT
    trap "echo pillada senyal SIGSTOP" SIGSTOP
    trap "echo pillada senyal SIGTERM" SIGTERM
    trap "echo Fent veure que netejo..." EXIT
nice, renice

Nice value is a user-space and priority PR is the process’s actual priority that use by Linux kernel. In linux system priorities are 0 to 139 in which 0 to 99 for real time and 100 to 139 for users. nice value range is -20 to +19 where -20 is highest, 0 default and +19 is lowest.

https://www.linux.com/news/influence-scheduling-priority-nice-and-renice
http://www.thegeekstuff.com/2013/08/nice-renice-command-examples/?utm_source=tuicool
https://developer.ibm.com/technologies/linux/tutorials/l-lpic1-103-6/

uptime - load

From http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html

Linux load averages are “system load averages” that show the running thread (task) demand on the system as an average number of running plus waiting threads. This measures demand, which can be greater than what the system is currently processing. Most tools show three averages, for 1, 5, and 15 minutes.

https://github.com/torvalds/linux/blob/master/include/linux/sched/loadavg.h#L21

The original load averages show only CPU demand: the number of processes running plus those waiting to run.

In Linux, by adding the uninterruptible state means that Linux load averages can increase due to a disk (or NFS) I/O workload, not just CPU demand.

cat /proc/loadavg
0.31 0.35 0.37 2/863 20526

stty - Configure TTY settings chsh - Change shell - (“sudo chsh -s /usr/local/bin/bash smith” to change user smith to use bash on login)

File system

fsck - check and repair a Linux filesystem

EXT FS
dumpe2fs <device>   -  to see (a lot of) info about "ext" file system (dumpe2fs /dev/sda7)
resize2fs <device> [new_size]  -  Resize a file system (resize2fs /dev/sda7 7G)
LVM
lvs
pvs  -  report information about physical volumes
vgs
Network
ifconfig wlan0 up    -- to activate wireless
iwconfig and iwlist  -- set up wireless option in wireless interface. iwlist shows detailes wiereless parameters.
ip vs ifconfig

Unfortunate for some oldies like Mico Maco, there well known utilities of the net-tools package (arp, ifconfig, iptunnel, iwconfig, nameif, netstat, and route) have outdated by the (not so) new iproute2 package (ip, ss, bridge, rtacct, rtmon, tc, ctstat, lnstat, nstat, routef, routel, rtstat, tipc, arpd and devlink).

Below is a micro cheat-sheet table.

Action ifconfig ip
Show network device configuration # ifconfig $ ip addr show $ ip link show
Network interface activation # ifconfig eth0 up # ip link set eth0 up
Network interface des-activation # ifconfig eth0 down # ip link set eth0 down
Set an IP address # ifconfig eth0 192.168.1.1 # ip address add 192.168.1.1 dev eth0
Remove an IP address # ip address del 192.168.1.1 dev eth0
Add virtual interface or alias # ifconfig eth0:1 10.0.0.18 # ip addr add 10.0.0.18 dev eth0 label eth0:1
Add data in ARP table # arp -i eth0 -s 192.168.0.1 00:11:22:33:44:55 # ip neigh add 192.168.0.1 lladdr 00:11:22:33:44:55 nud permanent dev eth0
Switch off ARP in a device # ifconfig -arp eth0 # ip link set dev eth0 arp off
Some more iproute2 commands

ip -s link

ss

See here a much detailed conversion list for all commands.

tcpdump
tcpdump -i eth0
tcpdump -i eth0 -w packets.pcap
ethtool

It is a kernel network utility useful for

ethtool eth0                            -  List Ethernet Device Properties
ethtool -i eth0                         -  Information about the driver
ethtool -S eth0                         -  NIC statistics
ethtool -s eth0 autoneg off             - Turn autonegotiation off
ethtool -s eth0 speed 100 autoneg off   - Set NIC speed
Kernel configuration

getconf -a | grep -i page –> to get Page Size

revise object files
nm        - list symbols from object files
objdump   -  display information from object files.
   '-x'   - displays all headers
   '-t'   - displays symbols

Data gatherers - http://www.semicomplete.com/blog/articles/week-of-unix-tools/day-4-data-sources.html
Cut and Paste (commands…) - http://www.semicomplete.com/blog/articles/week-of-unix-tools/day-2-cut-and-paste.html
xargs Intro - http://www.semicomplete.com/blog/articles/week-of-unix-tools/day-5-xargs.html
VI Intro - http://www.semicomplete.com/presentations/vim/#slide_0
https://www.tecmint.com/learn-linux-cat-command-and-tac-command/
https://www.tecmint.com/command-line-tools-to-monitor-linux-performance/
https://www.tecmint.com/tag/linux-tricks/
   https://www.tecmint.com/clear-command-line-history-in-linux/
   https://www.tecmint.com/find-linux-server-public-ip-address/
   https://www.tecmint.com/use-wildcards-to-match-filenames-in-linux/
   https://www.tecmint.com/rename-all-files-and-directory-names-to-lowercase-in-linux/
   https://www.tecmint.com/find-out-which-process-listening-on-a-particular-port/
   https://www.tecmint.com/delete-huge-files-in-linux/
   https://www.tecmint.com/compare-find-difference-between-two-directories-in-linux/
   https://www.tecmint.com/chattr-command-examples/
   https://www.tecmint.com/make-file-directory-undeletable-immutable-in-linux/