<?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>blog.tordeu.com &#187; Bash</title>
	<atom:link href="http://blog.tordeu.com/?cat=53&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.tordeu.com</link>
	<description>a blog about stuff</description>
	<lastBuildDate>Thu, 25 Apr 2013 07:03:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>Changing PATH to include /sbin or: How to fix &#8220;bash: ldconfig: command not found&#8221;</title>
		<link>http://blog.tordeu.com/?p=374</link>
		<comments>http://blog.tordeu.com/?p=374#comments</comments>
		<pubDate>Fri, 08 Jun 2012 00:23:51 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[PATH]]></category>
		<category><![CDATA[profile]]></category>
		<category><![CDATA[sbin]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=374</guid>
		<description><![CDATA[It&#8217;s been a while, but in the oh so distant past I stumbled upon the following error on my then fairly new Debian Wheezy system: bash: ldconfig: command not found You might encounter this error or something very similar, especially when you are using sudo instead of changing to root with su (see my article <a href='http://blog.tordeu.com/?p=374'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been a while, but in the oh so distant past I stumbled upon the following error on my then fairly new Debian Wheezy system:</p>
<pre>bash: ldconfig: command not found</pre>
<p>You might encounter this error or something very similar, especially when you are using sudo instead of changing to root with su (<a title="How to enable sudo in Linux (here: Debian Squeeze)?" href="http://blog.tordeu.com/?p=31">see my article on how to enable sudo</a>).</p>
<p>&nbsp;</p>
<h2>Problem</h2>
<p>The problem is that programs like ldconfig are located in the  /sbin folder, but this folder is not added to the PATH variable.</p>
<p>If you don&#8217;t know what PATH is:</p>
<p>The path variable is essentially a list of folders. When you type a command in bash (the terminal/command line) like this:</p>
<pre>ldconfig</pre>
<p>then bash will go through all the folders listed in the PATH variable and see if a program with that name is in that folder. If it finds that program, it will execute it.</p>
<p>The problem here is that PATH (the list of directories where bash will look for the program) does not contain the folder /sbin. But because the program is in this folder and bash does not look there, bash will not find the program and instead annoy you with an error.</p>
<p>&nbsp;</p>
<h2>Solution</h2>
<p>The solution is to add  &#8220;/sbin&#8221; to the PATH variable. This can be done by editing the file &#8220;/etc/profile&#8221;.</p>
<p>Type</p>
<pre>sudo nano /etc/profile</pre>
<p>or &#8211; if you are more comfortable with a GUI based text editor &#8211; you can also type</p>
<pre>sudo gedit /etc/profile</pre>
<p>&nbsp;</p>
<p>At the beginning of the file, you will see the following:</p>
<pre>if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi</pre>
<p>This sets the PATH variable for the root user and every other user. The first line that starts with &#8220;PATH=&#8230;&#8221; sets the PATH for the root user and the second line sets PATH for everybody else. So the code fragment basically translates to:</p>
<pre>if user=root then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
otherwise
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"</pre>
<p>The second line is what we have to care right now. The directories in the list are separated by a colon, so the list contains the following directories:</p>
<ul>
<li>
<pre>/usr/local/bin</pre>
</li>
<li>
<pre>/usr/bin</pre>
</li>
<li>
<pre>/bin</pre>
</li>
<li>
<pre>/usr/local/games</pre>
</li>
<li>
<pre>/usr/games</pre>
</li>
</ul>
<p>We want to add a few directories to that, namely:</p>
<ul>
<li>
<pre>/usr/local/sbin</pre>
</li>
<li>
<pre>/usr/sbin</pre>
</li>
<li>
<pre>/sbin</pre>
</li>
</ul>
<p>For ldconfig and a lot of other programs, adding &#8220;/sbin&#8221; would be enough, but I added the others as well, because I thought: &#8220;I don&#8217;t wanna come back two more times, just because I might realize later that I should have added them as well in the first place&#8221;</p>
<p>You could add the directories anywhere in the list, but I like to add them in the same order they appear in the PATH variable for the root user, so I will change the beginning of the file to this (remember to separate the directories with a colon):</p>
<pre>if user=root then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
otherwise
  PATH="<span style="color: #ff0000;">/usr/local/sbin:</span>/usr/local/bin:<span style="color: #ff0000;">/usr/sbin:</span>/usr/bin:<span style="color: #ff0000;">/sbin:</span>/bin:/usr/local/games:/usr/games"
fi</pre>
<p>Save the file; when using nano, just</p>
<ul>
<li>hit &#8220;Ctrl+X&#8221; to quit</li>
<li>then &#8220;Y&#8221; to confirm that you want to save the modified version</li>
<li>then hit the enter key to confirm the filename</li>
</ul>
<p>And finally: Log out and log back in.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=374</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Aliases in Bash</title>
		<link>http://blog.tordeu.com/?p=289</link>
		<comments>http://blog.tordeu.com/?p=289#comments</comments>
		<pubDate>Sun, 22 Jan 2012 12:53:56 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Alias]]></category>
		<category><![CDATA[Aliases]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=289</guid>
		<description><![CDATA[Introduction What Aliases Are Here is a short introduction to aliases in case you have not idea what they are: An alias is simply a different name for a specific command line command. You can call an alias like any other program and the shell will then replace the alias with the command it represents. <a href='http://blog.tordeu.com/?p=289'>[...]</a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<h3>What Aliases Are</h3>
<p>Here is a short introduction to aliases in case you have not idea what they are:</p>
<p>An alias is simply a different name for a specific command line command. You can call an alias like any other program and the shell will then replace the alias with the command it represents.</p>
<p>If you find yourself typing&#8221;aptitude&#8221; a lot you could, for example, define an alias &#8220;a&#8221; for this command instead. Now, instead of typing</p>
<pre>aptitude install &lt;package&gt;</pre>
<p>you just need to type</p>
<pre>a install &lt;package&gt;</pre>
<p>Bash will recognize that the command a is really just an alias that stands for &#8220;aptitude&#8221; and therefore replace a with &#8220;aptitude&#8221;. You could make your like even easier by assigning an alias &#8220;ai&#8221; to the command &#8220;aptitude install&#8221;. Then, when typing</p>
<pre>ai &lt;package&gt;</pre>
<p>bash will notice that the command &#8220;ai&#8221; is really just an alias for &#8220;aptitude install&#8221; and replace &#8220;ai&#8221; accordingly. Therefore, the command that bash will execute is:</p>
<pre>aptitude install &lt;package&gt;</pre>
<p><span id="more-289"></span></p>
<h3>Permanently Storing Your Aliases</h3>
<p>It is important to note than running the commands below will only change the alias definitions for the current shell. The changes will not appear in other shells and they will be lost when closing the shell (and logout/shutdown/reboot). This basically means that the changes are only temporary.</p>
<p>Therefore, if you want to permanently define new aliases, you can create new file named</p>
<pre>.bash_aliases</pre>
<p>in your homefolder (the file will most likely not exist already) and put all you alias definitions in there. Alternatively you can add the definitions to the file</p>
<pre>.bashrc</pre>
<p>in your home folder, but it&#8217;s cleaner to have a separate file for the aliases and the .bashrc should already check for a file named .bash_aliases, so you only need to create the .bash_aliases and the file will automatically loaded.</p>
<p>It is important to note that the .bashrc and therefore the .bash_aliases will only be read when the shell is created. Therefore, you won&#8217;t see the changes to .bash_aliases immediately. You need to either create a new shell after saving your modified .bash_aliases or (re)load the .bash_aliases file like this:</p>
<pre>. ~/.bash_aliases</pre>
<p>You can, of course, create an an alias for this command in your .bash_aliases file&#8230;</p>
<p>&nbsp;</p>
<h2>List All Aliases</h2>
<p>To get a list of all aliases which are currently defined, all you need to do is to run:</p>
<pre>alias</pre>
<p>This will list all defined aliases, one per line, like this:</p>
<pre>alias la='ls -la'
alias ls='ls --color=auto'
alias lx='ls -lAh --group-directories-first'</pre>
<p>&nbsp;</p>
<h2>List A Specific Alias</h2>
<p>To only get the value of a specific alias, you can type:</p>
<pre>alias &lt;name&gt;</pre>
<p>You can also use this command to display several aliases:</p>
<pre>alias &lt;name1&gt; &lt;name2&gt; ...</pre>
<h3>Example 1</h3>
<pre>alias la</pre>
<p>will display</p>
<pre>alias la='ls -la'</pre>
<h3>Example 2</h3>
<pre>alias ls la</pre>
<p>will display:</p>
<pre>alias ls='ls --color=auto'
alias la='ls -la'</pre>
<h2></h2>
<h2>Create A New Alias / Change An Existing Alias</h2>
<p>Creating a new alias or changing the value of an existing alias can be done in the same way:</p>
<pre>alias &lt;name&gt;=&lt;value&gt;</pre>
<p>For example, creating an alias &#8220;www&#8221; to avoid typing &#8220;cd /var/www&#8221; all the time, can be done like this:</p>
<pre>alias www='cd /var/www'</pre>
<p>&nbsp;</p>
<h3>Combining Aliases</h3>
<p>When defining an alias it is possible to use another alias. Let me show you two examples of how this might be put to use:</p>
<h4>Supplying (additional) options</h4>
<p>The only predefined alias on Debian is</p>
<pre>ls='ls --color=auto</pre>
<p>By defining an alias la like this:</p>
<pre>alias la='ls -la'</pre>
<p>You already used an alias for defining the new alias. When typing</p>
<pre>la</pre>
<p>bash will now recognize that &#8220;la&#8221; is an alias and replace it with its value:</p>
<pre>ls -la</pre>
<p>But now, again, bash will recognize that &#8220;ls&#8221; is an alias as well and replace this will its value &#8220;ls &#8211;color=auto&#8221;, which results in the command:</p>
<pre>ls --color=auto -la</pre>
<p>(bash will not recognize ls an alias again, so this is where it stops)</p>
<p>In this example the alias &#8220;la&#8221; used the already defined alias &#8220;ls&#8221; (which adds coloring to the command ls) to add the additional options &#8220;-l&#8221; and &#8220;-a&#8221; to it. This can be used for various purposes. If you want to define various aliases for the ls command, for example, it might be a good idea to create an alias that already contains the common options (like coloring) you want to use for all the other aliases. This will shorten the alias values, which makes them easier to manage. Instead of defining the following aliases:</p>
<pre>alias la='ls -la --color=auto'
alias lh='ls -lh --color=auto'
alias lx='ls -lAh --color=auto --group-directories-first'</pre>
<p>you could define them like this:</p>
<pre>alias la='ls -a'
alias lh='ls -h'
alias ls='ls -l --color=auto'
alias lx='lh -A --group-directories-first'</pre>
<p>In this example the alias ls already carried the &#8220;-l&#8221; and &#8220;&#8211;color=auto&#8221; option, therefore avoiding the others to specify them as well. The definition of the lx alias uses the lh alias, which additionally carries the &#8220;-h&#8221; option. You should be careful when using this approach, because creating two many connections between aliases can make management more complicated instead of simplifying it and also create some unwanted side effects.</p>
<p>&nbsp;</p>
<h4> Supplying (additional) arguments</h4>
<p>Similar to applying additional command line options, you can supply additional arguments to</p>
<p>&nbsp;</p>
<h2>Delete An Alias</h2>
<p>To delete an alias you need to use the unalias command:</p>
<pre>unalias &lt;name&gt;</pre>
<p>It is also possible to delete several aliases at once:</p>
<pre>unalias &lt;name1&gt; &lt;name2&gt; ...</pre>
<h3>Example</h3>
<p>To delete the www alias that we just created, type:</p>
<pre>unalias www</pre>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=289</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rename &#8211; Batch renaming files in command line</title>
		<link>http://blog.tordeu.com/?p=112</link>
		<comments>http://blog.tordeu.com/?p=112#comments</comments>
		<pubDate>Thu, 08 Dec 2011 16:05:52 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux Commands]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=112</guid>
		<description><![CDATA[&#160; There are several ways one can batch rename files in Linux. One of the possibilities is the linux command rename. Usage To rename files you have to supply a Perl expression. Using Perl expressions might be scary for beginners (because they look like someone hit his head on the keyboard), but they allow for <a href='http://blog.tordeu.com/?p=112'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>&nbsp;</p>
<p>There are several ways one can batch rename files in Linux. One of the possibilities is the linux command rename.</p>
<h2>Usage</h2>
<p>To rename files you have to supply a Perl expression. Using Perl expressions might be scary for beginners (because they look like someone hit his head on the keyboard), but they allow for complex renaming with a minimum of characters.</p>
<p>Although I won&#8217;t go into detail about Perl expressions here, I will try to make it a little easier for beginners to use them for batch renaming.</p>
<p>According to the man page of rename, this is the way to use rename:</p>
<pre>rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]</pre>
<p>If you have not worked with Perl expressions before, this might not be enough to get you started. So let me present you with the way one would probably use rename most of the time:</p>
<pre>rename 's/<span style="color: #00ccff;">OLD</span>/<span style="color: #00ccff;">NEW</span>/' <span style="color: #00ccff;">FILES</span></pre>
<p>OLD is an expressions which describes a pattern in the name filenames. If rename can find this pattern in the name of a file, it will replace this part of the filename with what is defined as NEW. The files rename should rename are defined by FILES.</p>
<p>Let me provide you with two examples:<span id="more-112"></span></p>
<h2>Examples</h2>
<h3>Example 1 &#8211; Replacing a single word/string</h3>
<p>Let&#8217;s start with a very simple example: Imagine, you have a lot of images that are named</p>
<pre>picture 1.jpg
picture 2.jpg
...</pre>
<p>and you would like to replace the word &#8220;picture&#8221; with the word &#8220;image&#8221;, so that the files will be named like this:</p>
<pre>image 1.jpg
image 2.jpg
...</pre>
<p>As describes under usage, OLD represents the part that we want to replace. As we want  to replace the word &#8220;picture&#8221;, in this example OLD will be &#8220;picture&#8221;.</p>
<p>And because NEW defines with what OLD is replaced with, in our example. We want the OLD(=&#8221;picture&#8221;) to be replaced with &#8220;image&#8221;, so here NEW=&#8221;image&#8221;.</p>
<p>And because we want to do this only for all the pictures which name starts with &#8220;picture&#8221; FILES will be &#8220;picture*.jpg&#8221;.</p>
<p>Therefore the general command</p>
<pre>rename 's/OLD/NEW/' FILES</pre>
<p>becomes:</p>
<pre>rename 's/picture/image/' picture*.jpg</pre>
<p>It might have not been necessary for us to restrict this to the the files &#8220;picture*.jpg&#8221;. We might have just used *.jpg (all files in the current directory with the extension .jpg) or even * (all files in the current directory), because rename will only change the filename, if the pattern OLD (here: &#8220;picture&#8221;) is found in the filename. You have to be careful, though. A file with the name &#8220;just a picture.jpg&#8221; would have been renamed to &#8220;just a image.jpg&#8221;, because we told rename to replace &#8220;picture&#8221; with &#8220;image&#8221;.</p>
<h3>Example 2</h3>
<p>Let&#8217;s go back to Example 1. One problem I mentioned is that running</p>
<pre>rename 's/picture/image' *.jpg</pre>
<p>to rename files like &#8220;picture 23.jpg&#8221; to &#8220;image 23.jpg&#8221; would also rename files like &#8220;just a picture.jpg&#8221; to &#8220;just a image.jpg&#8221;, because rename would find the word &#8220;picture&#8221; and replace it with &#8220;image&#8221;, although this is not what we intended.</p>
<p>We could prevent this by calling rename like this:</p>
<pre>rename 's/picture/image' picture*.jpg</pre>
<p>This would call rename only for the .jpg files starting with the word &#8220;picture&#8221;. But this is not really a solution, because files like &#8220;picture of a sunset.jpg&#8221; would still be renamed to &#8220;image of a sunset.jpg&#8221;.</p>
<p>To make sure that rename really does what we want it to do, we need to be very clear about what should be renamed. Because what we want is not to replace the word &#8220;picture&#8221; with the word &#8220;image&#8221; in every case, we want to replace the word &#8220;picture&#8221; with the word &#8220;image&#8221;, but only for the files which name consists of the word &#8220;picture&#8221;, then a space and then a number and then the extension &#8220;.jpg&#8221;.</p>
<p>This can be achieved with the following command:</p>
<pre>rename 's/^picture( [0-9]*\.jpg)$/image$1/' *.jpg</pre>
<p>This might look a little scary, but is a good example of what you can do with Perl expressions. Because this would only rename the desired files. Files like &#8220;my picture 11.jpg&#8221; or &#8220;picture 28 (beach).jpg&#8221; would not be renamed and in this example that is exactly what we want to do.</p>
<p>Let me go through the different parts of the expression above briefly, to give you an idea of how this works. Keep in mind that this just an example and not intended as a complete tutorial. I will just try to explain to you the various elements of this expression to show you some of the possibilities and to demystify them a little bit.</p>
<p>When you compare the command to the general form under &#8220;Usage&#8221;, you will see that in this case:</p>
<ul>
<li>OLD = ^picture( [0-9]*\.jpg)$</li>
<li>NEW = image$1</li>
<li>FILES = *.jpg</li>
</ul>
<p>In this example OLD makes sure that only the desired files are renamed by defining the structure of what we are looking for exactly. This works, because OLD defines what should be renamed. That means that files where the pattern/structure defined by OLD can not be found will not be renamed. Therefore, OLD automatically works as a filter to make sure that only the desired files get renamed. The trick is to make sure that OLD and NEW reflect what you wish to do. Let&#8217;s dissect it:</p>
<p>Let&#8217;s ignore the &#8220;^&#8221; at the beginning, the &#8220;$&#8221; at the end, the parentheses and the &#8220;\&#8221;  at the moment. This leaves the following:</p>
<pre>picture [0-9]*.jpg</pre>
<p>This describes our basic structure. First, we have the word &#8220;picture&#8221;, then a space. [0-9] means any character from 0 to 9, which essentially means &#8220;a digit&#8221; and when putting a &#8220;*&#8221; after it, it means that there can be &#8220;any number of&#8221; digits. We need this, because we don&#8217;t know how many digits there will be. We can have files like &#8220;picture 7.jpg&#8221;, where there is only 1 digit or files like &#8220;picture 1432.jpg&#8221;, where he have four of them. So, &#8220;[0-9]*&#8221; stands for &#8220;any number of digits&#8221;, which just means: a number. And then we have &#8220;.jpg&#8221;, our extensions.</p>
<p>It is important to note that  &#8220;[0-9]*&#8221; really means &#8220;any number of digits&#8221; and that &#8220;any number&#8221; could also mean 0. This means that the number is optional and a file named &#8220;picture .jpg&#8221; would also be renamed. In this example it probably would not make a real difference, but in general you need to be careful. If there really has to be a number and it can not be optional, you can use a &#8220;+&#8221; instead of a &#8220;*&#8221;, because &#8220;[0-9]+&#8221; means &#8220;at least one digit&#8221;.  But because it will not be a problem in this case, we will keep the &#8220;*&#8221;.</p>
<p>Now, let&#8217;s add the parts we ignored:</p>
<p>The &#8220;.&#8221; is a special character which stand for &#8220;any character&#8221;. But in our pattern, we want to make clear that the filename ends with &#8220;.jpg&#8221;. But &#8220;.jpg&#8221; would be interpreted as &#8220;any character, then j, then p, then g&#8221;. Therefore a file like &#8220;picture 234Xjpg&#8221; would be renamed, because the . (which stand for &#8220;any character&#8221;) would be &#8220;X&#8221; in this case. But really want to make sure that the file ends with &#8220;.jpg&#8221; and therefore need to make it clear that the &#8220;.&#8221; really stand for &#8220;.&#8221; and not for &#8220;any character&#8221;. This can be done by escaping it with a &#8220;\&#8221;. Therefore, we need to write &#8220;\.jpg&#8221;.</p>
<p>The parentheses are added, because the pattern we are creating will be matching the complete filename. If we would run a command like this:</p>
<pre>rename 's/^picture [0-9]*\ .jpg$/image/' *.jpg</pre>
<p>A file like &#8220;picture 23.jgp&#8221; would be renamed to &#8220;image&#8221; and not to &#8220;image 23.jpg&#8221;. This is because our pattern &#8220;^picture [0-9]*\.jpg$&#8221; describes the whole filename. So the complete filename would be replace with &#8220;image&#8221;. But we just want to replace &#8220;picture&#8221; and keep the rest.</p>
<p>We can do this by telling rename that we want to keep everything after &#8220;picture&#8221;. While we can&#8217;t do this directly, we can put every after &#8220;picture&#8221; in parentheses and then later use this part to build our new filename, because everything within the parentheses will be saved accessible later as &#8220;$1&#8243;, so if we use the pattern</p>
<pre>picture( [0-9]*\.jpg)</pre>
<p>We can now see that the space, the number and the extension &#8220;.jpg&#8221; are within the parentheses and we can access this part as &#8220;$1&#8243;. When the filename is &#8220;picture 83.jpg&#8221;, $1 will therefore contain &#8221; 83.jpg&#8221;. Then, when we define our replacement, we can tell rename to use &#8220;image$1&#8243; as our replacement for the filename. Because $1 contains the space, the number and the extension, this would translate to &#8220;image 83.jpg&#8221;.</p>
<p>Now, what&#8217;s left are &#8220;^&#8221; and &#8220;$&#8221;. &#8220;^&#8221; stand for the beginning of the filename. This makes sure that files like &#8220;some picture 44.jpg&#8221; would not be renamed. This is because a pattern like &#8220;picture&#8221; means: find the word picture anywhere. Whereas &#8220;^picture&#8221; essentially means: find the word picture at the beginning of the input(=filename).</p>
<p>Similarly, &#8220;$&#8221; stand for the end of the input (here: filename). We use this to make sure that something like &#8220;picture 74.jpg.gz&#8221; does not get renamed.</p>
<p>Combining &#8220;^&#8221; and &#8220;$&#8221; therefore ensures that the filename only consists of the pattern we defined and there is nothing before &#8220;picture&#8221; and nothing after our &#8220;.jpg&#8221;.</p>
<p>Therefore, our final pattern is:</p>
<pre>^picture( [0-9]*\.jpg)$</pre>
<p>And the command to rename the files is, again:</p>
<pre>rename 's/^picture [0-9]*\ .jpg$/image/' *.jpg</pre>
<h3>Example 4 &#8211; add a prefix to all filenames (added on 2012-05-20)</h3>
<p>In case you want to add a prefix at the beginning of a filename, you can use this line:</p>
<p>rename &#8216;s/(.*)/PREFIX$1/&#8217; FILES</p>
<p>Substitute PREFIX to whatever you want to put in front of the filename and substitute FILES for the files you want to rename (see Example 6 below for some examples)</p>
<p>If you run</p>
<p>rename &#8216;s/(.*)/foo_$1/&#8217; *.txt</p>
<p>All the .txt files in the current directory will get an &#8220;foo_&#8221; in front of of their name. (So info.txt would be renamed to foo_info.txt, for example)</p>
<p>&nbsp;</p>
<h3>Example 5 &#8211; add a suffix to all filenames (added on 2012-05-20)</h3>
<p>Adding a suffix at the end of a filename can be as simple as adding a prefix; you could simply run:</p>
<pre>rename 's/(.*)/$1SUFFIX/' FILES</pre>
<p>The problem is that this might not be what you expected, because the suffix will be added at the end of the filename, which also means after the suffix the file might already have to denote its type (e.g. &#8220;.txt&#8221;, &#8220;.tar&#8221;, &#8220;.jpg&#8221; etc. Running</p>
<pre>rename 's/(.*)/$1boo/' info.tar</pre>
<p>would rename the file &#8220;info.tar&#8221; to &#8220;info.tar.boo&#8221;. If that is what you want, you can use the line above, but if you would want to rename it to &#8220;infoboo.tar&#8221;, then you can use the following line:</p>
<pre>rename 's/(.*)\.(.*)/$1SUFFIX.$2/' FILES</pre>
<p>with this, running</p>
<pre>rename 's/(.*)\.(.*)/$1boo.$2/' FILES</pre>
<p>would rename &#8220;info.tar&#8221; to &#8220;infoboo.tar&#8221;.</p>
<p>Be aware that this will not work for files with suffixes that contain two dots (like &#8220;archive.tar.gz&#8221;).</p>
<h4>Improvement 1 (Update 2012-05-22):</h4>
<p>The fact that files like &#8220;archive.tar.gz&#8221; would be renamed as well and the result would certainly not be as hoped, we can try to improve the pattern above a little bit:</p>
<pre>rename 's/^([^.]*)\.([^.]*)$/$1SUFFIX.$2/' FILES</pre>
<p>This approach will only rename files that only contain a single dot and leave all other files alone.</p>
<h4>Improvement 2 (Update 2012-05-22):</h4>
<p>If you want to rename files with extensions like &#8220;.tar.gz&#8221; correctly, you can use the following approach:</p>
<pre>rename 's/^([^.]*)\.(.*)$/$1SUFFIX.$2/' FILES</pre>
<p>This will essentially add the suffix directly in front of the first dot. However, this approach will not work in case you have files that contain dots that are not supposed to be part of the file extension (in &#8220;some.great.file.txt&#8221;, the suffix &#8220;111&#8243; would be added like this: &#8220;some111.great.file.txt&#8221;)</p>
<h3>Example 6 &#8211; how to define which files to rename (added on 2012-05-20)</h3>
<p>In the beginning I noted the general form of how to use rename:</p>
<pre>rename 's/OLD/NEW/' FILES</pre>
<p>I now want to give you some simple examples of what you can do with &#8220;FILES&#8221;, which tells rename which files it<br />
should try to rename in the first place. This list is by no means complete. It just provides you with a few (hopefully useful) examples.</p>
<ul>
<li>to rename a single file just state its name. Example: backup.tar</li>
<li>to rename several specific files just list all their names. Example: backup.tar mypicture.jpg asong.ogg</li>
<li>to rename all files in the current directory just use &#8220;*&#8221; (without the &#8220;).</li>
<li>to rename all files with a specific ending (e.g. .jpg) use &#8220;*ENDING&#8221;. Example: *.jpg</li>
<li>to rename files with different endings, just list them all. Example: *.jpg *.jpeg *.mp3 *.txt *.ar</li>
<li>to rename files that start with the same word/characters use &#8220;START*&#8221;. Example: my* old*</li>
<li>to rename files that contain the same word/sequence of characters use &#8220;*WITHIN*&#8221;. Example: *good*</li>
<li>you can combine all of those things. Example: my*.txt *boo*.jpg foo* test.tmp</li>
</ul>
<p>&nbsp;</p>
<h2>Conclusion</h2>
<p>I hope that this article succeeds in demonstrating how rename can be used to batch rename files (not in detail, but at least in pushing you in the right direction). And I also hope that while covering Perl expressions in detail is beyond the scope of this article, the second example gives you an idea of how powerful and useful they can be and why it can make sense to learn more about them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=112</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
