> ls -F
> Applications/ Documents/ Library/ Music/
> ls -r #list reversedls in order of most recently modified
> ls -ltrls in order of file size (descending)
> ls -lS> ssh-keygen -t rsa -C "your_email@example.com"Create tar archive file
> tar -cvf archivename.tar /archive/directoryCreate tar.gz archive file
> tar -zcvf archivename.tar /archive/directoryCreate tar.bz2 archive file
> tar -jcvf archivename.tar /archive/directoryUnarchive
> tar -xvf archivename.tarFind file with name
> find . -name name.extFind file with name ingoring case
> find . -iname name.extFind file with name and type
> find . -type d -name path
> find . -type f -name name.extFind files with permission and chmod to another permission
> find . -type f -perm 0777 -print -exec chmod 644 {} \;
> find . -type d -perm 777 -print -exec chmod 644 {} \;Find and remove file(s)
> find . -type f -name "name.ext" -exec rm -f {} \;
> find . -type f -name "*.ext" -exec rm -f {} \;Like find but using spotlight therefore much faster than find
> mdfind -name homebrewShow disk space usage in human readable format
> df -hShow information about a specific directory
> df -hT /homeList all ports (both TCP and UDP)
> netstat -a | moreList only TCP or UDP port connections
> netstat -at
> netstat -auList all active listening ports (TCP or UDP) connections
> netstat -l
> netstat -lt
> netstat -luShow statistics by protocol
> netstat -s
> netstat -st
> netstat -suDisplay kernel IP routing
> netstat -rLook for DNS server
> nslookup google.com
> nslookup -port 56 google.comLook for mail exchanger
> nslookup -query=mx google.comLook for domain server
> nslookup -query=ns google.comLook for DNS record
> nslookup -type=any google.comQuery domain "A" record
> dig google.com
> dig google.com +shortQuery specific record for domain
> dig google.com MX
> dig google.com SOA
> dig google.com TTLDNS reverse look-up
> dig -x 72.30.38.140 +shortShow how long the system has been running
Combination of 'uptime' and 'who'
Reference: Link Use the output of netstat as example
> cat netstat.txt
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 37 0 dhcp-10-101-55-2.57321 client-14a.v.dro.https CLOSE_WAIT
tcp4 0 0 dhcp-10-101-55-2.57317 ord08s11-in-f2.1.https ESTABLISHED
tcp4 0 0 dhcp-10-101-55-2.57316 ec2-46-137-116-1.https ESTABLISHED
tcp4 0 0 dhcp-10-101-55-2.57304 17.110.227.101.5223 ESTABLISHED
tcp4 0 0 dhcp-10-101-55-2.57302 17.110.224.20.5223 ESTABLISHED
tcp4 0 0 dhcp-10-101-55-2.57303 snt-re3-9a.sjc.d.https ESTABLISHED
tcp4 37 0 10.0.0.2.56038 server-54-230-89.https CLOSE_WAIT
tcp4 37 0 10.0.0.2.55989 client-15b.v.dro.https CLOSE_WAIT
tcp4 37 0 10.0.0.2.53596 client-12b.v.dro.https CLOSE_WAIT
tcp4 37 0 dhcp-10-101-54-1.61837 server-54-230-33.https CLOSE_WAIT
tcp4 37 0 dhcp-10-101-54-1.61760 client-15b.v.dro.https CLOSE_WAITPrint selected row in a file
> awk '{print $1, $4}' netstat.txtFormat print just as C language
> awk '{printf "%-8s %-8s %-8s %-22s %-22s %-22s\n", $1, $2, $3, $4, $5, $6}' netstat.txtUsing compare operator same as C language
> awk '$3==0 && $6=="ESTABLISHED"' netstat.txt
> awk '$2>0 {print $0}' netstat.txtPreserve first line with inner variable NR
> awk '$3==0 && $6=="ESTABLISHED" || NR==1' netstat.txt| Name | Description |
|---|---|
| $0 | whole record |
| $1~$n | Nth row |
| FS | separator default is space or TAB |
| NF | number of rows in the whole record |
| NR | column No. start from 1 |
| FNR | column No. belongs to each file |
| FILENAME | input file name |
Example: print column No.
> awk '$3==0 && $6=="ESTABLISHED" || NR==1 {printf "%02s %s %-22s %-22s %s\n",NR, FNR, $4, $5, $6}' netstat.txtSet separator
> awk 'BEGIN{FS=":"} {print $1,$3,$6}' /etc/passwd
> awk -F: '{print $1, $3, $6}' /etc/passwd
> awk -F '[;:]'
> awk -F: '{print $1, $3, $6}' OFS="\t" /etc/passwdBasic example and use like grep
> awk '$6 ~ /EST/ || NR == 1 {print NR, $4, $5, $6}' OFS="\t" netstat.txt
> awk '/WAIT/' netstat.txtUse pattern to match 'EST' or 'WAIT'
> awk '$6 ~ /EST|WAIT/ || NR==1 {print NR, $4, $5, $6}' OFS="\t" netstat.txtUse opposite of a pattern
> awk '$6 !~ /EST/ || NR == 1 {print NR, $4, $5, $6}' OFS="\t" netstat.txt
> awk '!/EST/' netstat.txtSplit the file based on $6 and ignore first line
> awk 'NR!=1 {print > $6}' netstat.txt
> ls
CLOSE_WAIT ESTABLISHED netstat.txtOutput selected rows to file
> awk 'NR!=1 {print $4, $5 > $6}' netstat.txtUsing if-else-if control to split file
> awk 'NR!=1 {if ($6 ~ /EST/) print > "1.txt";
else if ($6 ~ /WAIT/) print > "2.txt";
else print > "3.txt"}' netstat.txt
> ls ?.txt
1.txt 2.txt CLOSE_WAIT ESTABLISHED netstat.txtFind columns whose length in greater than 80 in file
> awk 'length>80' filePring 9x9 table
> seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'Reference: Link
Based on grep 2.5.1-FreeBSD
> grep --version | grep grep
grep (BSD grep) 2.5.1-FreeBSDUsing grep
> cat WSJHandler.log | grep index
> grep index WSJHandler.logCount number of lines that contains a specific keyword
> grep index WSJHandler.log | wc -l
547
> grep index WSJHandler.log -c
547Create a test file
> cat test.txt
one two three
seven eight one eight three
thirteen fourteen fifteen
sixteen seventeen eighteen seven
sixteen seventeen eighteen
twenty seven
one 504 one
one 503 one
one 504 one
one 504 one
#comment UP
twentyseven
#comment down
twenty1
twenty3
twenty5
twenty7Search lines that contains a word
> grep -w 'seven' test.txt
seven eight one eight three
sixteen seventeen eighteen seven
twenty sevenSearch lines that contains a word starting (ending) with a prefix (suffix)
> grep '\<seven' test.txt
seven eight one eight three
sixteen seventeen eighteen seven
sixteen seventeen eighteen
twenty seven
> grep 'seven\>' test.txt
seven eight one eight three
sixteen seventeen eighteen seven
twenty seven
twentysevenSearch lines starting (ending) with a prefix (suffix)
> grep '^seven' test.txt
seven eight one eight three
> grep 'seven$' test.txt
sixteen seventeen eighteen seven
twenty seven
twentysevenShow context of the result lines
> grep -C 1 twentyseven test.txt
#comment UP
twentyseven
#comment down
> grep -A 1 twentyseven test.txt
twentyseven
#comment down
> grep -B 1 twentyseven test.txt
#comment UP
twentysevenAlso
> grep 'twenty[1-4]' test.txt
twenty1
twenty3
> grep 'twenty[^1-4]' test.txt
twenty seven
twentyseven
twenty5
twenty7Use re to match IP address
> grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" /etc/resolv.conf
nameserver 129.105.49.1
nameserver 165.124.49.21
> grep -E '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' /etc/resolv.conf
nameserver 129.105.49.1
nameserver 165.124.49.21
> grep -oE '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' /etc/resolv.conf
129.105.49.1
165.124.49.21> ps -afx | grep bash
501 14561 14560 0 3:43PM ttys000 0:00.07 -bash
501 14966 14561 0 4:33PM ttys000 0:00.00 grep bash
501 876 875 0 13Apr15 ttys001 0:00.06 -bash
501 879 875 0 13Apr15 ttys002 0:00.01 -bash
501 887 875 0 13Apr15 ttys003 0:00.07 -bash
501 1395 875 0 13Apr15 ttys004 0:00.01 -bash
501 1107 875 0 13Apr15 ttys005 0:00.01 -bash
501 2039 875 0 13Apr15 ttys006 0:00.01 -bash
501 2042 875 0 13Apr15 ttys007 0:00.01 -bash
501 3962 875 0 14Apr15 ttys008 0:00.16 -bash
501 4035 875 0 14Apr15 ttys009 0:00.12 -bash
501 6177 875 0 Tue04PM ttys010 0:00.05 -bash
> ps -afx | grep bash | grep -v grep
501 14561 14560 0 3:43PM ttys000 0:00.08 -bash
501 876 875 0 13Apr15 ttys001 0:00.06 -bash
501 879 875 0 13Apr15 ttys002 0:00.01 -bash
501 887 875 0 13Apr15 ttys003 0:00.07 -bash
501 1395 875 0 13Apr15 ttys004 0:00.01 -bash
501 1107 875 0 13Apr15 ttys005 0:00.01 -bash
501 2039 875 0 13Apr15 ttys006 0:00.01 -bash
501 2042 875 0 13Apr15 ttys007 0:00.01 -bash
501 3962 875 0 14Apr15 ttys008 0:00.16 -bash
501 4035 875 0 14Apr15 ttys009 0:00.12 -bash
501 6177 875 0 Tue04PM ttys010 0:00.05 -bash[:alpha:] Any alphabetical character, regardless of case
[:digit:] Any numerical character
[:alnum:] Any alphabetical or numerical character
[:blank:] Space or tab characters
[:xdigit:] Hexadecimal characters; any number or A–F or a–f
[:punct:] Any punctuation symbol
[:print:] Any printable character (not control characters)
[:space:] Any whitespace character
[:graph:] Exclude whitespace characters
[:upper:] Any uppercase letter
[:lower:] Any lowercase letter
[:cntrl:] Control characters
> grep "[[:upper:]]" test.txt
#comment UP
> grep --color "[[:upper:]]" test.txtPrint multi-line output as one-line output
> cat eg.txt
1 2 3 4 5 6
7 8 9 10
11 12 13
> cat eg.txt | xargs
1 2 3 4 5 6 7 8 9 10 11 12 13one-line to multi-line
> cat eg.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12
13Work with find (concern with space in file name)
> find . -type f -name "*.txt" -print0 | xargs -0 rm -fCount number of code lines
> ls -1 *.py | xargs wc -l