snipt

Ctrl+h for KB shortcuts

Bash

validate IP

[[ "$IP" =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] && echo "valid" || echo "invalid"
https://snipt.net/embed/a9253d8df5f51a88c9becb1b4c407589/
/raw/a9253d8df5f51a88c9becb1b4c407589/
a9253d8df5f51a88c9becb1b4c407589
bash
Bash
1
2019-06-18T05:59:02
True
False
False
Oct 18, 2015 at 12:39 PM
/api/public/snipt/141789/
validate-ip
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="o">[[</span> <span class="s2">&quot;</span><span class="nv">$IP</span><span class="s2">&quot;</span> <span class="o">=</span>~ ^<span class="o">(([</span>0-9<span class="o">]</span><span class="p">|</span><span class="o">[</span>1-9<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>1<span class="o">[</span>0-9<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>2<span class="o">[</span>0-4<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>25<span class="o">[</span>0-5<span class="o">])</span>.<span class="o">){</span>3<span class="o">}([</span>0-9<span class="o">]</span><span class="p">|</span><span class="o">[</span>1-9<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>1<span class="o">[</span>0-9<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>2<span class="o">[</span>0-4<span class="o">][</span>0-9<span class="o">]</span><span class="p">|</span>25<span class="o">[</span>0-5<span class="o">])</span>$ <span class="o">]]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> <span class="s2">&quot;valid&quot;</span> <span class="o">||</span> <span class="nb">echo</span> <span class="s2">&quot;invalid&quot;</span> </span></pre></div> </td></tr></table>
bash, ip, linux

Bash

sed append to match

1
2
3
4
file="/path/to/file"
regex="find this .*"
suffix="-suffix"
sed -i "s/$regex/&$suffix/" $file
https://snipt.net/embed/940f084dd40897180b5a15ed7a27be6c/
/raw/940f084dd40897180b5a15ed7a27be6c/
940f084dd40897180b5a15ed7a27be6c
bash
Bash
4
2019-06-18T08:37:00
True
False
False
Oct 13, 2015 at 06:33 AM
/api/public/snipt/141656/
sed-append-to-match
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">file</span><span class="o">=</span><span class="s2">&quot;/path/to/file&quot;</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">regex</span><span class="o">=</span><span class="s2">&quot;find this .*&quot;</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">suffix</span><span class="o">=</span><span class="s2">&quot;-suffix&quot;</span> </span><span id="L-4"><a name="L-4"></a>sed -i <span class="s2">&quot;s/</span><span class="nv">$regex</span><span class="s2">/&amp;</span><span class="nv">$suffix</span><span class="s2">/&quot;</span> <span class="nv">$file</span> </span></pre></div> </td></tr></table>
append, bash, linux, regex, sed

Bash

Get package version from deb file

debFile="/path/to/deb/file"
dpkg -I "$debFile"  | grep -oP '(?<=Version: ).*'
https://snipt.net/embed/0bf8d894f6ead618ac4cf860825e6a34/
/raw/0bf8d894f6ead618ac4cf860825e6a34/
0bf8d894f6ead618ac4cf860825e6a34
bash
Bash
2
2019-06-19T00:26:37
True
False
False
Oct 06, 2015 at 08:16 AM
/api/public/snipt/141521/
get-package-version-from-deb-file
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">debFile</span><span class="o">=</span><span class="s2">&quot;/path/to/deb/file&quot;</span> </span><span id="L-2"><a name="L-2"></a>dpkg -I <span class="s2">&quot;</span><span class="nv">$debFile</span><span class="s2">&quot;</span> <span class="p">|</span> grep -oP <span class="s1">&#39;(?&lt;=Version: ).*&#39;</span> </span></pre></div> </td></tr></table>
bash, deb, linux, package, ubuntu

Bash

get certificate validity end date

file="/path/to/cert"
openssl x509 -enddate -noout -in "$file"
https://snipt.net/embed/6b8f7fa0414e49696df5002e3239f3cb/
/raw/6b8f7fa0414e49696df5002e3239f3cb/
6b8f7fa0414e49696df5002e3239f3cb
bash
Bash
2
2019-06-17T22:00:53
True
False
False
/api/public/snipt/141376/
get-certificate-validity-end-date
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">file</span><span class="o">=</span><span class="s2">&quot;/path/to/cert&quot;</span> </span><span id="L-2"><a name="L-2"></a>openssl x509 -enddate -noout -in <span class="s2">&quot;</span><span class="nv">$file</span><span class="s2">&quot;</span> </span></pre></div> </td></tr></table>
cert, expiration, linux, openssl, x509

Bash

get file size

file="/path/to/some/file"
size=$(du -sh "$file" 2>/dev/null | awk '{print $1}')
https://snipt.net/embed/50d37b912c2717665d2a4269be762565/
/raw/50d37b912c2717665d2a4269be762565/
50d37b912c2717665d2a4269be762565
bash
Bash
2
2019-06-19T13:56:56
True
False
False
/api/public/snipt/141375/
get-file-size
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">file</span><span class="o">=</span><span class="s2">&quot;/path/to/some/file&quot;</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">size</span><span class="o">=</span><span class="k">$(</span>du -sh <span class="s2">&quot;</span><span class="nv">$file</span><span class="s2">&quot;</span> 2&gt;/dev/null <span class="p">|</span> awk <span class="s1">&#39;{print $1}&#39;</span><span class="k">)</span> </span></pre></div> </td></tr></table>
linux, size

Bash

get file MD5

file="/path/to/some/file"
md5=$(md5sum "$file" 2>/dev/null | awk '{print $1}')
https://snipt.net/embed/7502b80883172beaabff83951c07909a/
/raw/7502b80883172beaabff83951c07909a/
7502b80883172beaabff83951c07909a
bash
Bash
2
2019-06-18T14:13:42
True
False
False
Oct 01, 2015 at 05:07 AM
/api/public/snipt/141374/
get-file-md5
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nv">file</span><span class="o">=</span><span class="s2">&quot;/path/to/some/file&quot;</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">md5</span><span class="o">=</span><span class="k">$(</span>md5sum <span class="s2">&quot;</span><span class="nv">$file</span><span class="s2">&quot;</span> 2&gt;/dev/null <span class="p">|</span> awk <span class="s1">&#39;{print $1}&#39;</span><span class="k">)</span> </span></pre></div> </td></tr></table>
hash, linux, md5