snipt

Ctrl+h for KB shortcuts

Text only

Linux commands

1
2
3
4
5
6
7
// tail
tail myfile.txt -n 100

// VI

/var // search for string
n // next occurence
https://snipt.net/embed/e881779d6c6006dbee71bec3844330ad/
https://snipt.net/raw/e881779d6c6006dbee71bec3844330ad/
e881779d6c6006dbee71bec3844330ad
text
Text only
7
2016-02-09T01:31:39
True
False
/api/public/snipt/143348/
linux-commands-0bb380f1
<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> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>// tail </span><span id="L-2"><a name="L-2"></a>tail myfile.txt -n 100 </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a>// VI </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a>/var // search for string </span><span id="L-7"><a name="L-7"></a>n // next occurence </span></pre></div> </td></tr></table>
linux

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/
https://snipt.net/raw/a9253d8df5f51a88c9becb1b4c407589/
a9253d8df5f51a88c9becb1b4c407589
bash
Bash
1
2016-02-09T00:26:30
True
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 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="nv">$ </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/
https://snipt.net/raw/940f084dd40897180b5a15ed7a27be6c/
940f084dd40897180b5a15ed7a27be6c
bash
Bash
4
2016-02-10T11:58:25
True
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 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/
https://snipt.net/raw/0bf8d894f6ead618ac4cf860825e6a34/
0bf8d894f6ead618ac4cf860825e6a34
bash
Bash
2
2016-02-09T00:28:34
True
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 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/
https://snipt.net/raw/6b8f7fa0414e49696df5002e3239f3cb/
6b8f7fa0414e49696df5002e3239f3cb
bash
Bash
2
2016-02-09T01:31:59
True
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 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/
https://snipt.net/raw/50d37b912c2717665d2a4269be762565/
50d37b912c2717665d2a4269be762565
bash
Bash
2
2016-02-09T01:32:49
True
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 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/
https://snipt.net/raw/7502b80883172beaabff83951c07909a/
7502b80883172beaabff83951c07909a
bash
Bash
2
2016-02-09T01:33:11
True
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 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

Bash

get installed version of package

packageName="dreampie"
apt-cache policy "$packageName" | grep -oP "(?<=Installed: ).*$" || { echo "Not installed"; }
https://snipt.net/embed/abe43dda393d51df967f0de029e78284/
https://snipt.net/raw/abe43dda393d51df967f0de029e78284/
abe43dda393d51df967f0de029e78284
bash
Bash
3
2016-02-09T01:33:34
True
False
/api/public/snipt/141322/
get-installed-version-of-package
<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 id="L-1"><a name="L-1"></a><span class="nv">packageName</span><span class="o">=</span><span class="s2">&quot;dreampie&quot;</span> </span><span id="L-2"><a name="L-2"></a>apt-cache policy <span class="s2">&quot;</span><span class="nv">$packageName</span><span class="s2">&quot;</span> <span class="p">|</span> grep -oP <span class="s2">&quot;(?&lt;=Installed: ).*</span><span class="nv">$&quot;</span><span class="s2"> || { echo &quot;</span>Not installed<span class="s2">&quot;; }</span> </span></pre></div> </td></tr></table>
apt, linux, package

Bash

add gpg key to apt keyring

url="http://www.example.com/gpg.key"
wget -O - "$url" | sudo apt-key add -
https://snipt.net/embed/14af6e64ee94a94600c79800a4daed82/
https://snipt.net/raw/14af6e64ee94a94600c79800a4daed82/
14af6e64ee94a94600c79800a4daed82
bash
Bash
2
2016-02-09T01:33:52
True
False
/api/public/snipt/141321/
add-gpg-key-to-apt
<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 id="L-1"><a name="L-1"></a><span class="nv">url</span><span class="o">=</span><span class="s2">&quot;http://www.example.com/gpg.key&quot;</span> </span><span id="L-2"><a name="L-2"></a>wget -O - <span class="s2">&quot;</span><span class="nv">$url</span><span class="s2">&quot;</span> <span class="p">|</span> sudo apt-key add - </span></pre></div> </td></tr></table>
apt, gpg, key, linux
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.