<title>How to rickroll people that try to run "rm -rf" on your system</title>
<metaname="description"content="WARNING: The method showed here could not prevent the actual execution of “rm -rf” if the “UNIX vandal” is clever enough. Proceed at your own risk, and make ...">
WARNING: The method showed here could not prevent the actual execution of “rm -rf” if the “UNIX vandal” is clever enough. Proceed at your own risk, and make backups!
<p>I like rickrolling people myself too, especially if they’re trying to delete my entire <codeclass="highlighter-rouge">/home</code> directory or, even worse, <codeclass="highlighter-rouge">/</code>. Since I learned how to use the <codeclass="highlighter-rouge">alias</code> built-in, I wanted a way to prevent that random people tinkering with my laptop (that I may forgot to lock) could delete potentially important stuff, just for fun or boredom.</p>
<p>The method that I’ll show will lock any <codeclass="highlighter-rouge">rm</code> command runned in both recursive and force mode, so <codeclass="highlighter-rouge">rm -rf</code>, <codeclass="highlighter-rouge">rm -f -r</code> and <codeclass="highlighter-rouge">rm -r --force</code> are all blocked, even if they are launched by <codeclass="highlighter-rouge">sudo</code>. I’m going to alias the rm command in <codeclass="highlighter-rouge">/etc/profile</code><codeclass="highlighter-rouge">/etc/bash.bashrc</code> and in <codeclass="highlighter-rouge">/etc/zsh/zshrc</code> (I’m a zsh user) so that the rickroll will be possible from all users, even root and the ones with a brand new <codeclass="highlighter-rouge">bashrc</code> or <codeclass="highlighter-rouge">zshrc</code>. Here is the code I appended to those files:</p>
<spanclass="nb">alias sudo</span><spanclass="o">=</span><spanclass="s1">'sudo '</span><spanclass="c"># this enables aliases in sudo, see http://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo</span></code></pre></figure>
<p>Since <codeclass="highlighter-rouge">alias</code> is not able to control the flags of the aliases (see <ahref="http://apple.stackexchange.com/questions/50963/how-do-i-add-a-flag-to-an-alias">here</a>), we’re going to redirect each call of <codeclass="highlighter-rouge">rm</code> to <codeclass="highlighter-rouge">/bin/rmAlias</code>, that would run the command if it’s safe. I didn’t use a function because it’s a bit tricky to make that work with <codeclass="highlighter-rouge">sudo</code>. So, let’s see the code I put in <codeclass="highlighter-rouge">rmAlias</code>:</p>
<spanclass="c"># Rickroll whoever tries to desert this system, even root.</span>
<spanclass="c"># To achieve this, set the appropriate aliases even in /etc/profile and similars.</span>
<spanclass="c"># Video played when rickrolling</span>
<spanclass="nv">ROLLVIDEO</span><spanclass="o">=</span>/opt/anti-rm/serious-video.mkv <spanclass="c"># it's just Never Gonna Give You Up on my system, but be free to customize this!</span>
rickroll<spanclass="o">(){</span>
<spanclass="nb">echo</span><spanclass="s2">"Never gonna desert this system..."</span>
xdg-open <spanclass="nv">$ROLLVIDEO</span> 2>&1 &
<p>It may look messy to a UNIX guy more experienced than me, but it works. The <codeclass="highlighter-rouge">getopts</code> built-in sees if both the <codeclass="highlighter-rouge">-r</code> and the <codeclass="highlighter-rouge">-f</code> flags are used and, if so, it starts <codeclass="highlighter-rouge">rickroll()</code>, which opens with <codeclass="highlighter-rouge">xdg-open</code> that amazing clip from RickAstleyVEVO. From line 30 and below, the script checks if the <codeclass="highlighter-rouge">--force</code> flag is used instead of <codeclass="highlighter-rouge">-f</code>.</p>
<p>Give execute permissions to the script we’ve just created:</p>
<p>Restart your shell, and enjoy. If you want to test safely, I suggest trying to run <codeclass="highlighter-rouge">rm -rf</code> with no folders or one nonexistant, since this script stop even these commands.</p>
<p>If you want even more security, you can rename this script to <codeclass="highlighter-rouge">/bin/rm</code> and move the original one in some other place, getting rid of all the aliases. I prefer the solution above because it’s tidier: you haven’t to move anything. In fact, this could be just an AUR package…</p>