General Essential Tips - degutos/wikis GitHub Wiki
General Essential Tips
ITERM 2 tips
say something
Use say and blah blah blah to your terminal to say whatever you want
➜ ~ say You can say anything you want
PBCOPY
Use bpcopy to copy something to your clipboard
➜ ~ ls -lh | pbcopy
the content of your command will be added to your clipboard
How to see any wifi password you have ever used
this is cool when you want to share your wifi password with a friend
security find-generic-password -wa "deguto"
you can concatenate the above command with | pbcopy and copy your password to clipboard
caffeinate
Use caffeinate on your terminal to hold your terminal up, your MacBook will not go into sleep mode
$ caffeinate
Few terminal application to show on screen
$ cmatrix
$ asciiquarium
$ toilet something_you_want_to_print_on_screen
For all the above we can install with $ brew install toilet
Opening a WebServer on your Macbook
$ python3 -m http.server
This will open a http server on your Macbook, if you want share a file on your Download folder just cd into folder and run the above command. From the browser anyone on your subnet will be able to browser files on your local directory
fpaste
- Copy some content from your Linux terminal and paste it into a the web for sharing purpose. This is a tool for fedora and centos family
[root@fedora36-template ~]# df -h | fpaste
Uploading (0.6KiB)...
https://paste.centos.org/view/0a2cfa51
The content will be available in that URL https://paste.centos.org/view/0a2cfa51
[root@fedora36-template ~]# top -b -n1 | fpaste
Uploading (12.6KiB)...
https://paste.centos.org/view/2389a47d
The content will be available for 24 hours as default
Check for $ man fpaste for more options
we can use fbcopy also
Using wl-copy and wl-paste
[root@fedora36-template ~]# sudo dnf install wl-clipboard
$ cat /etc/hosts | wl-copy
$ wl-paste
This will paste whatever content is in the CLIPBOARD
How to create a variable array and iterate through it
➜ ~ ARRAY=(HOST1 HOST2 HOST3 HOST4 HOST5)
➜ ~ for i in "${ARRAY[@]}" ; do echo "$i" ; done
HOST1
HOST2
HOST3
HOST4
HOST5
How to change your bash prompt color to green
export PS1="\[\e[32;40m\][\u@\h \W]\$\[\e[0m\] "
We can also add this line to $HOME/.bashrc file
[root@vsi-andre-rocky-eu-de ~]$ cat ~/.bashrc | grep PS1
PS1="\[\e[32;40m\][\u@\h \W]\$\[\e[0m\] "
How to know the host ip when you don't have ifconfig nor ip a commands
$ cat /proc/net/fib_trie | grep "|--" | egrep -v "0.0.0.0| 127."
OR (with luck)
$ hostname -i
Awk
Show specific columns from a file with awk and add content
$ awk -F ":" '{print "O usuario " $1 "tem o diretorio " $6}' /etc/passwd
Using cut sed and awk
$ cat /proc/meminfo | grep -i memtotal | cut -d: -f 2 | sed 's/^ *//g' | cut -d " " -f 1 | awk '{print $0/1024" MB"}'
3794.33 MB
AWK SORT and CUT
Lets consider the following original text file
➜ /tmp cat file.txt
hello my name is John: 4
hello my name is Joi: 45
hello my name is Loi: 23
hello my name is Jordan: 476
hello my name is Manu: 98
We want to order this by number from higher to smaller
awk to print entire file (like cat command does) we use variable print $0
➜ /tmp awk {'print $0'} file.txt
hello my name is John: 4
hello my name is Joi: 45
hello my name is Loi: 23
hello my name is Jordan: 476
hello my name is Manu: 98
awk to print last column only we use $NF
➜ /tmp awk {'print $NF'} file.txt
4
45
23
476
98
awk to print both together
``➜ /tmp awk {'print $NF,$0'} file.txt 4 hello my name is John: 4 45 hello my name is Joi: 45 23 hello my name is Loi: 23 476 hello my name is Jordan: 476 98 hello my name is Manu: 98
With this we repeat the numbers first and then we print all the content line again with $0
### SORT
sort -n will order numbers and not letters
sort -r will sort reverse order from higher to smaller
➜ /tmp awk {'print $NF,$0'} file.txt | sort -nr 476 hello my name is Jordan: 476 98 hello my name is Manu: 98 45 hello my name is Joi: 45 23 hello my name is Loi: 23 4 hello my name is John: 4
Now we have numbers in order by higher first, but we sill have the number printed twice, we will need fix this
### CUT
$ cut -d ' ' specify the delimiter for cut, in this case we consider as a space $ cut -f 2 specify to get the second column $ cut -f 2-7 specify to get from the second column to the 7th column $ cut -f 2- specify to get from the second column until the end (last column).
### All commands together
➜ /tmp awk {'print $NF,$0'} file.txt| sort -nr | cut -d ' ' -f 2- hello my name is Jordan: 476 hello my name is Manu: 98 hello my name is Joi: 45 hello my name is Loi: 23 hello my name is John: 4
---
# SED
Given the file palavras
degutos@raspberrypi:~ $ cat palavras casa1 casa1 casa1 casa1 casa2 casa2 casa2 casa1 casa3 casa1 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
- Use SED to replace a word
degutos@raspberrypi:~ $ sed "s/casa1/casa001/g" palavras casa001 casa001 casa001 casa001 casa2 casa2 casa2 casa001 casa3 casa001 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa001 casa5
- use -i parameter to save the output to original file
$ sed -i "s/casa1/casa001/g" palavras
- replace the 3rd occurrence only
degutos@raspberrypi:~ $ sed "s/casa1/casa001/3" palavras casa1 casa1 casa001 casa1 casa2 casa2 casa2 casa1 casa3 casa1 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
- replace from the 3rd occurrence until the end
degutos@raspberrypi:~ $ sed "s/casa1/casa001/3g" palavras casa1 casa1 casa001 casa001 casa2 casa2 casa2 casa1 casa3 casa1 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
- replace from 1st line and 3rd line
degutos@raspberrypi:~ $ sed "1,3 s/casa1/casa001/g" palavras casa001 casa001 casa001 casa001 casa2 casa2 casa2 casa001 casa3 casa001 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
- replace on 1st line and 3rd line
degutos@raspberrypi:~ $ sed -e "1 s/casa1/casa001/g" -e "3 s/casa1/casa001/g" palavras casa001 casa001 casa001 casa001 casa2 casa2 casa2 casa1 casa3 casa001 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
- replace 2nd line the first caracter found and from the 3rd line replace all
degutos@raspberrypi:~ $ sed -e "2 s/casa1/casa001/1g" -e "s/casa1/casa001/g" palavras
casa001 casa001 casa001 casa001
casa2 casa2 casa2 casa001
casa3 casa001 casa3 casa3
casa4 casa4 casa4 casa4
casa5 casa5 casa001 casa5
- replace if a line has no casa2
degutos@raspberrypi:~ $ sed "/casa2/! s/casa1/casa001/g" palavras casa001 casa001 casa001 casa001 casa2 casa2 casa2 casa1 casa3 casa001 casa3 casa3 casa4 casa4 casa4 casa4 casa5 casa5 casa001 casa5
- delete line if line starts with casa3
degutos@raspberrypi:~ $ sed "/^casa3/d" palavras casa1 casa1 casa1 casa1 casa2 casa2 casa2 casa1 casa4 casa4 casa4 casa4 casa5 casa5 casa1 casa5
### BASH
CTRL+W - Delete word to the left Option+Backspace - Delete word to the left until the next point CTRL+A - Cursor to initial line CTRL+E - End of the line CTRL+K - Delete till the end CTRL+U - Delete till the beginning