Lifesaving snippets

(Last update 17.04.2023)

This post is mostly for myself so I don’t have to remember all the specifics.

Clipboard / terminal (Mac)

pbpaste | ...
... | pbcopy

★ DNS

dig bjelic.net ANY +noall +answer
nslookup www.bjelic.net 8.8.8.8
dscacheutil -flushcache

domain name by IP address

dig –x 172.217.14.238

🔖 SSL/TLS certificate chain

openssl s_client -showcerts -connect bjelic.net:443 </dev/null

╬ find duplicate files (same files) in the current directory

find . -size 20 \! -type d -exec cksum {} \; | \
sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | \
uniq -d | grep -hif - /tmp/f.tmp > dup.txt

⌲ cUrl request/response headers only

curl -s -v -o /dev/null https://www.electrictoolbox.com/

⌲ cUrl a SOAP service

curl "https://www.example.com/Service.svc" \
-H "SOAPAction: ""urn:service.example.com/IService/PresentationGet""" \
--data-binary @post.txt \
--comcodessed -k -D responseHeaders.txt \
-v -o response.txt

(optional headers)

-H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate" -H "Content-Type: text/xml; charset=UTF-8" -H "Accept: application/xml, text/xml, */*; q=0.01" -H "Cache-Control: no-cache" 

★ delete a folder with contents (⚠️ be aware of rm -rf dangers)

rm -rf [folder]

↪ Create a “soft” symbolic link to a folder

ln -s /path/to/original/ /path/to/link

★ copy via ssh

scp <source> <target> # remote name as [user@]remote: or as per config

➗ calculator

echo $((5 * 7 + 2))

⎈ Simple Python3 based http server in the current folder

python3 -m http.server 8000
# python2: python -m SimpleHTTPServer 8000

♺ Find the process

ps -fA | grep python

-A Display information about other users’ processes, …

-f Display the uid, pid, parent pid, recent CPU usage, …

✁ Tail a log

watch -n 5 tail -10 videos.csv

⬌ OpenSSL encrypt / decrypt

openssl des3 -in file.txt -out encrypted.txt
openssl des3 -d -in encrypted.txt -out normal.txt

☂ File character garbage cleanup

tr -cd '\11\12\15\40-\176' 'original.csv' 'cleaned.csv'

-c complement the set of values in string1.

-d Delete characters in string1 from the input.

⎈ Start Apache

sudo apachectl start

⎈ Edit hosts file

sudo nano /private/etc/hosts
dscacheutil -flushcache

Zip with password all the files in the current directory

zip -er archive.zip *.*

♺ Tasks, processes, jobs:

screen # ^a^d to detach
screen -ls
screen -r {__} # reattach
jobs -l

-l Provide more information about each job listed.

-p Display only the process IDs for the process group leaders of the selected jobs.

kill {pid}
kill -9 {pid}

♺ Switch to stopped task:

fg [%n]

Powershell

⌲ curl like

Invoke-WebRequest https://www.example.com/hello-world.html http://proxy.int:8080 -TimeoutSec 10
(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html")

⚆ create guid

[guid]::NewGuid()

☞ touch

(ls testFile.txt).LastWriteTime = Get-Date

Windows

echo %USERPROFILE%
%ALLUSERSPROFILE% %APPDATA% %CD% (current folder) %DATE% %COMPUTERNAME% %CMDEXTVERSION% %RANDOM% %TIME% %USERDOMAIN% %USERNAME% %USERPROFILE% %WINDIR%

Excel

sep=<delimiter>

as the first line of a CSV file will automatically tell Excel to use that character as the separator instead of the default comma.

Other

https://gist.github.com/bojanbjelic

https://gist.github.com/cikabo

Useful unicode characters (some of them turned into emojis during the time)

⬌ ╬ℹ↪⎈☂♺☞⌲⚆✄✔✅×✖★☆≈±🔎🔜📌🔔👋👍✌✈⌨⚓△💼💳💵💥💡📁📂📄🎉👽🚀💀🔥📊📈📅⌚📆

q

pbpaste | q "select distinct c1 from -"
q -d, "SELECT u2.c1,u2.c2,u1.c1,u1.c2 FROM users-update.csv AS u1 INNER JOIN users-update.csv AS u2 on u1.c20=u2.c3 order by u2.c1, u2.c2"
q -H -d, 'SELECT * from data/users.csv WHERE profileId="4815-92e6-f3a9bc93a691" OR fromUserProfileId="4815-92e6-f3a9bc93a691"

http://harelba.github.io/q/

jq

jq '.[] | .url' org-repos.json
pbpaste | jq -r '.[] | [.name, .size] | @tsv' | pbcopy
jq ".ratedProducts | max_by(.annualSaving)" response_comparison_private_nodiscounts.json

jq '.ratedProducts | map(select(.productProperties[].propName == "ONLINE_PRODUCT" and .contractTermInfo.priceGuaranteeType == "GUARANTEE")) | length' response-temp.json

jq '.ratedProducts | map(select(.productProperties[].propName == "ONLINE_PRODUCT" and .contractTermInfo.priceGuaranteeType == "GUARANTEE")) | max_by(.annualSaving) | .annualSaving' response-temp.json

jq ".ratedProducts | max_by(.annualSaving) | .annualSaving" response-temp.json

jq '.ratedProducts | map(select(.productProperties[].propName == "ONLINE_PRODUCT" and .productProperties[].propName == "TOTAL_ACCOUNT" and .contractTermInfo.priceGuaranteeType == "GUARANTEE")) | max_by(.annualSaving)' "response gas wrong mainProductId 23-07-2021.json"

----
jq '.elements | map(select(.name=="one"))' jqtest.json
jq '.elements | map(select(.properties[].p2 == "y"))' jqtest.json
jq '.elements | map(select(.properties[].p2 == "y" and .properties[].p1 == "x"))' jqtest.json

---
echo '{
"t": [{
    "name": "George",
    "id": 20,
    "email": [ "george@domain1.com" , "george@domain2.com" ]
}, {
    "name": "Jack",
    "id": 18,
    "email": [ "jack@domain3.com" , "jack@domain5.com" ]
}, {
    "name": "Joe",
    "id": 19,
    "email": [ "joe@domain.com" ]
}]
}' | jq -r '.t | (["ID","NAME"] | (., map(length*"-"))), (.[] | [.id, .name]) | @tsv'

pbpaste | jq -r '.t[] | [.id, .name] | @tsv'

jq -r '.records| (["Name","Id","CreatedDate","brandId","mainId"] | (., map(length*"-"))), (.[] | [.Name, .Id, .CreatedDate, .brandId__c, .mainId__c, .mainAssociationId__c]) | @tsv'

cat ~/Documents/jira-updated.json | jq -r '.issues[] | [.fields.summary, .fields.parent.fields.summary]'

cat ~/Documents/jira-updated.json | jq -r '[.issues[] | .fields.parent?.fields.summary] | unique'

curl --silent --location -g --request GET 'https://tatatuproject.atlassian.net/rest/api/3/search?jql=updatedDate%20%3E%20startOfWeek(-1)%20AND%20updatedDate%20%3C%20startOfWeek()%20ORDER%20BY%20cf[10014]%20ASC,%20created%20DESC&maxResults=500' --header 'Authorization: Basic _' | jq -r '[.issues[] | .fields.parent?.fields.summary] | unique'

cat ~/moderation.json | jq -r '.result[] | select(.user.id == "6235e5d7cb2f08701d7ea243")'

https://stedolan.github.io/jq/

Gist

AWS

for region in `aws ec2 describe-regions --output text | cut -f4`; do echo -e "\nListing Instances in region:'$region'..."; aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, type: .InstanceType, key: .KeyName})' >> aws-instances.txt; done

for task in `aws ecs list-tasks --cluster "arn:aws:ecs:eu-central-1:546289415761:cluster/tatatu-ecs-stage" --output text | cut -f2`; do aws ecs describe-tasks --cluster "arn:aws:ecs:eu-central-1:546289415761:cluster/tatatu-ecs-stage" --tasks "$task" | jq '(.tasks[] |  [.group, .cpu, .memory, .availabilityZone]) | @tsv' >> aws-ecs-stage.txt; done

cat aws_eu-central-1_instances.json | jq -r '.Reservations | map(select(.Instances[].State.Code == 16)) | (.[] | [.Instances[].InstanceType, .Instances[].Placement.AvailabilityZone]) | @tsv'  | pbcopy

cat aws_eu-central-1_volumes.json | jq -r '.Volumes | map(select(.State=="in-use")) | length'
cat aws_eu-central-1_volumes.json | jq -r '.Volumes | map(select(.State=="in-use")) | (.[] | [.VolumeType, .Size]) | @tsv' | pbcopy

for task in `aws ec2 describe-regions --output text | cut -f4`; do echo -e "\nListing Instances in region:'$region'..."; aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | [.State.Name, .KeyName, .InstanceType, .Placement.AvailabilityZone]) | @tsv' >> aws-instances.txt; done


Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.