<?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; Linux Commands</title>
	<atom:link href="http://blog.tordeu.com/?cat=44&#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>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>
		<item>
		<title>mkdir &#8211; Creating directories with the Linux command mkdir</title>
		<link>http://blog.tordeu.com/?p=262</link>
		<comments>http://blog.tordeu.com/?p=262#comments</comments>
		<pubDate>Thu, 01 Dec 2011 21:52:23 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Linux Commands]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[create directories]]></category>
		<category><![CDATA[create directory]]></category>
		<category><![CDATA[directories]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[mkdir]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=262</guid>
		<description><![CDATA[The Linux command mkdir (make directory/make directories) can be used to create new directories. Basic Usage Creating directories in the current working directory The most basic usage of mkdir is the following: mkdir &#60;name&#62; This will create a new directory with the given name in the current directory. Therefore, if your username is &#8220;me&#8221; and <a href='http://blog.tordeu.com/?p=262'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>The Linux command mkdir (make directory/make directories) can be used to create new directories.</p>
<h2>Basic Usage</h2>
<h3>Creating directories in the current working directory</h3>
<p>The most basic usage of mkdir is the following:</p>
<pre>mkdir &lt;name&gt;</pre>
<p>This will create a new directory with the given name in the current directory. Therefore, if your username is &#8220;me&#8221; and in the terminal you are using you are currently in your home folder (/home/me) then running the command</p>
<pre>mkdir backup</pre>
<p>will create a the directory /home/me/backup.</p>
<p><span id="more-262"></span></p>
<h3>Creating directories using relative or absolute paths</h3>
<p>You do not need to be in a directory do create a new directory in it. You can, for example, use relative or absolute  paths to create them.</p>
<p>Let&#8217;s assume you are in your home folder (/home/me) and you just created the folder backup (/home/me/backup). And now you want to create a folder named &#8220;old&#8221; in it. Then you can either call mkdir with a relative path:</p>
<pre>mkdir backup/old</pre>
<p>or with the absolute path:</p>
<pre>mkdir /home/me/backup/old</pre>
<p>&nbsp;</p>
<p>Note, however, that if you would call</p>
<pre>mkdir backup/old</pre>
<p>and the directory &#8220;backup&#8221; would not exist already in the current folder, then you would get an error.  This is, because calling mkdir like this:</p>
<pre>mkdir &lt;path&gt;/&lt;name&gt;</pre>
<p>will create the directory &lt;name&gt; in the directory &lt;path&gt; only if &lt;path&gt; already exists. Otherwise, mkdir will quit with an error message. This is the default behavior of mkdir.</p>
<p>&nbsp;</p>
<h2>Options</h2>
<p>There are a few options which can be quite useful when creating directories.</p>
<h3>-p, &#8211;parents</h3>
<p>Under &#8220;Basic Usage&#8221;, I mentioned that the path you want to create a new directory in, needs to exists or otherwise you will get an error. You can use the option &#8220;-p&#8221; or &#8220;&#8211;parents&#8221; to change that behavior. With this option, mkdir will create all the directories that don&#8217;t exists already, not just the last one. If you would call</p>
<pre>mkdir -p backup/old</pre>
<p>or</p>
<pre>mkdir --parents backup/old</pre>
<p>in your home directory and the directory &#8220;backup&#8221; would not exist already, then the -p options will tell mkdir to create this directory as well instead of quitting with an error message. This option is therefore very useful if you need to create a directory, but you are not sure if the directory you want to create it in already exists. Even if you know that this directory does not exist yet, you can use the -p option to reduce the number of necessary calls.</p>
<p>If you know that there currently is no backup (or are not sure) and you want to create the folder backup/old, then calling</p>
<pre>mkdir backup/old</pre>
<p>could result in an error if the directory &#8220;backup&#8221; would not exist. This is a problem especially in scripts. Without the -p option you would have to make two calls to mkdir; one to create the &#8220;backup&#8221; directory (to ensure it is there) and one to create the directory &#8220;old&#8221; itself:</p>
<pre>mkdir backup
mkdir old</pre>
<p>When creating deeper directory structures, more than two calls might be needed. Think about creating the folder &#8220;backup/server/2011/11/30&#8243; in your home folder (/home/me). You would have to create every folder separately:</p>
<pre>mkdir backup
mkdir backup/server
mkdir backup/server/2011
mkdir backup/server/2011/11
mkdir backup/server/2011/11/30</pre>
<p>With the -p option you just need to call mkdir once:</p>
<pre>mkdir -p backup/server/2011/11/30</pre>
<p>and everything will be created for you.</p>
<p>&nbsp;</p>
<p>It is also worth noting that using the &#8220;-p&#8221; option will prevent errors in case the directory already exists. Without the &#8220;-p&#8221; option, running</p>
<pre>mkdir backup</pre>
<p>would result in the following error message in case the directory backup already exists:</p>
<pre>mkdir: cannot create directory `backup': File exists</pre>
<p>However, running</p>
<pre>mkdir -p backup</pre>
<p>will prevent the error message from being printed. Running mkdir with the  &#8220;-p&#8221; can therefore be interpreted as &#8220;make sure the following directory exists&#8221;.</p>
<p>&nbsp;</p>
<h3>-v, &#8211;verbose</h3>
<p>When using the &#8220;-v&#8221;/&#8221;&#8211;verbose&#8221; option, mkdir will ouput a message for the directory  it created.</p>
<p>Calling</p>
<pre>mkdir backup</pre>
<p>will therefore either output</p>
<pre>mkdir: created directory `backup'</pre>
<p>if the directory did not exist already or print the previously mentioned error message in case the directory was already there. When combining the &#8220;-v&#8221; option&#8221; with the &#8220;-p&#8221; option, mkdir will output a message for every directory it created.</p>
<p>Typing</p>
<pre>mkdir -p -v backup/server/2011/11/30</pre>
<p>or even shorter</p>
<pre>mkdir -pv backup/server/2011/11/30</pre>
<p>would create the following output if none of the directories would already exist:</p>
<pre>mkdir: created directory `backup'
mkdir: created directory `backup/server'
mkdir: created directory `backup/server/2011'
mkdir: created directory `backup/server/2011/11'
mkdir: created directory `backup/server/2011/11/30'</pre>
<p>If, for example, the directory &#8220;backup/server&#8221; already existed, then mkdir would only need to create the last three directories and therefore the output would be:</p>
<pre>mkdir: created directory `backup/server/2011'
mkdir: created directory `backup/server/2011/11'
mkdir: created directory `backup/server/2011/11/30'</pre>
<p>Using this option therefore provides feedback about which directories already existed and which had to be created. Because the &#8220;-p&#8221; option suppresses error messages about already existing directories, you won&#8217;t get error messages about the fact that &#8220;backup&#8221; and &#8220;backup/server&#8221; could not be created, because they were already there. The messages from mkdir about which directories have been created would therefore only implicitly tell you which directories existed beforehand.</p>
<h3>-m, &#8211;mode=MODE</h3>
<p>Using the &#8220;-m&#8221; option you can set the permission on the directory you want to create. If you want to create a directory which should be  fully accessible by everyone, you could type</p>
<pre>mkdir -m 777 backup</pre>
<p>or</p>
<pre>mkdir -m a=rwx backup</pre>
<p>It is important to note that when combining this option with the &#8220;-p&#8221; option, the mode only affects the last directory. Calling</p>
<pre>mkdir -p -m 777 backup/server/2011/11/30</pre>
<p>would therefore create all the directories necessary, but the permissions specified with the &#8220;-m&#8221; option will be only assigned to the directory &#8220;30&#8243; and not to the other directories. Their permissions are instead determined by the current umask.</p>
<p>It is also worthy to note that the permissions of the directory &#8220;30&#8243; would not be changed in case it already existed. This is especially important to point out, because the &#8220;-p&#8221; option will prevent all error messages about already existing directories. Therefore, running this command does not give you any feedback about the permissions the directory &#8220;30&#8243; has after running the command. If it already existed, the permissions will be left untouched and if it didn&#8217;t exist, the permissions will be set as defined by the &#8220;-m&#8221; option.</p>
<p>Therefore, if you are not sure if the directory already existed, but want to make sure that the permissions are set correctly, you could either run the command with the -v option (which will then tell you if the directory &#8220;30&#8243; was created) and then use chmod if necessary or &#8211; if you are writing a script &#8211; you should just run chmod afterwards instead of using the &#8220;-m&#8221; option:</p>
<pre>mkdir -p backup/server/2011/11/30
chmod 777 backup/server/2011/11/30</pre>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=262</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>xwd &#8211; Using xwd To Take Screenshots In Linux</title>
		<link>http://blog.tordeu.com/?p=135</link>
		<comments>http://blog.tordeu.com/?p=135#comments</comments>
		<pubDate>Tue, 22 Nov 2011 00:26:12 +0000</pubDate>
		<dc:creator>tordeu</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux Commands]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[imagemagick]]></category>
		<category><![CDATA[screenshot]]></category>
		<category><![CDATA[x window dump]]></category>
		<category><![CDATA[x window system]]></category>
		<category><![CDATA[xwd]]></category>
		<category><![CDATA[xwinfo]]></category>

		<guid isPermaLink="false">http://blog.tordeu.com/?p=135</guid>
		<description><![CDATA[Introduction One of the possibilities of taking screenshots in Linux is using xwd , a utility of the X Window System. I just want to give a short overview on how to take interactive and non-interactive screenshots, view them and convert them to different formats. Taking Screenshots To take a screenshot you could just open <a href='http://blog.tordeu.com/?p=135'>[...]</a>]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>One of the possibilities of taking screenshots in Linux is using</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>, a utility of the X Window System.</p>
<p>I just want to give a short overview on how to take interactive and non-interactive screenshots, view them and convert them to different formats. <span id="more-135"></span></p>
<h2>Taking Screenshots</h2>
<p>To take a screenshot you could just open a terminal and type &#8221;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>&#8220;, but by default</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>does not save the screenshot to a file (it outputs the image to stdout). To save the screenshot in a file, you can either use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-out</div></div>
<p>option of</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>:</p>
<pre>xwd -out &lt;filename&gt;</pre>
<p>or redirect the output of</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>to a file using the functionality offered by the shell:</p>
<pre>xwd &gt; &lt;filename&gt;</pre>
<p>Just replace</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;lt;filename&amp;gt;</div></div>
<p>with the name of the file you want to use for your screenshot. Examples:</p>
<pre>xwd -out myshot.xwd</pre>
<p>or</p>
<pre>xwd &gt; myshot.xwd</pre>
<p>Notice the changed mouse pointer after typing one of these commands ? That&#8217;s because you now need to select the window of which you want to take a screenshot. The format</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>uses for the images it is called &#8220;X Window Dump&#8221; and the extension often used is</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.xwd</div></div>
<p>. I will cover the subject of converting the images to different formats (like JPEG or PNG) later.</p>
<h3></h3>
<h3>Including the Window Frame</h3>
<p>If you want to include the frame of the window in the screenshot, you have to add the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-frame</div></div>
<p>option. For example:</p>
<pre>xwd -frame -out myshot.xwd</pre>
<p>or</p>
<pre>xwd -frame &gt; myshot.xwd</pre>
<p>&nbsp;</p>
<h3>Taking Screenshots Of The Whole Desktop</h3>
<p>If you want to create a screenshot of the whole desktop instead of a single window, you can use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-root</div></div>
<p>option for that:</p>
<pre>xwd -root -out myshot.xwd</pre>
<p>or</p>
<pre>xwd -root &gt; myshot.xwd</pre>
<p>&nbsp;</p>
<h3>Automated Screenshots Without Selecting The Window</h3>
<p>It&#8217;s also possible to create screenshots of windows without the need to select them every time. This is especially useful if you want to make numerous screenshots of a single window (e.g. for a tutorial, a presentation, an article etc.).</p>
<p>To do this, you have to tell xwd which window you want to take a screenshot of on the command line, so that it does not have to ask. You can do this either by using the &#8220;resource id&#8221; of the window or the &#8221;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WM_NAME</div></div>
<p>property&#8221;. The value of the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WM_NAME</div></div>
<p>property basically is the title of your window as it is displayed in the window title bar. Using this to create screenshots can be problematic (With Firefox, for example, this value depends on the title of the active tab, so going to a different tab will change the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WM_NAME</div></div>
<p>property of the window).</p>
<p>To determine the resource id or the value of the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WM_NAME</div></div>
<p>property, you can use the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwininfo</div></div>
<p>tool. Just type this in the terminal:</p>
<pre>xwininfo</pre>
<p>And then, similar to</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>, click on the window you are interested in. It will then display a bunch of information about the window, which might look like this:</p>
<pre>xwininfo: Window id: <span style="color: #ff0000;">0x40000e7</span> <span style="color: #3366ff;">"Home of the Mozilla Project - Mozilla Firefox"</span>

  Absolute upper-left X:  1287
  Absolute upper-left Y:  70
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 1159
  Height: 1501
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +1287+70  -114+70  -114-29  +1287-29
  -geometry 1159x1501+1279-21</pre>
<p>The red and blue color was something I added for clarification. The red thing is the resource ID we are interested in and the blue thing is the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WM_NAME</div></div>
<p>property that we also could use.</p>
<p>If we wanted to use the resource ID, we would do it like this</p>
<pre>xwd -id  -out myshot.xwd</pre>
<p>If we want to use the WM_NAME property, we would do this:</p>
<pre>xwd -name  -out myshot.xwd</pre>
<p>&nbsp;</p>
<p>One important thing to note about this approach is that using the -frame option for xwd does not have any effect if you supply the id or name as well. That is, because the resource id for both cases is different. Therefore, if you want to include the window frame, you have to supply the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-frame</div></div>
<p>option to</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwininfo</div></div>
<p>to retrieve the correct resource id. You would then type:</p>
<pre>xwininfo -frame</pre>
<p>Just to show you that they really differ: In my case, right now, running</p>
<pre>xwininfo</pre>
<p>gives me:</p>
<pre>xwininfo: Window id: 0x40000e7 "Home of the Mozilla Project - Mozilla Firefox"</pre>
<p>while running</p>
<pre>xwininfo -frame</pre>
<p>and clicking on the same, unchanged window, gives me:</p>
<pre>xwininfo: Window id: 0xe002df (has no name).</pre>
<p>&nbsp;</p>
<h2>Viewing Screenshots</h2>
<p>You can view the</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>-screenshots directly with the tool</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwud</div></div>
<p>. Just type:</p>
<pre>xwud &lt;filename&gt;</pre>
<p>for example:</p>
<pre>xwud screenshot.xwd</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Converting and Editing Screenshots</h2>
<h3>Converting Screenshots</h3>
<p>You can easily convert the screenshots to &#8220;normal&#8221; image formats like JPEG or PNG with tools like ImageMagick. If you don&#8217;t have ImageMagick installed, it should be available through your repositories. For example, in Ubuntu/Debian you would simply type:</p>
<pre>sudo aptitude install imagemagick</pre>
<p>or</p>
<pre>sudo apt-get install imagemagick</pre>
<p>&nbsp;</p>
<p>Once you have ImageMagick installed, you can convert an</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>image by using</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">convert</div></div>
<p>. For example, to convert an</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xwd</div></div>
<p>-screenshot named</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shot.xwd</div></div>
<p>to a JPEG file named</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shot.jpg</div></div>
<p>, type:</p>
<pre>convert shot.xwd shot.jpg</pre>
<p>You can convert the image to other formats as well, like PNG for example:</p>
<pre>convert sot.xwd shot.png</pre>
<p>&nbsp;</p>
<h3>Editing Screenshots</h3>
<p>I won&#8217;t include a tutorial on editing screenshots here, but I just wanted to mention that you might not have to convert your screenshots to JPG or PNG (or something else) in order to edit them, because your image editor might already be able to load</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.xwd</div></div>
<p>files. GIMP, for example, can open them directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tordeu.com/?feed=rss2&#038;p=135</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
