mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-22 12:57:01 +00:00
Compare commits
No commits in common. "3199d638aee109469a43e8d784df45d1fd5e5f56" and "212326d7a3274f16d85f16b18d144c7ddbc2194c" have entirely different histories.
3199d638ae
...
212326d7a3
72
README.md
72
README.md
@ -545,7 +545,7 @@ sed -i '$ s/.$//' filename
|
|||||||
|
|
||||||
##### Add string to beginning of file (e.g. "\[")
|
##### Add string to beginning of file (e.g. "\[")
|
||||||
```bash
|
```bash
|
||||||
sed -i '1s/^/[/' filename
|
sed -i '1s/^/[/' file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Add string at certain line number (e.g. add 'something' to line 1 and line 3)
|
##### Add string at certain line number (e.g. add 'something' to line 1 and line 3)
|
||||||
@ -692,7 +692,7 @@ sed '$ s/.$//'
|
|||||||
|
|
||||||
##### Insert character at specified position of file (e.g. AAAAAA --> AAA#AAA)
|
##### Insert character at specified position of file (e.g. AAAAAA --> AAA#AAA)
|
||||||
```bash
|
```bash
|
||||||
sed -r -e 's/^.{3}/&#/' filename
|
sed -r -e 's/^.{3}/&#/' file
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@ -747,7 +747,7 @@ awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
|
|||||||
|
|
||||||
##### Print filename and last line of all files in directory
|
##### Print filename and last line of all files in directory
|
||||||
```bash
|
```bash
|
||||||
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' filename
|
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Add string to the beginning of a column (e.g add "chr" to column $3)
|
##### Add string to the beginning of a column (e.g add "chr" to column $3)
|
||||||
@ -757,12 +757,12 @@ awk 'BEGIN{OFS="\t"}$3="chr"$3'
|
|||||||
|
|
||||||
##### Remove lines with string (e.g. 'bbo')
|
##### Remove lines with string (e.g. 'bbo')
|
||||||
```bash
|
```bash
|
||||||
awk '!/bbo/' filename
|
awk '!/bbo/' file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Remove last column
|
##### Remove last column
|
||||||
```bash
|
```bash
|
||||||
awk 'NF{NF-=1};1' filename
|
awk 'NF{NF-=1};1' file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Usage and meaning of NR and FNR
|
##### Usage and meaning of NR and FNR
|
||||||
@ -827,7 +827,7 @@ awk '{printf("%s\t%s\n",NR,$0)}'
|
|||||||
# David cat
|
# David cat
|
||||||
# David dog
|
# David dog
|
||||||
|
|
||||||
awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' filename
|
awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file
|
||||||
|
|
||||||
# Detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
|
# Detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
|
||||||
```
|
```
|
||||||
@ -941,7 +941,7 @@ find /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file
|
|||||||
|
|
||||||
##### With sed
|
##### With sed
|
||||||
```bash
|
```bash
|
||||||
ls |xargs -n1 -I file sed -i '/^Pos/d' filename
|
ls |xargs -n1 -I file sed -i '/^Pos/d' file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Add the file name to the first line of file
|
##### Add the file name to the first line of file
|
||||||
@ -1052,10 +1052,9 @@ else
|
|||||||
echo >&2 "Fatal error. This script requires mydir."
|
echo >&2 "Fatal error. This script requires mydir."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if a variable is null
|
# if variable is null
|
||||||
if [ -z "$var" ]; then echo "NULL"; else echo "Not NULL"; fi
|
if [ ! -s "myvariable" ]; then echo -e "variable is null!" ; fi
|
||||||
# or
|
#True of the length if "STRING" is zero.
|
||||||
[ -z "$var" ] && echo "NULL"
|
|
||||||
|
|
||||||
# Using test command (same as []), to test if the length of variable is nonzero
|
# Using test command (same as []), to test if the length of variable is nonzero
|
||||||
test -n "$myvariable" && echo myvariable is "$myvariable" || echo myvariable is not set
|
test -n "$myvariable" && echo myvariable is "$myvariable" || echo myvariable is not set
|
||||||
@ -1552,17 +1551,9 @@ stat filename.txt
|
|||||||
ps aux
|
ps aux
|
||||||
```
|
```
|
||||||
|
|
||||||
##### List processes by top memory usage
|
|
||||||
```bash
|
|
||||||
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Display a tree of processes
|
##### Display a tree of processes
|
||||||
```bash
|
```bash
|
||||||
pstree
|
pstree
|
||||||
|
|
||||||
# or
|
|
||||||
ps aux --forest
|
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Find maximum number of processes
|
##### Find maximum number of processes
|
||||||
@ -1968,7 +1959,7 @@ ldconfig -p
|
|||||||
ldd /bin/ls
|
ldd /bin/ls
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Check the most recent login of all users
|
##### Check user login
|
||||||
```bash
|
```bash
|
||||||
lastlog
|
lastlog
|
||||||
```
|
```
|
||||||
@ -2281,12 +2272,7 @@ sar -f /var/log/sa/sa31|tail
|
|||||||
journalctl --file ./log/journal/a90c18f62af546ccba02fa3734f00a04/system.journal --since "2020-02-11 00:00:00"
|
journalctl --file ./log/journal/a90c18f62af546ccba02fa3734f00a04/system.journal --since "2020-02-11 00:00:00"
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Show a listing of last logged in users
|
##### Show a listing of last logged in users.
|
||||||
```bash
|
|
||||||
last
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Show a listing of unsuccessful (bad) login attempts
|
|
||||||
```bash
|
```bash
|
||||||
lastb
|
lastb
|
||||||
```
|
```
|
||||||
@ -2464,11 +2450,6 @@ dig +short www.example.com
|
|||||||
host www.example.com
|
host www.example.com
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Check public IP address
|
|
||||||
```bash
|
|
||||||
curl http://checkip.amazonaws.com
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Get DNS TXT record a of domain
|
##### Get DNS TXT record a of domain
|
||||||
```bash
|
```bash
|
||||||
dig -t txt www.example.com
|
dig -t txt www.example.com
|
||||||
@ -2509,7 +2490,7 @@ $ sudo nc -l 80
|
|||||||
|
|
||||||
##### Check which ports are listening for TCP connections from the network
|
##### Check which ports are listening for TCP connections from the network
|
||||||
```bash
|
```bash
|
||||||
# note that some companies might not like you using nmap
|
#notice that some companies might not like you using nmap
|
||||||
nmap -sT -O localhost
|
nmap -sT -O localhost
|
||||||
|
|
||||||
# check port 0-65535
|
# check port 0-65535
|
||||||
@ -2517,7 +2498,7 @@ nmap -p0-65535 localhost
|
|||||||
```
|
```
|
||||||
##### Check if a host is up and scan for open ports, also skip host discovery.
|
##### Check if a host is up and scan for open ports, also skip host discovery.
|
||||||
```bash
|
```bash
|
||||||
# skips checking if the host is alive. this may sometimes cause a false positive, stopping the scan.
|
#skips checking if the host is alive which may sometimes cause a false positive and stop the scan.
|
||||||
$ nmap google.com -Pn
|
$ nmap google.com -Pn
|
||||||
|
|
||||||
# Example output:
|
# Example output:
|
||||||
@ -2539,7 +2520,7 @@ $ nmap -A -T4 scanme.nmap.org
|
|||||||
# -A to enable OS and version detection, script scanning, and traceroute; -T4 for faster execution
|
# -A to enable OS and version detection, script scanning, and traceroute; -T4 for faster execution
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Look up website information (e.g. name server), searches for an object in a RFC 3912 database
|
##### Look up website information (e.g. name server), searches for an object in a RFC 3912 database.
|
||||||
```bash
|
```bash
|
||||||
whois google.com
|
whois google.com
|
||||||
```
|
```
|
||||||
@ -2549,7 +2530,7 @@ whois google.com
|
|||||||
openssl s_client -showcerts -connect www.example.com:443
|
openssl s_client -showcerts -connect www.example.com:443
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Display network interfaces and their associated IP addresses
|
##### Display IP address
|
||||||
```bash
|
```bash
|
||||||
ip a
|
ip a
|
||||||
```
|
```
|
||||||
@ -2609,10 +2590,6 @@ curl -I http://example.com/
|
|||||||
# Accept-Ranges: bytes
|
# Accept-Ranges: bytes
|
||||||
# Vary: Accept-Encoding
|
# Vary: Accept-Encoding
|
||||||
```
|
```
|
||||||
##### Find out the time spent between request and response
|
|
||||||
```
|
|
||||||
curl -v -o /dev/null -s -w 'Total: %{time_total}s\n' google.com
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Find out the http status code of a URL
|
##### Find out the http status code of a URL
|
||||||
```bash
|
```bash
|
||||||
@ -2893,7 +2870,7 @@ echo -e 'text here \c'
|
|||||||
|
|
||||||
##### View first 50 characters of file
|
##### View first 50 characters of file
|
||||||
```bash
|
```bash
|
||||||
head -c 50 filename
|
head -c 50 file
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Cut and get last column of a file
|
##### Cut and get last column of a file
|
||||||
@ -2931,11 +2908,6 @@ cat >>myfile
|
|||||||
let me add sth here
|
let me add sth here
|
||||||
# exit with ctrl+d
|
# exit with ctrl+d
|
||||||
|
|
||||||
# or
|
|
||||||
cat << EoF >> filename
|
|
||||||
> add something here
|
|
||||||
> EoF
|
|
||||||
|
|
||||||
# or using tee
|
# or using tee
|
||||||
tee -a myfile
|
tee -a myfile
|
||||||
let me add sth else here
|
let me add sth else here
|
||||||
@ -2954,7 +2926,7 @@ echo 'hihi' >>filename
|
|||||||
|
|
||||||
##### Working with json data
|
##### Working with json data
|
||||||
```bash
|
```bash
|
||||||
# Install the useful jq package
|
#install the useful jq package
|
||||||
#sudo apt-get install jq
|
#sudo apt-get install jq
|
||||||
#e.g. to get all the values of the 'url' key, simply pipe the json to the following jq command(you can use .[]. to select inner json, i.e jq '.[].url')
|
#e.g. to get all the values of the 'url' key, simply pipe the json to the following jq command(you can use .[]. to select inner json, i.e jq '.[].url')
|
||||||
cat file.json | jq '.url'
|
cat file.json | jq '.url'
|
||||||
@ -3029,7 +3001,7 @@ while read a b; do yes $b |head -n $a ; done <test.txt
|
|||||||
## Others
|
## Others
|
||||||
[[back to top](#handy-bash-one-liners)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Describe the format and characteristics of image files
|
##### Describe the format and characteristics of image files.
|
||||||
```bash
|
```bash
|
||||||
identify myimage.png
|
identify myimage.png
|
||||||
#myimage.png PNG 1049x747 1049x747+0+0 8-bit sRGB 1.006MB 0.000u 0:00.000
|
#myimage.png PNG 1049x747 1049x747+0+0 8-bit sRGB 1.006MB 0.000u 0:00.000
|
||||||
@ -3305,12 +3277,6 @@ scp -r directoryname user@ip:/path/to/send
|
|||||||
# :(){:|:&};:
|
# :(){:|:&};:
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Trigger kernel crash
|
|
||||||
```bash
|
|
||||||
# Don't try this at home!
|
|
||||||
echo c > /proc/sysrq-trigger
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Use the last argument
|
##### Use the last argument
|
||||||
```bash
|
```bash
|
||||||
!$
|
!$
|
||||||
|
Loading…
Reference in New Issue
Block a user