Skip to content

Basic commands

Below are basic commands with neat features

chmod

u (as user) + (adds) x (execute) rights. Minus for removing the right.

chmod u+x some_script.sh 


crontab

% sign must be back slashed.

0 * * * * /usr/bin/timeout 60m /usr/sbin/tcpdump -i eth0 -w /proxy_error_investigation/10_61_1_5_dump_"`date '+\%Y-\%m-\%d_\%H_\%M_\%S'`".pcap src 10.0.0.4


date

date -s "`ssh 172.18.4.54 date`"

date --set="$(ssh user@server date)"

#2020-03-11_22_26_39
date '+%Y-%m-%d_%H_%M_%S'

#2020-03-11T23:00Z
date '+%Y-%m-%dT%H:00Z'

# plus hour
date -v+1H

# in other command
tcpdump -i eth0 -w /var/lib/docker/terre-355-"`date '+%Y-%m-%d_%H_%M_%S'`".pcap > /var/lib/docker/terre-355-out.log

du

du -hs * | sort -h

ls | grep -v db_backup | xargs du -shc

find

find . -type f -newermt "2014-10-08 10:17:00" ! -newermt "2014-10-08 10:53:00"

zip /opt/shared_installer/FBCM-1177_db_logs.zip `find . -type f -newermt "2016-08-15 22:00:00"`

find -name "*filename*" -exec cp --parents '{}' /opt/shared_installer/to_find/ \;

find -path "*Domain*" -and -name "*_200_*" -exec du -h '{}' \;

find -name "*_151_*" -print0 | xargs -0 cp --parents -t /opt/shared_installer/files

find . -type f | while read x; do
# do something with $x
done

# DELETE found files
find -type f -name ".*" -delete

# files older then 1 day
find -type f -mmin +1440

for

#for each folder
for D in */; do echo $D; done
for D in */; do echo $D; cd $D; git status; cd ..; done

#!/bin/bash

for hour in {0..1}; do
hourprefix=""
hoursize=${#hour}
if [[ "$hoursize" -eq 1 ]]; then
hourprefix="0"
fi
for minute in {0..59}; do
minuteprefix=""
minutesize=${#minute}
if [[ "$minutesize" -eq 1 ]]; then
minuteprefix="0"
fi
for interval in {0..5}; do
grep_interval=$hourprefix$hour:$minuteprefix$minute:$interval
count=$(grep "20/Sep/2021:$grep_interval" stdout_20210920.log | wc -l)
echo $grep_interval $count
done
done
done

grep

# exclude 
grep -v

zgrep "email" ./archive/info/info.2016-10-*

# grep and 180 characters after
grep -o -P 'failed to parse field.{0,180}' /var/log/logstash/logstash-plain.log
# for macOS install ggrep, then use ggrep -o -P

# 10 characters before and after string
grep -o -P '.{0,10}failed to parse field.{0,10}' /var/log/logstash/logstash-plain.log

# when search string with dash in the beginning, it has to be escaped. E.g. -2024-02-02
grep “\-[0-9]{4}-[0-9]{2}-[0-9]{2}” file.txt

helm

# how to use the application
helm get notes -n namespace app

journalctl

# since -S until -U
journalctl -u named -S "2019-09-11 15:00:00" -U "2019-09-11 16:00:00"
journalctl --vacuum-size=100M

jq

# select only some fields from json 
jq .<first_element>.<second_elemnt> < /tmp/in.json

# only top level elements
cat scratch.json | jq '. |= keys'

less

# less keyboard http://sheet.shiar.nl/less
/ - search
n - next occurance
p - previous occurance
u - up
d - down
j - one line down
l - one up up
g - start of file
G - end of file
F - follow log

less -N // show line numbers

ls

ls --full-time

# sort by time
ls -t
# sort by time reverse 
ls -tr

# ls and size
ls | grep -v db_backup | xargs du -shc

netcat

# listens for first contact, -p port
netcat -l -p 8080

openssl

# print cert
openssl x509 -in uat-root-cert1.crt -text -noout

# print remote cert
openssl s_client -showcerts -connect devopsmanager.eu:443

# verify certificate is generated from another certificate
openssl verify -CAfile <ca-bundle.crt> <certificate.crt>

# verify csr
openssl req -text -noout -verify -in in.csr

screen

screen -ls
screen -r <id>
Inside screen: CTRL+a d (detach)

sed

https://linuxhint.com/50_sed_command_examples/

sed -i "s/^M//" file.csv (interactive)
sed -e "s/^M//" file.csv > clear_file.csv (Character ^M is written by CTRL-V CTRL-M )

# on mac the -i '' must be used
sed -i '' 'command'

# replaces , for ;
sed -i '' 's/,/;/g' dms_folders*.sh;

# removes four lines in the beginning of the document
sed -i '' "1,4d" dms_folders*.sh; 

# adds mkdir command to the start
sed -i '' "s/\"\//mkdir -p \"\/opt\/dms\/storage\//" dms_folders*.sh; # 

# removes bracket on the last line
sed -i '' "$ d" dms_folders*.sh

# add ; to the last line
sed -i '' "$ s/$/;/" dms_folders*.sh; 

# insert uuid to ndjson
sed -i 's/^{/{"uuid":"UUIDGEN",/g' 10000.ndjson

# get second line
sed -n '2p' myfile

ssh

# end of hanged connection
# keyboard sequence of Enter, ~, .
ENTER~.

tar

# make archive
tar -czvf /tmp/archive.tar.gz ./*
# extract
tar -xzvf archive.tar.gz

tcpdump

tcpdump -i eth0 -w /var/lib/docker/terre-355-"`date '+%Y-%m-%d_%H_%M_%S'`".pcap > /var/lib/docker/terre-355-out.log

timeout

timeout 5m tcpdump -i eth0 -w /var/lib/docker/terre-355-"`date '+%Y-%m-%d_%H_%M_%S'`".pcap > /var/lib/docker/terre-355-out.log

vim

# http://www.fprintf.net/vimCheatSheet.html
dd - delete line (cut line)
p - paste line (after dd)
A - end of the line and start editing
I - start of the line and start editing
$ or g_ end of the line without editing
^ or _ start of the line without editing

# line numbers
:set number
:set nonumber

# replace
:%s/WORD-To-Find-HERE/Replace-Word-Here/g

# visual block
ctrl-v
select more lines
shift-i 
insert character
esc, esc

while

# adds uuid to every line
while read; do
echo "{\"uuid\":\"$(uuidgen)\"$REPLY" >> 10000_with_uuid.ndjson
done < 10000.ndjson

xmllint

# format xml document
xmllint --format doc.xml
# xml validation against xsd schema
xmllint --noout --schema schema.xsd file.xml

zip

# split zip archives by 20MB
zip -s 20m