<?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>Do Know Evil - A Blog by Tyler Mulligan &#187; Bash</title> <atom:link href="http://www.doknowevil.net/tag/bash/feed/" rel="self" type="application/rss+xml" /><link>http://www.doknowevil.net</link> <description>Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like</description> <lastBuildDate>Sat, 16 Jul 2011 01:25:35 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.5</generator> <item><title>Tips for Using Bash in the Linux Terminal &#8211; Part 1</title><link>http://www.doknowevil.net/2010/10/23/tips-for-using-bash-in-the-linux-terminal-part-1/</link> <comments>http://www.doknowevil.net/2010/10/23/tips-for-using-bash-in-the-linux-terminal-part-1/#comments</comments> <pubDate>Sat, 23 Oct 2010 10:13:05 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Computers]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[OSX]]></category> <category><![CDATA[Operating Systems]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[cli]]></category> <category><![CDATA[shortcuts]]></category> <category><![CDATA[terminal]]></category> <category><![CDATA[tips]]></category><guid isPermaLink="false">http://www.doknowevil.net/?p=687</guid> <description><![CDATA[Introduction Bash is the default shell in the terminal on many Linux and UNIX based operating system, such as Ubuntu or Mac OS X. I&#8217;ve mentioned commandlinefu.com before as a great reference for learning some neat tricks with the terminal. I&#8217;ve gained a lot from the site and a few others, such as the Advanced]]></description> <content:encoded><![CDATA[<h2>Introduction</h2><p>Bash is the default shell in the terminal on many Linux and UNIX based operating system, such as Ubuntu or Mac OS X.  I&#8217;ve mentioned <a href="http://www.commandlinefu.com" title="learn to be a terminal master">commandlinefu.com</a> before as a great reference for learning some neat tricks with the terminal.  I&#8217;ve gained a lot from the site and a few others, such as the <a href="http://tldp.org/LDP/abs/html/" target="_blank">Advanced Bash Scripting Guide</a> and <a href="http://wiki.bash-hackers.org" target="_blank">the bash hackers wiki</a>.  I wanted to share some of the tips I use most often, combined with other information that I&#8217;ve compiled through my use of the terminal.</p><p>I think I should mention that I&#8217;ve been using more applications in the terminal recently.  Some people view this as backwards but I argue just the opposite.  Limiting the amount of times I need to use the mouse and the number of keystrokes I need to make drastically increases my efficiency.  Many bash applications are designed around single keystrokes, layered hotkeys and edit &#8220;modes&#8221;.  GUIs have their place but many of my tasks can be completely more accurately and consistently via the terminal.</p><p>With that said, lets dive in.</p><h2>The Basics</h2><h3>Hotkeys</h3><p>Moving:<br /> <b>Ctrl + a</b> -> Go to the beginning of the line you are currently typing on.<br /> <b>Ctrl + e</b> -> Go to the end of the line you are currently typing on<br /> <b>Alt + f</b> -> Move cursor forward one word on the current line.<br /> <b>Alt + b</b> -> Move cursor backward one word on the current line.</p><p>Editing:<br /> <b>Ctrl + u</b> -> Clears from before the cursor position. If you are at the end of the line, clears the entire line.<br /> <b>Ctrl + k</b> -> Clear from after the cursor. If you are at the beginning of the line, clears the entire line.<br /> <b>Ctrl + w</b> -> Delete the word before the cursor.<br /> <b>Ctrl + h</b> -> Same as backspace.<br /> <b>Ctrl + t</b> -> Swap the last two characters before the cursor.<br /> <b>Esc + t</b> -> Swap the last two words before the cursor.</p><p>Other:<br /> <b>Ctrl + l</b> -> Clear screen (same as clear command).<br /> <b>Ctrl + c</b> -> Kill the current command or process.<br /> <b>Ctrl + z</b> -> Puts whatever you are running into a suspended background process, fg to restore it.<br /> <b>Ctrl + d</b> -> Exit the current shell.</p><p>Oddly, unlike many terminal applications, Bash hotkeys don&#8217;t make a lot of sense to me.  There are very few that are &#8220;intuitive&#8221;.</p><h3>History</h3><p>Press the <b>up arrow for the last command</b> or:</p><p><b>!!</b> &#8212; repeat last command</p><pre class="brush:bash">echo &quot;hello&quot;
!!
</pre><p>Outputs:</p><pre class="brush:bash">z@zentury:~$ echo &quot;hello&quot;
hello
z@zentury:~$ !!
echo &quot;hello&quot;
hello</pre><p><b>ctrl+r</b> is one of the best ways to search through your history.  it will initialize a reverse search as you type.  To go to the next result, press ctrl+r again</p><h2>Advanced</h2><h3>History Expansion/Modification</h3><p>!:0 &#8212; will repeat the first token</p><pre class="brush:bash">cd ~
ls -la
!:0
</pre><p>!:1-3 &#8212; defining a range: 1-3 will repeat the 2nd to 4th tokens (count starts at 0).  It&#8217;s important to note that double quotes will group tokens together.</p><pre class="brush:bash">echo &quot;hello there&quot; &amp;&amp; ls ~
!:3-4</pre><p>!!:s/find/replace/ &#8212; will allow you to replace a part of the command</p><pre class="brush:bash">echo &quot;hello there&quot;
!!:s/hello/hi/</pre><p>Outputs:</p><pre class="brush:bash">z@zentury:~$ echo &quot;hello there&quot;
hello there
z@zentury:~$ !!:s/hello/hi/
echo &quot;hi there&quot;
hi there
</pre><p><i>OR even shorter:</i></p><p>^find^replace</p><pre class="brush:bash">echo &quot;hello there&quot;
^hello^hi</pre><p>Outputs:</p><pre class="brush:bash">z@zentury:~$ echo &quot;hello there&quot;
hello there
z@zentury:~$ ^hello^hi
echo &quot;hi there&quot;
hi there
</pre><h3>Sequences and Pattern Expansion</h3><p>Typically in a Linux or UNIX environment you have access to a command line tool name &#8220;seq&#8221; which <a href="http://www.doknowevil.net/2009/08/15/generating-sequences-of-numbers-or-characters-with-bash/" target="_blank">gone over before</a>.  However, it&#8217;s good to know that bash has built-in sequence expansion and you don&#8217;t need to rely on seq.</p><pre class="brush:bash">echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z</pre><p>by defining a start and end character with the &#8216;..&#8217; in between, we tell bash to fill in the rest and echo a list for us.  Those are all lowercase, what if you wanted uppercase? simple:</p><pre class="brush:bash">echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z</pre><p>Or both, with a few extra characters in the mix:</p><pre class="brush:bash">echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z</pre><p>It doesn&#8217;t always have to be a-z though,</p><pre class="brush:bash">echo {A..G}
A B C D E F G</pre><p>This also works with numbers:</p><pre class="brush:bash">echo {0..9}
0 1 2 3 4 5 6 7 8 9
echo {0..100}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</pre><p>Descending as well as ascending</p><pre class="brush:bash">echo {9..0}
9 8 7 6 5 4 3 2 1 0</pre><p>Echo a specific set</p><pre class="brush:bash">echo {1,4,6,9}
1 4 6 9</pre><p>Applying it</p><h4>Quickly backup a file</h4><pre class="brush:bash">touch file1.txt
cp file1.txt{,.bak}
ls
file1.txt file1.txt.bak</pre><p>explanation: the first parameter is empty, the second is .bak, this expands to >> cp file1.txt file1.txt.bak << and creates the copy</p><h4>Convert an image type</h4><p>If you have image magick installed, you can convert file types pretty easy using this same concept:</p><pre class="brush:bash">sudo apt-get install imagemagick</pre><p>(To install on Ubuntu)</p><pre class="brush:bash">convert file.{jpg,png}</pre><h4>Permutations</h4><pre class="brush:bash">echo {a..c}{a..c}{a..c}
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc</pre><h2>Stay tuned for Part 2</h2><p>These are some pretty common techniques I use to reduce the amount of typing and thinking required to complete a task in the terminal.  Stay tuned for part 2 and <a href="http://www.commandlinefu.com/commands/by/zed" target="_blank">check out some of my creative usages at commandlinefu.com</a>.<br /> -</p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2010/10/23/tips-for-using-bash-in-the-linux-terminal-part-1/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Random cow(ish) animals preaching quotes on Ubuntu 9.10</title><link>http://www.doknowevil.net/2010/02/03/random-cowish-animals-preaching-quotes-on-ubuntu-910/</link> <comments>http://www.doknowevil.net/2010/02/03/random-cowish-animals-preaching-quotes-on-ubuntu-910/#comments</comments> <pubDate>Thu, 04 Feb 2010 01:02:01 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[cowsay]]></category> <category><![CDATA[fortune]]></category> <category><![CDATA[ssh]]></category><guid isPermaLink="false">http://www.doknowevil.net/?p=521</guid> <description><![CDATA[Looking for something interesting when I login to one of my servers, I decided to whip up the following script I appended to my ~/.bashrc file. # fortune and cowsay are needed for the snippet to work, I had to install these first sudo apt-get install fortune cowsay COWDIR=/usr/share/cowsay/cows/; COWNUM=$(($RANDOM%$(ls $COWDIR &#124; wc -l))); COWFILE=$(ls]]></description> <content:encoded><![CDATA[<p>Looking for something interesting when I login to one of my servers, I decided to whip up the following script I appended to my ~/.bashrc file.</p><pre class="brush:bash"># fortune and cowsay are needed for the snippet to work, I had to install these first
sudo apt-get install fortune cowsay</pre><pre class="brush:bash">COWDIR=/usr/share/cowsay/cows/; COWNUM=$(($RANDOM%$(ls $COWDIR | wc -l))); COWFILE=$(ls $COWDIR | sed -n &#039;&#039;$COWNUM&#039;p&#039;); fortune | cowsay -f $COWFILE</pre><p>UPDATE:</p><p>Suggested by MrBougo, a shorter but perhaps more process intensive method:</p><pre class="brush:bash">fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf | head -n1)</pre><div id="attachment_523" class="wp-caption alignnone" style="width: 1342px"><a href="http://www.doknowevil.net/wp-content/uploads/2010/02/screenshot1.png"><img src="http://www.doknowevil.net/wp-content/uploads/2010/02/screenshot1.png" alt="random cowsay fortune" title="random cowsay fortune" width="1332" height="850" class="size-full wp-image-523" /></a><p class="wp-caption-text">random cowsay fortune</p></div><p>Breaking down the script, the first 3 parts create variables and the last command executes the cowsay and quote.</p><pre class="brush:bash">
# defines the directory of the cow files
COWDIR=/usr/share/cowsay/cows/;

# Get a random number limited to the number of files in the directory, making clever use of % (mod) and adding 1 to make sure it doesn&#039;t return 0
COWNUM=$(($RANDOM%$(ls $COWDIR | wc -l))+1);

# list the contents of the cow dir again, pipe to sed and use the number as a random line to get the name of a file
COWFILE=$(ls $COWDIR | sed -n &#039;&#039;$COWNUM&#039;p&#039;);

# use fortune to get a quote, pipe to cowsay and use the file as defined above
fortune | cowsay -f $COWFILE;</pre>]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2010/02/03/random-cowish-animals-preaching-quotes-on-ubuntu-910/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Finding the difference in time between the first and last file in a folder using bash</title><link>http://www.doknowevil.net/2009/08/22/finding-the-difference-in-time-between-the-first-and-last-file-in-a-folder-using-bash/</link> <comments>http://www.doknowevil.net/2009/08/22/finding-the-difference-in-time-between-the-first-and-last-file-in-a-folder-using-bash/#comments</comments> <pubDate>Sat, 22 Aug 2009 15:40:58 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Computers]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://www.doknowevil.net/?p=440</guid> <description><![CDATA[I was working on running some statistics on log files and it required me to figure out the difference to increase the accuracy. I came up with the following bash script: #!/bin/bash # get the dates start_date=$(date --utc --date &#34;$(ls -Rt --full-time &#124; tail -n1 &#124; awk &#039;{ print $6 }&#039;)&#34; +%s) end_date=$(date --utc --date]]></description> <content:encoded><![CDATA[<p>I was working on running some statistics on log files and it required me to figure out the difference to increase the accuracy.  I came up with the following bash script:</p><pre class="brush:bash">#!/bin/bash
# get the dates
start_date=$(date --utc --date &quot;$(ls -Rt --full-time | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s)
end_date=$(date --utc --date &quot;$(ls -Rt --full-time | head -n2 | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s)

# find the difference
difference=$((end_date-start_date))

# echo results
echo $end_date - $start_date = $difference seconds
echo $((difference/86400)) days</pre><p>Which I originally wrote as a one liner:</p><pre class="brush:bash">start_date=$(date --utc --date &quot;$(ls -Rt --full-time | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s); end_date=$(date --utc --date &quot;$(ls -Rt --full-time | head -n2 | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s); difference=$((end_date-start_date)); echo $end_date - $start_date = $difference seconds; echo $((difference/86400)) days;</pre><p>I got a little carried away and created this beast, which still isn&#8217;t as accurate as I need it to be but it did give me some information:</p><pre class="brush:bash">map_1=nordiccastle;map_2=dance;start_date=$(date --utc --date &quot;$(ls -Rt --full-time | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s); end_date=$(date --utc --date &quot;$(ls -Rt --full-time | head -n2 | tail -n1 | awk &#039;{ print $6 }&#039;)&quot; +%s); difference=$((end_date-start_date)); echo $... Read Moreend_date - $start_date = $difference seconds; echo logs for $((difference/86400)) days; map_1_ended=$(find -name *00*.log | xargs egrep -A 4 &quot;endmatch|timelimit -1&quot; |grep $map_1 |wc -l); map_1_played=$(find -name *00*.log | xargs egrep &quot;gamestart&quot; |grep $map_1 |wc -l); echo $map_1 endmatched $map_1_ended out of $map_1_played times played; map_2_ended=$(find -name *00*.log | xargs egrep -A 4 &quot;endmatch|timelimit -1&quot; |grep $map_2 |wc -l); map_2_played=$(find -name *00*.log | xargs egrep &quot;gamestart&quot; |grep $map_2 |wc -l); echo $map_2 endmatched $map_2_ended out of $map_2_played times played</pre><p>It was used to see how many times a map was played and how many times it was voted to end the match.</p><p>It should really be a separate script to allow for more organization</p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2009/08/22/finding-the-difference-in-time-between-the-first-and-last-file-in-a-folder-using-bash/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Generating sequences of numbers or characters with bash</title><link>http://www.doknowevil.net/2009/08/15/generating-sequences-of-numbers-or-characters-with-bash/</link> <comments>http://www.doknowevil.net/2009/08/15/generating-sequences-of-numbers-or-characters-with-bash/#comments</comments> <pubDate>Sat, 15 Aug 2009 15:14:07 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Computers]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[seq]]></category><guid isPermaLink="false">http://www.doknowevil.net/?p=442</guid> <description><![CDATA[If you ever needed to generate a sequence of characters or numbers, the terminal (using bash) is a quick and easy way to do it. Lets explore some examples bash&#8217;s brace expansion: $ echo {a..z} a b c d e f g h i j k l m n o p q r s t]]></description> <content:encoded><![CDATA[<p>If you ever needed to generate a sequence of characters or numbers, the terminal (using bash) is a quick and easy way to do it.  Lets explore some examples bash&#8217;s brace expansion:</p><pre class="brush:bash">$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z</pre><p>by defining a start and end character with the &#8216;..&#8217; in between, we tell bash to fill in the rest and echo a list for us.  Those are all lowercase, what if you wanted uppercase? simple:</p><pre class="brush:bash">$ echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z</pre><p>Or both, with a few extra characters in the mix:</p><pre class="brush:bash">$ echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z</pre><p>It doesn&#8217;t always have to be a-z though,</p><pre class="brush:bash">$ echo {A..G}
A B C D E F G</pre><p>This also works with numbers:</p><pre class="brush:bash">$ echo {0..9}
0 1 2 3 4 5 6 7 8 9
echo {0..100}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</pre><p>Descending as well as ascending</p><pre class="brush:bash">$ echo {9..0}
9 8 7 6 5 4 3 2 1 0</pre><p>There is another method to generate a sequence of numbers from the command line, rightfully called &#8216;seq&#8217;</p><pre class="brush:bash">$ seq 1 5
1
2
3
4
5</pre><p>The difference here is that it&#8217;s delimited by a new line, however, we can override that with the -s (seperator) flag</p><pre class="brush:bash">$ seq -s &quot; &quot; 1 10
1 2 3 4 5 6 7 8 9 10</pre>]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2009/08/15/generating-sequences-of-numbers-or-characters-with-bash/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Finding files and strings using the terminal in Linux</title><link>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/</link> <comments>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/#comments</comments> <pubDate>Wed, 04 Feb 2009 22:06:43 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Bash]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[find]]></category> <category><![CDATA[grep]]></category> <category><![CDATA[locate]]></category> <category><![CDATA[search]]></category><guid isPermaLink="false">http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/</guid> <description><![CDATA[My favorite thing about Linux is the terminal. I use it countless times a day to do all sorts of tasks, like managing game servers or writing scripts to do tedious tasks. One of the most popular things I do in the terminal is search for files or strings inside of files and today I&#8217;d]]></description> <content:encoded><![CDATA[<p>My favorite thing about Linux is the terminal.  I use it countless times a day to do all sorts of tasks, like <a href="http://github.com/z/nst/" title="Nexuiz Server Toolz" target="_blank">managing game servers</a> or <a href="http://pics.nexuizninjaz.com/files/nk1g9e0z5i1jxlgwiszw.png" title="revision map packages with bash">writing scripts to do tedious tasks</a>.  One of the most popular things I do in the terminal is search for files or strings inside of files and today I&#8217;d like to go over a few methods and tricks for doing this.  There are three tools that make this task amazingly easy but combining them is where we find the real power.  These three tools are locate, find and grep.  I will cover the basic use of these tools with some examples and tricks but I suggest you take a look at the man pages for the tools for addition information (i.e. man locate).</p><h3>locate</h3><p>Locate has got to be the most straight forward way to search for files.  It uses a database so it&#8217;s blazing fast when searching your entire computer, compared to the &#8216;find&#8217; we&#8217;ll cover later.  It&#8217;s not available by default on all Linux distributions (it is on Ubuntu) so you might have to install it.  Since I&#8217;ll be mentioning fstab later, I&#8217;ll give my examples with it.</p><p>Basic syntax is: locate [filename]</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~
/usr/include/fstab.h
/usr/lib/udev/migrate-fstab-to-uuid.sh
/usr/share/apps/katepart/syntax/fstab.xml
/usr/share/doc/m4/examples/fstab.m4
/usr/share/doc/mount/examples/fstab
/usr/share/doc/util-linux/examples/fstab.example2
/usr/share/man/man5/fstab.5.gz
/usr/share/pysdm/fstab.py
/usr/share/pysdm/fstab.pyc</pre><p>This database will automatically update itself at various times but to force an update type the following command:</p><pre class="brush:bash">tyler@quadjutsu:~$ sudo updatedb</pre><p>Folder exclusions can be found (and edited) in /etc/updatedb.conf</p><p>By default (at least in ubuntu) /media (your mounted media) is not included in this database.  This means if you use extra drives you&#8217;ve added to your computer and you want them to be searchable through the locate tool, you&#8217;ll have to mount them to a directory like /mnt.</p><p>To mount a drive on boot, you&#8217;ll need to add a line like the following to your /etc/fstab.  The one below mounts an ntfs drive to /mnt/mydrive.  That folder must exist for the drive to be mounted.</p><pre class="brush:bash">/dev/sda1 /mnt/mydrive ntfs-3g defaults,locale=en_US.utf8 0 0</pre><p>I found the /dev/sda1 part by listing my harddrive partitions using the following command:</p><pre class="brush:bash">sudo fdisk -l</pre><h3>find</h3><p>Find is a little more cryptic than locate but it&#8217;s a very powerful tool that should be available on most Linux distributions if not all.</p><p>Basic syntax is: find [directory] -name &#8220;[string]&#8221; -print</p><p>Find will search recursively through the folder you&#8217;re calling it from.  The directory isn&#8217;t required but it will show you the full path.  A neat trick is to use $(pwd) which creates a string for the &#8220;present working directory&#8221;.  It&#8217;s also a good habit to use quotes around your name search because you can&#8217;t do regular expressions without it.  find does not do matching the same way locate does.  You&#8217;ll have to specific a wild card (*) for partial searches.</p><pre class="brush:bash">tyler@quadjutsu:~/Desktop$ find -name &quot;notes&quot; -print
tyler@quadjutsu:~/Desktop$ find -name &quot;notes*&quot; -print
./notes.txt~
./notes.txt
tyler@quadjutsu:~/Desktop$ find $(pwd) -name &quot;notes*&quot; -print
/home/tyler/Desktop/notes.txt~
/home/tyler/Desktop/notes.txt
tyler@quadjutsu:~/Desktop$ find -name &quot;no[a-z]*&quot; -print
./notes.txt~
./notes.txt</pre><p>My Buddy From Belgium, MrBougo has asked I make note that -iname makes it case insensitive.</p><h3>grep</h3><p>In the context of searching, grep is like a pocket knife.  It&#8217;s great for limiting returned results and searching through files, which are my two most common uses of it, though I&#8217;m sure there are a million others.  First I&#8217;ll cover the searching files for strings portion and in combined tools we&#8217;ll discuss how to refine search results.</p><p>In terms of searching strings in files, basic syntax is: grep &#8220;[string]&#8221; [filename]</p><p>grep is case sensitive but can be changed to insensitive with -i.  You can access extended regular expressions (which allow for such functions as use of the + sign to signify 1 or more characters) by calling &#8220;egrep&#8221;</p><pre class="brush:bash">tyler@quadjutsu:~$ grep &quot;tyler&quot; resume.txt
tyler@detrition.net
tyler@quadjutsu:~$ grep &quot;Tyler&quot; resume.txt
Tyler J. Mulligan
tyler@quadjutsu:~$ grep -i &quot;tyler&quot; resume.txt
Tyler J. Mulligan
tyler@detrition.net
tyler@quadjutsu:~$ grep -i &quot;tyl&quot; resume.txt
Tyler J. Mulligan
tyler@detrition.net
tyler@quadjutsu:~$ egrep -i &quot;tyler [a-z.]+&quot; resume.txt
Tyler J. Mulligan
tyler@quadjutsu:~$ egrep &quot;tyler [a-z.]+&quot; resume.txt</pre><p>MrBougo also mentioned that, grep returns a 1 exit status if it doesnt find, so grep &#8220;foo&#8221; &#038;&#038; grep &#8220;bar&#8221; will only grep for bar when foo is found.</p><h3>Combining the tools</h3><p>There are two very important techniques for linking together commands in the terminal.  The pipe | and &#038;&#038;.  The pipe passes the output of one command to the next, the &#038;&#038; runs a command after the one before is complete.</p><p>Starting off slow, we&#8217;ll refine our search of grep with another grep.  Note that you don&#8217;t have to use the quotes as I&#8217;ve shown below.</p><pre class="brush:bash">
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt
    * Web Developer and Interaction Designer for Nexuiz / Alientrap
    * Owner and Creator of Nexuiz Ninjaz
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt |grep -i ninjaz
    * Owner and Creator of Nexuiz Ninjaz</pre><p>Not that you would really want to do the follow commands but to emphasize how | and &#038;&#038; work, the following example shows how you&#8217;re running the command twice rather than refining a search:</p><pre class="brush:bash">
tyler@quadjutsu:~$ grep -i &quot;nexuiz&quot; resume.txt &amp;&amp; grep -i &quot;ninjaz&quot; resume.txt
    * Web Developer and Interaction Designer for Nexuiz / Alientrap
    * Owner and Creator of Nexuiz Ninjaz
    * Owner and Creator of Nexuiz Ninjaz
</pre><p>Recalling our first example with fstab, there was a lot of extra results we didn&#8217;t want.  We know know it was in the etc folder, so we can filter our results by that.</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep etc
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~
</pre><p>Another way would be to EXCLUDE folders we don&#8217;t want with the -v flag.</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep -v usr
/etc/fstab
/etc/fstab.orig
/etc/fstab.pre-ntfs-config
/etc/fstab~</pre><p>And filtering out the junk like so:</p><pre class="brush:bash">tyler@quadjutsu:~$ locate fstab |grep etc |grep -v &quot;~&quot; |grep -v &quot;\.&quot;
/etc/fstab</pre><p>~ has a special meaning in the terminal so you must quote it to match it.  ~ refers to the user&#8217;s home directory, in this case /home/tyler.  I&#8217;ve also had to escape . because it too has special meaning, in the context of regular expressions, it will match any character&#8230; if we&#8217;re excluding any character, we&#8217;re excluding our results :).</p><h3>Quick Tricks</h3><p>Remember commands is sometimes a hard thing to do, especially when it&#8217;s a whole string of complicated commands and regexes.  Sometimes, I know I don&#8217;t have the mind to take notes but that&#8217;s okay (for a grace period) because bash keeps track for me.  The history command will list all your recently executed commands and utilizing the grep command, we can filter that list.</p><pre class="brush:bash">tyler@quadjutsu:~$ history |grep locate
  613  locate fstab
  614  locate fstab |grep etc
  624  locate fstab |grep etc |grep -v &quot;~&quot; |grep -v &quot;\.&quot;
</pre><p>A quick way to search for files in a directory would be to use locate as the path and grep the results</p><pre class="brush:bash">tyler@quadjutsu:~$ locate ~ |grep notes.txt
/home/tyler/Desktop/notes.txt
</pre><p>These are the basics of finding files and searching through them.  Linux provides many tools to reformat and replace strings.  There are a million different ways to combine commands and always a faster way to do it.  Keep playing and you&#8217;ll just get better and better.</p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2009/02/04/finding-files-and-strings-using-the-terminal-in-linux/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Ubuntu Window Management with Multiple Monitors, Window Effects and Default File Associations</title><link>http://www.doknowevil.net/2008/11/25/ubuntu-window-management-with-multiple-monitors-window-effects-and-default-file-associations/</link> <comments>http://www.doknowevil.net/2008/11/25/ubuntu-window-management-with-multiple-monitors-window-effects-and-default-file-associations/#comments</comments> <pubDate>Wed, 26 Nov 2008 02:25:38 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Application Management]]></category> <category><![CDATA[Bash]]></category> <category><![CDATA[Desktop Mods]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[open source]]></category> <category><![CDATA[dual screen]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[multiple monitors]]></category><guid isPermaLink="false">http://www.doknowevil.net/2008/11/25/ubuntu-window-management-with-multiple-monitors-window-effects-and-default-file-associations/</guid> <description><![CDATA[Multiple Monitors Window Management in Ubuntu Moving your applications from one monitor to the next with hotkeys I&#8217;ve been using multiple monitors for a while now, Starting with a 17&#8243; CRT with a 19&#8243; CRT and moving up to two 19&#8243; LCD, then 3, then temporarily one wide screen and back to 2. Something I]]></description> <content:encoded><![CDATA[<h3>Multiple Monitors Window Management in Ubuntu</h3><h4>Moving your applications from one monitor to the next with hotkeys</h4><p>I&#8217;ve been using multiple monitors for a while now, Starting with a 17&#8243; CRT with a 19&#8243; CRT and moving up to two 19&#8243; LCD, then 3, then temporarily one wide screen and back to 2.  Something I always loved having as a utility in <a href="http://www.realtimesoft.com/ultramon/" title="Ultramon - multiple monitors in Windows" target="_blank">ultramon</a> that I couldn&#8217;t find in Ubuntu (Gnome) or any window manager for that matter, was the ability to move applications from monitor to monitor.  I had assumed the search futile until I was searching about some questions I had concerning <a ref="http://compiz-fusion.org/" title="Compiz-fusion - Window Effects in Linux" target="_blank">Compiz-fusion</a> with dual monitors and I came about <a href="http://ubuntuforums.org/showthread.php?t=815925" title="Multiple monitor window handling in Linux">this thread on Ubuntu forums</a> which brightened up my day. A fellow named gfixler posted a bash script that utilizes command line applications to move the windows.</p><p>For you multi-monitor users seeking salvation from removing your hand from the keyboard to move your application from one monitor to another, here&#8217;s the skinny on getting it setup using compiz-fusion, aka Advanced Desktop Effects, to set my keybinds.</p><p>1. Open a terminal and setup your prerequisites with apt-get:</p><pre class="brush:bash">sudo apt-get install wmctrl xprop xwininfo</pre><p>If you get errors about x11-utils, just ignore them, this package will handle your needs.</p><p>2. Next, lets put the script somewhere you can call it, say &#8220;<b>~/scripts</b>&#8221;</p><pre class="brush:bash">mkdir ~/scripts &amp;&amp; cd ~/scripts &amp;&amp; touch movewin.sh &amp;&amp; chmod +x movewin.sh &amp;&amp; gedit movewin.sh</pre><p>2. Paste the following code, find the first function &#8220;<b>getNumberOfMonitors</b>&#8221; and configure it to the number of monitors you have (default 2).</p><div style="height:300px;overflow:auto;padding:2px;"><pre class="brush:bash">
#!/bin/bash
# swap_monitor.sh (original version)
# Moves the active window to the other screen of a dual-screen Xinerama setup.
#
# movewin.sh (modified version)
# allows movement of windows left and right between multiple monitors
#
# Requires: wmctrl, xprop, xwininfo
#
# Original Author: Raphael Wimmer
# raphman@gmx.de
#
# Modified by: Gary Fixler
# gfixler+bash@gmail.com

function getNumberOfMonitors
{
    # simply must be hardcoded
    # e.g. MatroxTripleHead2Go can service 3 screens,
    # but appears as only one monitor to the computer

    # change to your number of monitors
    echo 2
}

function getMonitorWidth
{
    numberOfMonitors=$(getNumberOfMonitors)
    monitorLine=$(xwininfo -root | grep &quot;Width&quot;)
    monitorWidth=$((${monitorLine:8}/$numberOfMonitors ))
    echo $monitorWidth
}

function getActiveWindowID
{
    activeWinLine=$(xprop -root | grep &quot;_NET_ACTIVE_WINDOW(WINDOW)&quot;)
    activeWinID=&quot;${activeWinLine:40}&quot;
    echo $activeWinID
}

function getActiveWindowHorizontalPosition
{
    activeWinID=$(getActiveWindowID)
    xPosLine=$(xwininfo -id $activeWinID | grep &quot;Absolute upper-left X&quot;)
    xPos=${xPosLine:25}
    echo $xPos
}

function getActiveWindowWidth
{
    activeWinID=$(getActiveWindowID)
    xWidthLine=$(xwininfo -id $activeWinID | grep &quot;Width&quot;)
    xWidth=${xWidthLine:8}
    echo $xWidth
}

function getActiveWindowCurrentMonitor
{
    numberOfMonitors=$(getNumberOfMonitors)
    monitorWidth=$(getMonitorWidth)
    activeWinID=$(getActiveWindowID)
    xPos=$(getActiveWindowHorizontalPosition)
    i=&quot;0&quot;
    while [ $xPos -gt $monitorWidth ]
    do
        xPos=$[$xPos-$monitorWidth]
        i=$[$i+1]
    done
    echo $i
}

function getActiveWindowPositionOneMonitorToTheLeft
{
    monitorWidth=$(getMonitorWidth)
    currentMonitor=$(getActiveWindowCurrentMonitor)
    activeWinID=$(getActiveWindowID)
    xPos=$(getActiveWindowHorizontalPosition)
    xPos=$[$xPos-$monitorWidth]
    echo $xPos
}

function getActiveWindowPositionOneMonitorToTheRight
{
    monitorWidth=$(getMonitorWidth)
    numberOfMonitors=$(getNumberOfMonitors)
    currentMonitor=$(getActiveWindowCurrentMonitor)
    activeWinID=$(getActiveWindowID)
    xPos=$(getActiveWindowHorizontalPosition)
    xPos=$[$xPos+$monitorWidth]
    echo $xPos
}

function changeActiveWindowMonitor
{
    activeWinID=$(getActiveWindowID)
    if [ $1 -eq &quot;0&quot; ]
    then
        newXPos=$(getActiveWindowPositionOneMonitorToTheLeft)
        newXPos=$[$newXPos-5]
    else
        newXPos=$(getActiveWindowPositionOneMonitorToTheRight)
        newXPos=$[$newXPos-5]
    fi

    winState=$(xprop -id ${activeWinID} | grep &quot;_NET_WM_STATE(ATOM)&quot; )

    if [[ `echo ${winState} | grep &quot;_NET_WM_STATE_MAXIMIZED_HORZ&quot;` != &quot;&quot; ]]
        then
        maxH=1
        wmctrl -i -r ${activeWinID} -b remove,maximized_horz
    fi

    if [[ `echo ${winState} | grep &quot;_NET_WM_STATE_MAXIMIZED_VERT&quot;` != &quot;&quot; ]]
        then
        maxV=1
        wmctrl -i -r ${activeWinID} -b remove,maximized_vert
    fi

    if [[ `echo ${winState} | grep &quot;_NET_WM_STATE_FULLSCREEN&quot;` != &quot;&quot; ]]
        then
        fulls=1
        wmctrl -i -r ${activeWinID} -b remove,fullscreen
    fi

    # move window (finally)
    wmctrl -i -r ${activeWinID} -e 0,${newXPos},-1,-1,-1

    # restore maximization
    ((${maxV})) &amp;&amp; wmctrl -i -r ${activeWinID} -b add,maximized_vert
    ((${maxH})) &amp;&amp; wmctrl -i -r ${activeWinID} -b add,maximized_horz
    ((${fulls})) &amp;&amp; wmctrl -i -r ${activeWinID} -b add,fullscreen

    # raise window (seems to be necessary sometimes)
    wmctrl -i -a ${activeWinID}

}

function moveActiveWindowOneMonitorToTheLeft
{
    changeActiveWindowMonitor 0
}

function moveActiveWindowOneMonitorToTheRight
{
    changeActiveWindowMonitor 1
}

&quot;$1&quot;

exit 0
</pre></div><p>3. Setup your hot keys with compiz-fusion.  Go to System >> Preferences >> Advanced Desktop Effects.  Inside &#8220;<b>General Options</b>&#8220;, click on the command tab (I apologize for my heinous blue links).</p><p><a href='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-205445_833x464_scrot.png' title='Compiz-fusion Hotkeys 1'><img src='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-205445_833x464_scrot.png' alt='Compiz-fusion Hotkeys 1' /></a></p><p><a href='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-210327_900x435_scrot.png' title='2008-11-25-210327_900x435_scrot.png'><img src='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-210327_900x435_scrot.png' alt='2008-11-25-210327_900x435_scrot.png' /></a></p><p>Use</p><pre class="brush:bash">scripts/./movewin.sh moveActiveWindowOneMonitorToTheRight</pre><p>and</p><pre class="brush:bash">scripts/./movewin.sh moveActiveWindowOneMonitorToTheLeft</pre><p>respectively</p><h3>Per Application Window Effects in Ubuntu</h3><h4>Bring character and tickle your soul with per application window effects</h4><p>Another cool feature Compiz-fusion has is window animations.  My friend <a href="http://www.realhumanmoments" title="James Lindsay" target="_blank">James Lindsay</a> recently reminded me about Window Effects&#8230; which when I first install Ubuntu on my laptop, I experimented my butt off&#8230; but being a laptop&#8230; I just used simple ones I&#8217;d turn off half the time anyway.  He asked me why I don&#8217;t use them on my desktop and I didn&#8217;t have a good reason.  Well, now I have 2 good reasons to keep using compiz.</p><p><a href='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-210749_900x591_scrot.png' title='2008-11-25-210749_900x591_scrot.png'><img src='http://www.doknowevil.net/wp-content/uploads/2008/11/2008-11-25-210749_900x591_scrot.png' alt='2008-11-25-210749_900x591_scrot.png' /></a></p><p>I made my Thunderbird use the airplane effect so when I send emails, it flys away and for <a href="http://www.geany.org" title="Geany - My Linux Text Editor of Choice" target="_blank">Geany</a>, I used the magic lamp for open, close, maximize and minimize (different speeds).  It&#8217;s a fun little effect that breaks up the stiffness of the desktop.</p><h3>Default File Associations in Ubuntu</h3><h4>geany > gedit</h4><p>I was tired of gedit popping up when geany&#8217;s just as lightweight but more affective.  So found a command and altered it a bit to make my default editor geany.</p><p>1. Open the terminal and create\open the following file:</p><pre class="brush:bash">gedit ~/.local/share/applications/defaults.list</pre><p>If it&#8217;s blank, add &#8220;<b>[Default Applications]</b>&#8220;.  If it&#8217;s not, find &#8220;<b>[Default Applications]</b>&#8220;.</p><p>2. Then, back to the terminal, grep the default files associations and replace gedit with your editor of choice</p><pre class="brush:bash">grep gedit /usr/share/applications/defaults.list | sed s/gedit/geany/g</pre><p>Copy (ctrl+shift+c) and paste the output into gedit, below the &#8220;<b>[Default Applications]</b>&#8221; header.</p><p>3. Restart nautilus to load the changes (will close all your file managers that are open and blink/freeze your desktop for a second)</p><pre class="brush:bash">pkill nautilus</pre><p>Good luck, have fun and happy coding :)</p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2008/11/25/ubuntu-window-management-with-multiple-monitors-window-effects-and-default-file-associations/feed/</wfw:commentRss> <slash:comments>2</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)
Object Caching 668/705 objects using disk

Served from: www.doknowevil.net @ 2012-02-07 08:00:03 -->
