<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lyle Backenroth &#187; Command Line</title>
	<atom:link href="http://www.lylebackenroth.com/blog/tag/command-line/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lylebackenroth.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 01:07:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Command Line Magic: Part III</title>
		<link>http://www.lylebackenroth.com/blog/2010/03/20/command-line-magic-part-iii/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=command-line-magic-part-iii</link>
		<comments>http://www.lylebackenroth.com/blog/2010/03/20/command-line-magic-part-iii/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 08:55:09 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=526</guid>
		<description><![CDATA[As part of my continuing Command Line Magic series and many of the other Command Line oriented posts I&#8217;ve made (click here for category-summary of Command Line oriented posts, or just click the Command Line tag in the tag cloud to the right), I&#8217;m happy to post another set of highly useful commands. As always, the context [...]]]></description>
			<content:encoded><![CDATA[<p>As part of my continuing Command Line Magic series and many of the other Command Line oriented posts I&#8217;ve made (<a href="http://www.lylebackenroth.com/blog/tag/command-line/" target="_blank">click here for category-summary of Command Line oriented posts</a>, or just click the Command Line tag in the tag cloud to the right), I&#8217;m happy to post another set of highly useful commands. As always, the context of these commands are within the <a href="http://en.wikipedia.org/wiki/Bash_(Unix_shell)" target="_blank">Bash shell</a> in Linux. A moderate understanding of Bash shell commands is required to fully appreciate this post.</p>
<p>Here are some very useful commands, that any power user would find helpful:</p>
<p><strong><span style="color: #ff9900;">1. Start a simple webserver to serve up any directory as browsable from anywhere (for file transfers):</span></strong></p>
<pre>$ <strong>python -m SimpleHTTPServer</strong></pre>
<p>I&#8217;ve mentioned this in past posts. This is a simple command, that when run from any directory will launch a simple python web server that will serve up the local directory as a browsable directory using a browser such as Firefox or Chrome. Any subdirectories underneath the local directory from which this command is run will also be browsable. You can right-click and save any file or left-click it to attempt to view it on the fly. This works very well over SSH sessions, when you want to transfer a file, but don&#8217;t want to engage <a href="http://www.lylebackenroth.com/blog/sshfs/" target="_blank">SSHFS</a> or <a href="http://en.wikipedia.org/wiki/Secure_copy" target="_blank">SCP</a>. You can background the process with a <strong>ctrl-z</strong>, <strong>bg</strong>, then <strong>pkill python</strong> to stop the web server from running, or just leave it running in command prompt and <strong>ctrl-c</strong> to end it.</p>
<p><strong><span style="color: #ff9900;">2. Record your desktop and pipe the output to an mpeg file.</span></strong></p>
<pre>$ <strong>ffmpeg -f x11grab -s wsxga -r 25 -i :0.0 -sameq /home/john/desktop.mpg</strong></pre>
<ul>
<li><strong>-f</strong> allows ffmpeg to grab the data properly from the x11 framebuffer</li>
<li><strong>-s</strong> sets the size of the screen to actually record, starting from the upper left of the screen. Here wsxga denotes a specific preset resolution (in wsxga&#8217;s case that would be 1600 x 1024). You can however type any resolution you like in manually (e.g. <strong>-s 1024&#215;768</strong>). You will need to know the resolution of your desktop to set this correctly.</li>
<li><strong>-r</strong> sets the framerate. This could be left out as 25 is the default.</li>
<li><strong>-i</strong> sets which framebuffer to take, since XWindows can run in multiple sessions, generally you&#8217;ll want to leave this setting alone.</li>
<li><strong>-sameq</strong> forces the same quality was what is being fed in by the source (in this case the x11 framebuffer). This is helpful to have a max-quality video, though you may want to try other settings to degrade the quality to keep the file size down. If you&#8217;d prefer to reduce the quality on the fly, <span style="color: #ff9900;"><strong>replace </strong></span><span style="color: #ff9900;"><strong>-sameq</strong></span><span style="color: #ff9900;"><strong> with </strong></span><span style="color: #ff9900;"><strong>-qscale x</strong></span> where <strong>x</strong> is <strong>1</strong> &#8211; <strong>31</strong>. These are preset quality settings, with <strong>1</strong> being the highest and <strong>31</strong> being very poor video quality. I have found <strong>-qscale 10</strong> to be the sweetspot between quality and file size.</li>
<li>If you&#8217;d like the file to be a bit smaller and if you prefer an .AVI to a raw .MPG, then simply remove the <strong>/home/john/desktop.mpg</strong> in the command above and replace it with:
<ul>
<li><strong>-vcodec mpeg4 /home/john/desktop.avi</strong>
<ul>
<li>This is file will be a bit smaller using the mpeg4 codec in an avi container. You can still use the <strong>-qscale</strong> option with this change.</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><span style="color: #ff9900;"><strong>3. Copy an entire directory tree through ssh using on the fly compression through an SSH session (no temporary files!):</strong></span></p>
<pre>$ <strong>ssh &lt;host&gt; 'tar -cz /&lt;directory&gt;/&lt;subdirectory&gt;' | tar -xvz</strong></pre>
<p>Just enter the &lt;host&gt; to SSH to, and the host&#8217;s &lt;directory&gt; and &lt;subdirectory&gt; path to compress that subdirectory on the fly at the host, but decompress it as it arrives locally to your current location and path. This will have the advantage of not taking up any extra space at the host (since the files are compressed as they&#8217;re transmitted) and easily drops the entire directory tree specified onto the client uncompressed, saving time and bandwidth and transmission time.</p>
<p>This works well for large directory trees and is easy to use for a quick copy where you don&#8217;t want to spend a lot of time compressing it at the host manually and transmitting the compressed file, then uncompressing it, then deleting the original compressed file created at the host. Note: This will replicate the full directory path at the client side (desired).</p>
<p>SCP or RSYNC are recommended for automated backup though, this is more appropriate for a 1-shot copy of a large directory.</p>
<p><strong><span style="color: #ff9900;">4. Resize any image files in the current directory to Width x Height specifed (regardless of image format)!</span></strong></p>
<pre>$ <strong>for a in `ls`; do echo $a &amp;&amp; convert $a -resize &lt;Width&gt;x&lt;Height&gt; $a; done</strong></pre>
<p>Simply do a <strong>man convert</strong> to learn more about the convert program, other options can be added into the command. Also this is a great syntax for doing ANYTHING to any files in a particular directory that would be a batch process consistent with all the files in that directory.</p>
<p><span style="color: #ff9900;"><strong>5. Grab a screenshot of the current desktop to the current directory</strong></span></p>
<pre>$ <strong>import -pause 5 -window root desktop_screenshot.jpg</strong></pre>
<p>This command will wait 5 seconds (assuming you want some time to set up the shot and to get the command prompt out of the way) and take a snapshot of the root (primary) desktop currently running. This command requires <strong>imagemagick</strong> be installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2010/03/20/command-line-magic-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screen Profiles available Ubuntu 9.04</title>
		<link>http://www.lylebackenroth.com/blog/2009/04/28/screen-profiles-that-available-ubuntu-904/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=screen-profiles-that-available-ubuntu-904</link>
		<comments>http://www.lylebackenroth.com/blog/2009/04/28/screen-profiles-that-available-ubuntu-904/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 17:58:06 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Screen]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=358</guid>
		<description><![CDATA[Interesting article about .screenrc profiles that offer some unique status bars with GNU Screen under Ubuntu 9.04
For any that are interested, this is my .screenrc
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
#
# Default screens
# syntax: screen -t NameOfScreen ScreenNumber ShellCommand
screen -t "Work SSH" 0 /home/name/workssh
screen -t "Home 1" 1
screen -t "Home 2" [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://arstechnica.com/open-source/news/2009/04/ubuntu-brings-advanced-screen-features-to-the-masses.ars" target="_blank">Interesting article about .screenrc profiles that offer some unique status bars</a> with GNU Screen under Ubuntu 9.04</p>
<p>For any that are interested, this is my .screenrc</p>
<pre>hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
#
# Default screens
# syntax: screen -t NameOfScreen ScreenNumber ShellCommand
screen -t "Work SSH" 0 /home/name/workssh
screen -t "Home 1" 1
screen -t "Home 2" 2
screen -t "Home 3" 3
screen -t "Home 4" 4</pre>
<p>The hardstatus string gives me the local computer&#8217;s hostname on the left, lists the shells themselves in the middle, and on the right offers the date and time.</p>
<p>Line 6 with &#8220;Work SSH&#8221; executes the shell script &#8220;workssh&#8221;, labels the window &#8220;Work SSH&#8221; and assigns it to virtual shell number 0. This shell script allows me to SSH to one of my work servers offering me a series of local listening ports that forward to remote IP&#8217;s and ports.</p>
<p>Shells 1-4 are local bash shells.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2009/04/28/screen-profiles-that-available-ubuntu-904/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux Reference Sheets</title>
		<link>http://www.lylebackenroth.com/blog/2008/08/29/linux-reference-sheets/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=linux-reference-sheets</link>
		<comments>http://www.lylebackenroth.com/blog/2008/08/29/linux-reference-sheets/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 06:21:06 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Cheat Sheets]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=138</guid>
		<description><![CDATA[The 1 page Linux CommandManual
Linux Command Reference
Linux Security Quick Reference
The Gimp Hotkey Reference
]]></description>
			<content:encoded><![CDATA[<p align="left"><a href="http://www.lylebackenroth.com/blog/post-images/TheOnePageLinuxManual.pdf" target="_blank">The 1 page Linux CommandManual</a></p>
<p align="left"><a href="http://www.lylebackenroth.com/blog/post-images/LinuxCmdReference.pdf" target="_blank">Linux Command Reference</a></p>
<p align="left"><a href="http://www.lylebackenroth.com/blog/post-images/LinuxSecurityQuickReferenceGuide.pdf" target="_blank">Linux Security Quick Reference</a></p>
<p align="left"><a href="http://www.lylebackenroth.com/blog/post-images/gimp-reference.pdf" target="_blank">The Gimp Hotkey Reference</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/08/29/linux-reference-sheets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Line Magic: Part II</title>
		<link>http://www.lylebackenroth.com/blog/2008/07/27/command-line-magic-part-ii/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=command-line-magic-part-ii</link>
		<comments>http://www.lylebackenroth.com/blog/2008/07/27/command-line-magic-part-ii/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 21:14:03 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=130</guid>
		<description><![CDATA[Fun with rename:
1) To rename filenames with CAPS characters to lowercase:
rename &#8216;y/A-Z/a-z/&#8217; *
2) To remove &#60;spaces&#62; from filenames:
rename &#8217;s/ /-/g&#8217; *
3) To strip files with the .bak extension:
rename &#8217;s/\.bak$//&#8217; *.bak

Free: The free command displays the amount of free and used memory in the system. For example, free -m gives the information using megabytes, which is [...]]]></description>
			<content:encoded><![CDATA[<p>Fun with <strong>rename:</strong></p>
<p>1) To rename filenames with CAPS characters to lowercase:</p>
<blockquote><p><strong>rename &#8216;y/A-Z/a-z/&#8217; *</strong></p></blockquote>
<p>2) To remove &lt;spaces&gt; from filenames:</p>
<blockquote><p><strong>rename &#8217;s/ /-/g&#8217; *</strong></p></blockquote>
<p>3) To strip files with the .bak extension:</p>
<blockquote><p><strong>rename &#8217;s/\.bak$//&#8217; *.bak</strong><br />
<strong></strong></p></blockquote>
<p><strong>Free: </strong>The<span style="font-family: Courier,Monospace;"> free</span> command displays the amount of free and used memory in the system. For example, <span style="font-family: Courier,Monospace;">free -m</span> gives the information using megabytes, which is probably most useful for current computers.</p>
<p><strong>lsb_release -a:</strong> The <span style="font-family: Courier,Monospace;">lsb_release</span> command with the <span style="font-family: Courier,Monospace;">-a</span> option prints version information for the Linux release you&#8217;re running</p>
<p><strong>sed: </strong>The <span style="font-family: Courier,Monospace;">sed</span> (or Stream EDitor) command allows search and replace of a particular string in a file. For example, if you want to find the string &#8220;cat&#8221; and replace it with &#8220;dog&#8221; in a file named pets, type<br />
<span style="font-family: Courier,Monospace;">sed s/cat/dog/g pets</span>.</p>
<p><strong>grep:</strong> <a href="http://www.panix.com/%7Eelflord/unix/grep.html" target="_blank">An excellent tutorial</a>.</p>
<p><strong>Running commands sequentially in one command:</strong><br />
If you need to execute multiple commands in sequence but don&#8217;t need to pass output between them, there are two options based on whether or not you want the subsequent commands to run only if the previous commands succeed or not. If you want the commands to run one after the other regardless of whether or not preceding commands succeed, place a <span style="font-family: Courier,Monospace;">;</span> between the commands.</p>
<p>For example, if you want to get information about your hardware, you could run <strong><span style="font-family: Courier,Monospace;">lspci ; lsusb</span></strong> which would output information on your PCI buses and USB devices in sequence.</p>
<p>However, if you need to conditionally run the commands based on whether the previous command has succeeded, insert <span style="font-family: Courier,Monospace;">&amp;&amp;</span> between commands. An example of this is building a program from source, which is traditionally done with <em><span style="font-family: Courier,Monospace;">./configure, make,</span> and <span style="font-family: Courier,Monospace;">make install</span></em>.</p>
<p>The commands <span style="font-family: Courier,Monospace;">make</span> and <span style="font-family: Courier,Monospace;">make install</span> require that the previous commands have completed successfully, so you would use <strong><span style="font-family: Courier,Monospace;">./configure &amp;&amp; make &amp;&amp; sudo make install</span></strong>. Usually you&#8217;ll need an SUDO, for the last command, so make sure you&#8217;ve authenticated SUDO prior to running that command.</p>
<p><strong>Learning the Linux Shell:</strong> <a href="http://linuxcommand.org/learning_the_shell.php" target="_blank">A great tutorial</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/07/27/command-line-magic-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Essential tips of Linux Admins</title>
		<link>http://www.lylebackenroth.com/blog/2008/07/27/10-essential-tips-of-linux-admins/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=10-essential-tips-of-linux-admins</link>
		<comments>http://www.lylebackenroth.com/blog/2008/07/27/10-essential-tips-of-linux-admins/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 06:24:21 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=127</guid>
		<description><![CDATA[
Tips from IBM, not all that complicated, but worth looking at.
Other tips include&#8230;

Sharing computers on a Linux (or heterogeneous) network.
IBM&#8217;s top 10 Linux articles and tutorials.
Miscellaneous tips.
Linux-powered networking: Part 1, Part 2 and Part 3.
Industrial-strength Linux box lockdown, a howto: Part 1 and Part 2.
 Introduction to cryptology concepts: Part 1, Part 2 and Part [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" src="http://www.lylebackenroth.com/blog/post-images/ibm.png" alt="" /></p>
<p style="text-align: center;"><a href="http://www.ibm.com/developerworks/linux/library/l-10sysadtips/index.html?ca=drs-tp3008" target="_blank">Tips from IBM</a>, not all that complicated, but worth looking at.</p>
<p>Other tips include&#8230;</p>
<ul>
<li><a href="http://www.ibm.com/developerworks/linux/library/l-share1.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Sharing computers on a Linux (or heterogeneous) network.</a></li>
<li><a href="http://www.ibm.com/developerworks/linux/library/l-top-10.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">IBM&#8217;s top 10 Linux articles and tutorials.</a></li>
<li><a href="http://www.ibm.com/developerworks/views/linux/libraryview.jsp?topic_by=All+topics+and+related+products&amp;sort_order=desc&amp;lcl_sort_order=desc&amp;search_by=linux+tip%3A&amp;search_flag=true&amp;type_by=All+Types&amp;show_abstract=true&amp;start_no=1&amp;sort_by=Date&amp;end_no=100&amp;show_all=false&amp;S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Miscellaneous tips.</a></li>
<li>Linux-powered networking: <a href="http://www.ibm.com/developerworks/edu/l-dw-linux-lpndns-i.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Part 1</a>, <a href="http://www.ibm.com/developerworks/edu/l-dw-linux-lpndhcp-i.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Part 2</a> and <a href="http://www.ibm.com/developerworks/edu/l-dw-linux-lpnsamba-i.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Part 3</a>.</li>
<li>Industrial-strength Linux box lockdown, a howto: <a href="http://www.ibm.com/developerworks/edu/l-dw-linux-lockdown1-i.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Part 1</a> and <a href="http://www.ibm.com/developerworks/edu/l-dw-linux-lockdown2-i.html?S_TACT=105AGX03&amp;S_CMP=ART" target="_blank">Part 2</a>.</li>
<li> Introduction to cryptology concepts: <a href="http://www.ibm.com/developerworks/edu/s-dw-scrypto-i.html" target="_blank">Part 1</a>, <a href="http://www.ibm.com/developerworks/edu/s-dw-sucrypt2-i.html" target="_blank">Part 2</a> and <a href="http://www.ibm.com/developerworks/edu/s-dw-scrypt3-i.html" target="_blank">Part 3</a>.</li>
</ul>
<p><a href="https://www.ibm.com/account/profile/us?page=reg&amp;okURL=https%3A%2F%2Fwww14.software.ibm.com%2Fwebapp%2Fiwm%2Fweb%2Freg%2Fsignup.do%3Fsource%3Ddw-linux-lockdown1%26S_TACT%3D105AGX03%26S_CMP%3DART%26lang%3Den_US&amp;template=lightperson" target="_blank"><strong>Note: Some of these links requires registration.</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/07/27/10-essential-tips-of-linux-admins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Downloading torrents from command line</title>
		<link>http://www.lylebackenroth.com/blog/2008/07/27/downloading-torrents-from-command-line/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=downloading-torrents-from-command-line</link>
		<comments>http://www.lylebackenroth.com/blog/2008/07/27/downloading-torrents-from-command-line/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 06:11:55 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=126</guid>
		<description><![CDATA[Source: LinuxHaxor.net
Essentially RTorrent, CTorrent and Transmission-CLI.
]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><strong>Source: <a href="http://www.linuxhaxor.net/2008/05/11/downloading-torrent-from-the-terminal/" target="_blank">LinuxHaxor.net</a></strong></p>
<p style="text-align: left;">Essentially <a href="http://libtorrent.rakshasa.no/" target="_blank">RTorrent</a>, <a href="http://ctorrent.sourceforge.net/" target="_blank">CTorrent</a> and <a href="http://packages.debian.org/unstable/net/transmission-cli" target="_blank">Transmission-CLI</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/07/27/downloading-torrents-from-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CLI Magic</title>
		<link>http://www.lylebackenroth.com/blog/2008/07/23/cli-magic/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=cli-magic</link>
		<comments>http://www.lylebackenroth.com/blog/2008/07/23/cli-magic/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 00:13:38 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=125</guid>
		<description><![CDATA[Source for this great article: Linux.com
In this context, a one-liner is a set of commands normally joined through a pipe (&#124;). When joined by a pipe, the command on the left passes its output to the command on the right. Simple or complex, you can get useful results from a single line at the bash [...]]]></description>
			<content:encoded><![CDATA[<h3 style="text-align: center;"><strong>Source for this great article: <a href="http://www.linux.com/feature/141921" target="_blank">Linux.com</a></strong></h3>
<div class="xar-clearleft">In this context, a one-liner is a set of commands normally joined through a pipe (|). When joined by a pipe, the command on the left passes its output to the command on the right. Simple or complex, you can get useful results from a single line at the bash command prompt.</div>
<div id="featurecontent" class="xar-align-left">For example, suppose you want to know how many files are in the current directory. You can run:</p>
<pre>
<div class="code">ls | wc -l</div>
</pre>
<p>That&#8217;s a very simple example &#8212; you can get more elaborate. Suppose you want to know about the five processes that are consuming the most CPU time on your system:</p>
<pre>
<div class="code">ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6</div>
</pre>
<p>The <code>ps</code> command&#8217;s <code>o</code> lets you specify the columns that you want to be shown. <code>sort -r</code> does a reverse order sort with the second column (pcpu) as reference (<code>k2</code>). <code>head</code> gets only the first six lines from the ordered list, which includes the header line. You can place pcpu as the first column and then omit the <code>k2</code> option because <code>sort</code> by default takes the first column to do the sort. That illustrates how you may have to try several approaches on some one-liners; different versions and ways to manipulate the options may produce different results.  A common situation for Linux administrators on servers with several users is to get quick ordered user lists. One simple way to get that is with the command:</p>
<pre>
<div class="code">cat /etc/passwd | sort</div>
</pre>
<p>If you just need the username, the above command returns too much information. You can fix it with something like this:</p>
<pre>
<div class="code">cat /etc/passwd | sort | cut -d":" -f1</div>
</pre>
<p>The sorted list is passed to <code>cut</code>, where the <code>d</code> option indicates the field&#8217;s delimiter character. <code>cut</code> breaks into pieces each line, and the first field <code>f1</code> is the one that you need to display. That&#8217;s better; it shows only usernames now. But you may not want to see all the system usernames, like apache, bin, and lp. If you just want human users, try this:</p>
<pre>
<div class="code">cat /etc/passwd | sort | gawk '$3 &gt;= 500 {print $1 }' FS=":"</div>
</pre>
<p><code>gawk</code> evaluates each line from the output piped to it. If the third field &#8212; the UID &#8212; is equal or greater than 500 (most modern distros start numbering normal users from this number) then the action is done. The action, indicated between braces, is to print the first field, which is the username. The separator for field in the gawk command is a colon, as specified by the <code>FS</code> option.  Now suppose you have a directory with lots of files with different extensions, and you want to back up only the .php files, calling them <em>filename</em>.bkp. The next one-liner should do the job:</p>
<pre>
<div class="code">for f in *.php; do cp $f $f.bkp; done</div>
</pre>
<p>This command loops through all the files in the current directory looking for those with .php extensions. Each file&#8217;s name is held in the <code>$f</code> variable. A simple copy command then does the backup. Notice that in this example we used a semicolon to execute the commands one after another, rather than piping output between them.  What about bulk copy? Consider this:</p>
<pre>
<div class="code">tar cf - . | (cd /usr/backups/; tar xfp -)</div>
</pre>
<p>It creates a tar package recursevely on the current directory, then pipes this package to the next command. The parenthesis creates a temporary subshell, changes to a different directory, then extracts the content of the package, which is the whole original directory. The <code>p</code> option on the last <code>tar</code> command preserves file properties like time and permissions. After completion, the shell context will be at the original directory.  A variant on the previous one-liner lets you do the same kind of backup on a remote server:</p>
<pre>
<div class="code">tar cf - . | ssh smith@remote.server tar xfp - -C /usr/backup/smith</div>
</pre>
<p>Here, the command establishes an SSH remote session and untars the package with the <code>C</code> option, which changes the directory, in this case to /usr/backup/smith, where the extraction will be made.</p>
<h4>grep and gawk and uniq, oh my!</h4>
<p>Text processing is a common use for one-liners. You can accomplish marvelous things with the right set of commands. In the next example, suppose you want a report on incoming email messages that look like this:</p>
<pre>
<div class="code">
cat incoming_emails
2008-07-01 08:23:17 user1@example.com
2008-07-01 08:25:20 user2@someplace.com
2008-07-01 08:32:41 somebody@server.net
2008-07-01 08:35:03 spam not recived, filtered
2008-07-01 08:39:57 user1@example.com
...</div>
</pre>
<p>You are asked for a report with an ordered list of who received incoming messages. Many recipients would be repeated in the output of the <code>cat</code> command. This one-liner resolves the problem:</p>
<pre>
<div class="code">grep '@' incoming_email | gawk '{print $3}' | sort | uniq</div>
</pre>
<p><code>grep</code> filters the lines that contains a @ character, which indicates an email address. Next, <code>gawk</code> extracts the third field, which contains the email address, and passes it to the sort command. Sorting is needed to group the same recipients together because the last command, <code>uniq</code>, omits repeated lines from the sorted list. The output is shown below. Most text processing one-liners use a combination of <code>grep</code>, <code>sed</code>, <code>awk</code>, <code>order</code>, <code>tr</code>, <code>cut</code>, <code>uniq</code>, and other related commands.</p>
<pre>
<div class="code">
somebody@server.net
user1@example.com
user2@someplace.com</div>
</pre>
<p>If you like any of these one-liners but think they&#8217;re too long to type often, you can create an alias for the command and put it in your .bashrc file. When you log in your session, anything inside this file will be run, so your personal aliases would be ready at anytime.</p>
<pre>
<div class="code">alias p5="ps -eo pcpu,user,pid,cmd | sort -r | head -6"</div>
</pre>
<p>You can certainly create better and simpler variations of all of the commands in this article, but they&#8217;re a good place to start. If you are a Linux system administrator, it&#8217;s good practice to collect, create, and modify your own one-liners and keep them handy; you never know when are you going to need them. If you have a good one-liner, feel free to share it with other readers in a comment below.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/07/23/cli-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful less-used Linux commands</title>
		<link>http://www.lylebackenroth.com/blog/2008/07/20/useful-less-used-linux-commands/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=useful-less-used-linux-commands</link>
		<comments>http://www.lylebackenroth.com/blog/2008/07/20/useful-less-used-linux-commands/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 22:15:59 +0000</pubDate>
		<dc:creator>lyle</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.lylebackenroth.com/blog/?p=122</guid>
		<description><![CDATA[1. lsb_release -a
2. The watch command.
3. Command line intro summary, with some rarely used switches (TLDP).
4. If you&#8217;re using a system which has a lot of users, and you&#8217;d like to see who has started a particular script, daemon, or binary, then the pstree utility is very helpful.  It draws a tree of all [...]]]></description>
			<content:encoded><![CDATA[<p>1. lsb_release -a</p>
<p>2. The <a href="http://www.debian-administration.org/articles/605" target="_blank">watch</a> command.</p>
<p>3. <a href="http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/GNU-Linux-Tools-Summary.html" target="_blank">Command line intro summary</a>, with some rarely used switches (<a href="http://tldp.org/" target="_blank">TLDP</a>).</p>
<p>4. If you&#8217;re using a system which has a lot of users, and you&#8217;d like to see who has started a particular script, daemon, or binary, then the <strong><tt>pstree</tt></strong> utility is very helpful.  It draws a tree of all currently running processes &#8211; allowing you to see which processes are related.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lylebackenroth.com/blog/2008/07/20/useful-less-used-linux-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)

Served from: lylebackenroth.com @ 2010-09-08 00:57:54 -->