Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Bash Session

How many files in a folder

"ls *.pdf | wc -l"
https://snipt.net/embed/71df761d41673c79edc53c6427b60f3f/
https://snipt.net/raw/71df761d41673c79edc53c6427b60f3f/
71df761d41673c79edc53c6427b60f3f
shell-session
Bash Session
1
2016-12-10T17:09:09
True
False
False
Jan 14, 2014 at 08:27 AM
/api/public/snipt/105795/
how-many-files-in-a-folder
<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="go">&quot;ls *.pdf | wc -l&quot;</span> </span></pre></div> </td></tr></table>
ls, unix

Bash Session

wp.sh

1
2
3
4
5
6
# Finds out in which partition (wp) a given file/dir resides.
# Defaults to working directory.
# -----------------------------------------------------------
# http://snipt.net/metalcoder/wpsh
file=${1-.}
df -P $file | tail -1 | cut -d' ' -f 1
https://snipt.net/embed/e2057f158fa6f571e2f799aace9f8489/
https://snipt.net/raw/e2057f158fa6f571e2f799aace9f8489/
e2057f158fa6f571e2f799aace9f8489
shell-session
Bash Session
7
2016-12-07T02:58:23
True
False
False
/api/public/snipt/66239/
wpsh
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="gp">#</span> Finds out in which partition <span class="o">(</span>wp<span class="o">)</span> a given file/dir resides. </span><span id="L-2"><a name="L-2"></a><span class="gp">#</span> Defaults to working directory. </span><span id="L-3"><a name="L-3"></a><span class="gp">#</span> ----------------------------------------------------------- </span><span id="L-4"><a name="L-4"></a><span class="gp">#</span> http://snipt.net/metalcoder/wpsh </span><span id="L-5"><a name="L-5"></a><span class="go">file=${1-.}</span> </span><span id="L-6"><a name="L-6"></a><span class="go">df -P $file | tail -1 | cut -d&#39; &#39; -f 1</span> </span></pre></div> </td></tr></table>
file-system, linux, shellscript, sysadmin, unix

Bash

Split file into parts by size

#bigFile into bigFileaa bigFileab like
split --bytes=100M bigFile bigFile
https://snipt.net/embed/a48651799623e121e92ba4a034e2761c/
https://snipt.net/raw/a48651799623e121e92ba4a034e2761c/
a48651799623e121e92ba4a034e2761c
bash
Bash
2
2016-12-09T11:31:42
True
False
True
Jun 06, 2013 at 09:19 AM
/api/public/snipt/63383/
split-file-into-parts-by-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="c1">#bigFile into bigFileaa bigFileab like</span> </span><span id="L-2"><a name="L-2"></a>split --bytes<span class="o">=</span>100M bigFile bigFile </span></pre></div> </td></tr></table>
unix

Bash Session

perl convert dos to unix

perl -pi -e 's/\r\n|\n|\r/\r\n/g' file-to-convert  # Convert to DOS
perl -pi -e 's/\r\n|\n|\r/\n/g'   file-to-convert  # Convert to UNIX
https://snipt.net/embed/549871ea3347ad2dd3783eaf8305470e/
https://snipt.net/raw/549871ea3347ad2dd3783eaf8305470e/
549871ea3347ad2dd3783eaf8305470e
shell-session
Bash Session
3
2016-12-09T15:27:09
True
False
False
May 24, 2013 at 03:31 PM
/api/public/snipt/62165/
perl-convert-dos-to-unix
<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="go">perl -pi -e &#39;s/\r\n|\n|\r/\r\n/g&#39; file-to-convert # Convert to DOS</span> </span><span id="L-2"><a name="L-2"></a><span class="go">perl -pi -e &#39;s/\r\n|\n|\r/\n/g&#39; file-to-convert # Convert to UNIX</span> </span></pre></div> </td></tr></table>
convert, csv, dos, perl, unix

Text only

How to pretty-print JSON from the command line?

1
2
3
python -mjson.tool ugly.json > pretty.json

via http://stackoverflow.com/questions/352098/how-to-pretty-print-json-from-the-command-line
https://snipt.net/embed/98d63046e6e88ff075e961afa0d94441/
https://snipt.net/raw/98d63046e6e88ff075e961afa0d94441/
98d63046e6e88ff075e961afa0d94441
text
Text only
3
2016-12-07T02:58:52
True
False
False
May 20, 2013 at 06:27 AM
/api/public/snipt/61774/
how-to-pretty-print-json-from-the-command-line
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>python -mjson.tool ugly.json &gt; pretty.json </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a>via http://stackoverflow.com/questions/352098/how-to-pretty-print-json-from-the-command-line </span></pre></div> </td></tr></table>
command-line, json, pretty-print, python, unix

Bash

Convert epoch to Human Readable date

date -d @1365063840
Thu Apr  4 10:24:00 CEST 2013
https://snipt.net/embed/c5dc3f9efda88320e8a6a20de2e82b1a/
https://snipt.net/raw/c5dc3f9efda88320e8a6a20de2e82b1a/
c5dc3f9efda88320e8a6a20de2e82b1a
bash
Bash
2
2016-12-07T02:59:28
True
False
False
Apr 04, 2013 at 05:04 AM
/api/public/snipt/58775/
convert-epoch-to-human-readable-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>date -d @1365063840 </span><span id="L-2"><a name="L-2"></a>Thu Apr <span class="m">4</span> 10:24:00 CEST 2013 </span></pre></div> </td></tr></table>
date, unix

Bash

Restart of SSH Connections

pstree -cpal | egrep [s]shd | perl -lne '($i)=(/(\d+)/); `kill -1 $i`'
https://snipt.net/embed/8f8b6190734e8065fec2cd0b33808997/
https://snipt.net/raw/8f8b6190734e8065fec2cd0b33808997/
8f8b6190734e8065fec2cd0b33808997
bash
Bash
1
2016-12-07T03:00:08
True
False
False
Dec 20, 2012 at 05:53 AM
/api/public/snipt/52131/
restart-of-ssh-connections
<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>pstree -cpal <span class="p">|</span> egrep <span class="o">[</span>s<span class="o">]</span>shd <span class="p">|</span> perl -lne <span class="s1">&#39;($i)=(/(\d+)/); `kill -1 $i`&#39;</span> </span></pre></div> </td></tr></table>
restart, ssh, unix

Bash

find and delete files older than 5 days

1
2
3
4
5
$ find /path/to/files* -mtime +5 -exec rm {} \;

 
 
 
https://snipt.net/embed/6ba81578b80504eebccf9cbbef117b26/
https://snipt.net/raw/6ba81578b80504eebccf9cbbef117b26/
6ba81578b80504eebccf9cbbef117b26
bash
Bash
5
2016-12-08T21:07:10
True
False
False
/api/public/snipt/49835/
find-and-delete-files-older-than-5-days
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>$ find /path/to/files* -mtime +5 -exec rm <span class="o">{}</span> <span class="se">\;</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> </span></pre></div> </td></tr></table>
delete, find, linux, rm, time, ubuntu, unix

Bash

Find all .gz files and gunzip them.

find ./ -name *.gz | while read FILENAME; do gunzip $FILENAME; done
https://snipt.net/embed/7dbd82446ea8704c32f928c4c338a115/
https://snipt.net/raw/7dbd82446ea8704c32f928c4c338a115/
7dbd82446ea8704c32f928c4c338a115
bash
Bash
1
2016-12-04T05:32:27
True
False
True
Nov 06, 2012 at 07:33 AM
/api/public/snipt/49715/
find-all-gz-files-and-gunzip-them
<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>find ./ -name *.gz <span class="p">|</span> <span class="k">while</span> <span class="nb">read</span> FILENAME<span class="p">;</span> <span class="k">do</span> gunzip <span class="nv">$FILENAME</span><span class="p">;</span> <span class="k">done</span> </span></pre></div> </td></tr></table>
bash, unix
Copyrighted, illegal, or inappropriate content? Email [email protected].