how to locate installed deb package
dpkg -L package-name
refs https://askubuntu.com/questions/843294/where-does-a-deb-file-go-after-installation
get current ip of ubuntu
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
ifconfig wlan0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
how to generate password
$1
is param for length of password
date +%s | sha256sum | base64 | head -c $1 ; echo
openssl rand -base64 $1
take previous command with !!
apt update
# error permission denied
sudo !!
run the 102 th command in the history
!102
Bash load .env file
export $(grep -v '^#' .env | xargs)
create new swap
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
(optional) make new swap pernament when reboot
sudo nano /etc/fstab
then add these new content
/swapfile swap swap defaults 0 0
remove a swap
sudo swapoff -v /swapfile
sudo rm /swapfile
delete entry in /etc/fstab if you added it
sudo nano /etc/fstab
change swap size
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Print current ip address
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
or
ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
or
ifconfig wlan0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
Find all occurrences of a string (basic usage)
Ex: Search package.json for any nashorn word
grep nashorn package.json
Case insensitive search in a file set
Ex: Search for any file in npm/test that ends with .js that has nashorn with case insensitive so "Nashorn" still match
grep -i nashorn npm/test/*.js
Find all the non-matching files
note: cái này KHÔNG recursive tìm trong folder con
Ex: Search for all files that don't have the word "nashorn" in npm/test/
grep -iL nashorn npm/test/*
note: cái này CÓ recursive tìm trong folder con
nhưng KHÔNG tìm được các hidden files VD: .env
, .git
do vậy xem
Ex: Search for all files that don't have the word "nashorn"
in npm/ and It sub directories
will search in "npm/" and "npm/test/" and "npm/module"
grep -iL nashorn npm/**/*
Finding patterns into hidden files and recursively into sub-directories
This is not efficient as it will spawn a new grep process for each file
find npm/test/ -type f -exec grep -iL nashorn \{}\;
This may have issues with filenames containing space-like characters
grep -iL nashorn $(find npm/test/ -type f)
-r for recursive include hidden ones
grep -irL nashorn npm/test/
grep -irL nashorn npm/
Filtering files by their name (using regular expressions)
grep -ir nashorn ./ | grep "^[^:]*\.java"
The first one is to use grep to find all files containing the pattern “nashorn”, then pipe the output of that first command to a second grep instance filtering out non-java source files:
note: Unless you specify the -F option, grep assumes the search pattern is a regular expression.
^
the start of the line[^:]*
followed by a sequence of any characters except a colon\.
followed by a dot (the dot has a special meaning in regex, so I had to protect it with a backslash to express I want a literal match)- java and followed by the four letters “java.”
In practice, since grep will use a colon to separate the filename from the context,
I keep only lines having .java
in the filename section.
Worth mention it would match also .javascript
filenames.
This is something I let try solving by yourself if you want.
grep -ir nashorn ./ --include='*.java'
put working directory on a stack
pushd
pop that directory
popd
determine file type
file FILEPATH
update database for locate
updatedb
locate a command
which COMMAND
display bash command history
history
display the on-line manual descriptions
whatis
search the manual page names and descriptions
apropos
an interface to the on-line reference manuals
man
concatenate files and print on the standard output
cat file1 file2 file3 ...
file perusal filter for crt viewing
more
less
command line text editor
nano
kill a running command
Ctrl + C
log out of bash
exit
builtin env variables
print random value between 0 - 32767
echo $RANDOM
print current shell
echo $SHELL
echo $USER
echo $PWD
echo $HOSTNAME
add-all-rules-to-iptables from file
# this add all rules from file to ip tables
# can replace $1 to any file name you want
# example usage
# bash script.sh /home/tuana9a/rules.txt
for tmp in $(cat "$1")
do echo sudo iptables -D $tmp
done
delete-all-rules-to-iptables
#!/bin/bash # this delete all rules in file to ip tables
# can replace $1 to any file name you want
# example usage
# bash script.sh /home/tuana9a/rules.txt
input="$1"
# read line by line then delete
while IFS= read -r line
do sudo iptables -D $line
done < "$input"
variable
#! /bin/bash
STRING="tuan thich gemdino vai lua"
echo "my quote is \"$STRING\""
echo "param1 is $1, param2 is $2, param4 is $4, param3 is $3"
echo $(whoami)
echo =================io======================
echo "what is your name"
read yourname
echo $yourname is nice, LIKE ME!
condition
#! /bin/bash
echo =================if else======================
read yourname
echo $yourname is nice, LIKE ME!
if [ $yourname ]; then echo "$yourname is nice"
else echo "$youname is empty, right?"
fi
loop
#! /bin/bash
for i in file1 file2 file3
do command1 $i command2 # ...
done
Calculator
echo $(( 2 + 2 )) # output: 4
echo $(( 2 * 3 )) # output: 6
echo $(( 2 / 3 )) # output: 0
echo $(( 10 / 3 )) # output: 3
echo $(( 10 % 3 )) # output: 1
echo $(( $RANDOM % 100 )) # output: random number between 0 - 99
This only apply to arithmetic operation