Taking your BASH skills to the next level
A talk by torstein @ escenic
grep
find
egrep
, xargs
and |
wc
& cat
You can do incredible things
Maven modules are there in your project?
$ find -name pom.xml | wc -l
lines of Java code is there in your project?
$ find -name "*.java" | xargs cat | wc -l
lines of production Java code? I.e. not tests?
$ find -name "*.java" | grep src/main/java | xargs cat | wc -l
$ find -name "*.java" -o -name "*.properties" | \
grep trunk | \
grep src/main/java | \
xargs grep -ni --color IOObjectLoader
Don’t look at your IDEA project structure and guess.
$ grep -r com.example.app.MyClass /opt/tomcat*
What’s the problem with grep
here?
$ find -L /opt/tomcat -name "*.jar" | \
xargs grep com.example.app.MyClass
What’s the problem with find
and grep
here?
$ find -L /opt/tomcat -name "*.jar" | \
while read f; do \
echo $f;
unzip -t $f | grep com.example.app.MyClass; \
done
$ /opt/escenic/engine/bin/find-jar \
/opt/tomcat \
com.example.app.MyClass
How was that ssh
command again?
↑ ↑ ↑
$ history
$ grep ssh ~/.bash_history
Ctrl + r
(reverse-i-search)`ssh': ssh -L 99:localhost:80 foo@login.example.com
At the start Ctrl + a
‐
End of line.: Ctrl + e
‐
Delete letter: Ctrl + d
‐
Kill the rest: Ctrl + k
Delete word Alt + d
‐
Delete pevious word: Alt + ←
‐
Uppercase word: Alt + u
‐
Lowercase word: Alt + l
$ ssh-keygen -t rsa
$ ssh-copy-id user@host
$ ssh user@host
Different usernames on different servers
Host *.dev.example.com
User frodo
Install & enable BASH completion:
# apt-get install bash-completion
$ echo "source /usr/share/bash-completion/bash_completion" \
>> ~/.bashrc
$ source ~/.bashrc
Be sure that your system doesn’t hash the host names in ~/.ssh/known_hosts
:
$ echo "HashKnownHosts no" >> ~/.ssh/config
$ ssh p[TAB][TAB]
p4.dev.example.com
projects.example.com
Super useful. Makes life bearable in a world of stubborn sys admins.
$ ssh -L 9980:localhost:80 closy
Now, browse http://localhost:9980 and you’ll be in fact accessing http://closy:80
You may want to set the Host
header right
# echo 127.0.0.1 closy >> /etc/hosts
Now, you can use http://closy:9980 passing the correct Host
header through the tunnel to your target system.
If the CLI is enough:
$ curl -H "Host: closy" http://localhost:9980
$ p4 changes | grep 2015/08/03
$ for el in 3 4 5 6 7 ; do p4 changes | grep 2015/08/0${el}; done
$ for el in {3..7} ; do p4 changes | grep 2015/08/0${el}; done
$ for el in {03..7} ; do p4 changes | grep 2015/08/${el}; done
How to use ssh
? Read the man
pages!
$ man ssh
Searching man
will open your eyes:
$ man -k ssh
✏ torstein @ escenic dot com