Adventures in CS

Bash Cheatsheet

20 May 2019

Here a set of Bash one-liners that I have used in past

Starting ssh-agent in the background:

$ eval "$(ssh-agent -s)"

Adding ssh key to ssh-agent:

$ ssh-add -K <ssh private key>

Run shell script when a file or directory changes:

Install entr using apt install entr

For changes to file:

ls -d *.c | entr sh -c 'make && make test'

For changes to directories:

while true; do find path/ | entr -d echo Changed; sleep 10; done


while true; do ls path/* | entr -pd echo Changed; sleep 10; done

Starting a remote ssh command and shifting it to background and getting the PID of the process:

ssh -n "nohup >/dev/null 2>&1 &"
SERV_PID=$(ssh 'echo $!')

Adding numbers to the beginning of every line:

awk '{printf("%010d %s\n", NR, $0)}' example.txt

Local and remote variable inside an SSH command:

ssh host@name "B=3; echo $A; echo \$B;"

Remove the first line of a file:

tail -n +2 "$FILE"

Increment a variable:


Splitting string by the first occurrence of a delimiter:

id="$( cut -d ';' -f 1 <<< "$s" )";

Disable password authentication:

Add the following to /etc/ssh/sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Then restart ssh using sudo systemctl restart ssh

Changing java version:

sudo update-alternatives --config java

Useful CLI tools

TC tool for traffic control:

TC can only limit egress bandwidth (and do a bunch of other things like delays, packet loss and packet corruption). We can use the following command to limit egress bandwidth

tc qdisc add dev <interface> root tbf rate 1mbit latency 1us

You can remove the above limit using:

tc qdisc remove dev <interface> root tbf rate 1mbit latency 1us

IPERF for testing network performance:

On server side use iperf -s

On client side use iperf -c <serverip>


Tweet me @mb_ce if you like this post.

comments powered by Disqus