tag:blogger.com,1999:blog-63451631455111357312018-03-06T00:22:13.282-08:00Dot Slash StarEverything at the current level (not hidden).Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.comBlogger6125DotSlashStarhttps://feedburner.google.comtag:blogger.com,1999:blog-6345163145511135731.post-71584481382891160232012-05-16T14:03:00.000-07:002012-05-16T15:49:53.608-07:00Shell Trick: Background Launch<h2> The Issue </h2>Here is the problem.&nbsp; It is not a big one, but it was quite an annoyance for me.&nbsp; I want to launch a command, but I do not want it to block my command line, and I do not want it to pollute my terminal screen with all kinds of output.<br /><br />The typical example for me is the gimp photo editor.&nbsp; I use it infrequently so I do not give it precious desktop real estate with its own launcher.&nbsp; What I usually do is bring up whichever gnome-terminal is active and launch it from the command line in the background. <br /><pre>&gt; gimp &amp;<br /></pre><br />Of course I launch it in the background because I want the command prompt to return in case I want to do other command line operations before I exit gimp.&nbsp; The the problem with this is that gimp (like a bunch of other native GUI applications) produces a bunch of warning messages that I just do not want to see in my terminal.<br /><br /><pre>(gimp:12691): GLib-WARNING **: /build/buildd/glib2.0-2.30.0/./glib/goption.c:2168: ignoring no-arg, optional-arg or filename flags (8) on option of type 0<br /><br />(gimp:12691): Gtk-CRITICAL **: _gtk_file_chooser_entry_set_file_part: assertion `GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry)' failed<br /><br />(gimp:12691): GLib-GObject-WARNING **: invalid cast from `GimpView' to `GtkImage'<br /><br />(gimp:12691): Gimp-Widgets-WARNING **: gimp_dialog_factory_dispose: stale non-toplevel entries in factory-&gt;open_dialogs<br /></pre>Yes, these are just ignorable warnings, but do not want to see them. Perhaps I have historical output in that screen that I do not want scrolled away. More importantly, I do not want warnings to spontaneously appear while I type in that terminal.<br /><br /><h2> The Non-Solution</h2>So I could do <tt>Alt-F2</tt> to run a command in an invisible, implicit terminal. There are two big problems with this.<br /><ol><li>It is not a bash shell so I do get command completion, command history, or line editing.</li><li>It probably does not have my $PATH customizations, so many of the commands I want to reference will be inaccessible.</li></ol><h2> The Solution</h2>So I wrote a simple bash shell function to do the launching of the command, put it in the background, and send all of its output to a log file.&nbsp; (I could have sent output to <tt>/dev/null</tt> to make it completely silent, but the log file does not consume much resources and it does come in handy if a post-mortem is needed.) <br /><pre># Launch a command in the background, with all output saved to a log file.<br /># This is useful when verbose output is expected from the command and you<br /># do not want to clutter the terminal that launched it.<br />bglaunch () {<br /> if [[ $# != 0 ]]; then<br /> # The log name will be the command name + ".log"<br /> logname=$(basename $1)<br /><br /> # Build the full path name of the log file.<br /> if [ -d "${SCRATCH_LOG_DIR}" ]; then<br /> # If $SCRATCH_LOG_DIR is defined and exists, put the log there.<br /> logfile="${SCRATCH_LOG_DIR}/${logname}.log"<br /> else<br /> # Otherwise put it in [/var/tmp] and prepend it with $USER.<br /> logfile="/var/tmp/${USER}-${logname}.log"<br /> fi<br /><br /> printf "Output to [%s]\n" "${logfile}"<br /> eval "$@ &gt; ${logfile} 2&gt;&amp;1 &amp;"<br /> fi<br />}<br /></pre>Note that this snippet should be placed in your <tt>~/.bashrc</tt> or similar appropriate location.<br /><br />So now I simply do the following.<br /><pre>&gt; bglaunch gimp<br />Output to [/home/johndoe/var/log/gimp.log]<br />[2] 14275<br /></pre>I get exactly what I want.<br /><ul><li>The command is launched in the background so the command line is immediately returned.</li><li>Any arguments and switches can be given to the command without confusion or conflict.</li><li>Wildcards can be used to expand filenames in the arguments to the command.</li><li>No further output of the command show up on the screen.</li><li>I take advantage of command completion to write out the full command name, then go the beginning of the line and prepend "bglaunch" to the line. </li></ul><h2> Detail Notes</h2>The actual location of the output log is configurable.&nbsp; It allows the user to places all logs in a directory that is specific to that user.&nbsp; Otherwise, all the logs go to <tt>/var/tmp</tt> and get prepended by the username so that different users do not conflict with each other.&nbsp; As shown in the output above, this additional definition was declared in <tt>.bashrc</tt>. <br /><pre>&gt; export SCRATCH_LOG_DIR="${HOME}/var/log"<br /></pre><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/XQAsp15AEUc" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2012/05/shell-trick-background-launch.htmltag:blogger.com,1999:blog-6345163145511135731.post-19561768151379042862011-10-14T14:44:00.000-07:002011-10-14T17:48:17.878-07:00Shell Trick: Dynamic bashrc SetupAnyone who works on Unix has run into the issue of setting up the environment consistently among multiple hosts. If you are a power user, you likely have developed a robust set of aliases, functions, variable settings, etc. that streamline your command-line effectiveness. However, when you create a new account on a different host, it becomes a bigger task to set it up like your others.<br /><br />Besides just the volume of customizations, each different environment may require specific settings. For example, the path to your favorite editor may be different.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Overview</span></span><br /><br />I have come up with a solution for configuring a bash (Bourne Again Shell) environment which allows common settings and definitions, and also customizations at the level of operating system and host. The modularity is achieved by hooking the framework minimally into <span style="font-family:courier new;">.bashrc</span> and then using separate files for the actual settings so the proper ones can be selected on different environments. A graphical representation of this framework is shown in figure 1.<br /><br /><div style="text-align: center;"><a href="http://1.bp.blogspot.com/-uxuawvubwKM/Tpid9Sy5KgI/AAAAAAAABpk/7RhfjR0dYJg/s1600/envsetup.jpg"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 259px;" src="http://1.bp.blogspot.com/-uxuawvubwKM/Tpid9Sy5KgI/AAAAAAAABpk/7RhfjR0dYJg/s400/envsetup.jpg" alt="" id="BLOGGER_PHOTO_ID_5663450207761279490" border="0" /></a>Figure 1.<br /></div><br /><span style="font-size:130%;"><span style="font-weight: bold;">Hooking the Framework into the Shell</span></span><br /><br />The novice approach for customization is to put all the definitions in the <span style="font-family:courier new;">.bashrc</span> file. This becomes unmanageable quickly. Another downside of this is that the base image of this file can be different on different systems. Instead, this framework requires that only two lines are added at the end of the user <span style="font-family:courier new;">.bashrc</span> file.<br /><pre>export ENVSETUP="${HOME}/envsetup"<br />[[ -f ${ENVSETUP}/bashrc-common &amp;&amp; -n "${PS1}" ]] &amp;&amp; . ${ENVSETUP}/bashrc-common</pre>The first line declares where the framework is located. In this example, it is the envsetup directory in the user's home. The framework can be installed anywhere, as long as <span style="font-family: courier new;">ENVSETUP</span> is defined to be that locations. The second line calls the framework after performing two checks. First it checks that the expected main file exists. Then it checks that this is an interactive shell. A non-interactive shell (like one created for <span style="font-family:courier new;">scp</span>) will not need the customizations, and actually could fail because of output generated by the framework.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Driving the Framework<br /><br /></span></span>The entry point to the framework is the driver file <span style="font-family:courier new;">bashrc-common</span>. This is meant to be just structural logic, with the actual settings is separate files. All customizations should go in those other files. This file needs to be described to fully understand the framework.<br /><pre># A utility to do more env setup using the given file.<br /># Expect ENVSETUP to be properly defined.<br />SOURCE_LOG=""<br />sourceFrom () {<br /> base_file="$1"; shift<br /> source_file="${ENVSETUP}/${base_file}";<br /> if [ -f "${source_file}" ]; then<br /> # If an optional message was given, print it first.<br /> [[ $# -gt 0 ]] &amp;&amp; { printf "$1\n"; shift; }<br /><br /> . ${source_file}<br /> export SOURCE_LOG="${SOURCE_LOG}:${base_file}"<br /> return 0<br /> else<br /> return 1<br /> fi<br /><br /> # Note that if the file does not exist, no action is taken (silently).<br /> # Only the return value indicates that action was done.<br />}<br /><br /># - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br /><br /># Common aliases are defined externally.<br />sourceFrom bashrc-common-aliases<br /><br /># Common Functions are defined externally.<br />sourceFrom bashrc-common-functions<br /><br /># Platform-specific setup<br />printf "Bourne Again Shell "<br />os=$(uname -s)<br />case ${os} in<br /> Linux)<br /> sourceFrom bashrc-os-linux "on Linux.";;<br /> SunOS)<br /> sourceFrom bashrc-os-sunos "on SunOS.";;<br /> CYGWIN_NT-5.1)<br /> sourceFrom bashrc-os-win "on Windows XP.";;<br /> CYGWIN_NT-5.0)<br /> sourceFrom bashrc-os-win "on Windows 2000.";;<br /> CYGWIN_NT-4.0)<br /> sourceFrom bashrc-os-win "on Windows NT.";;<br /> *)<br /> printf "Unknown OS: %s\n" "${os}"<br />esac<br /><br /># host-specific setup<br />sourceFrom "bashrc-host-${HOSTNAME}" "Customization for host: ${HOSTNAME}"<br /><br /># Final processing after OS-specific and host-specific setup.<br />sourceFrom bashrc-common-final<br /></pre><br />The first element is the definition of a shell function which will assist in calling the sub-files. It is not necessary to detail it here. The main logic is after the dividing line.<br /><br /><span style="font-weight: bold;">Common Aliases and Functions</span><br /><br />First it reads the definitions of the aliases and the functions from separate files. There is no strict reason for these two to be in separate files other than it makes for cleaner organization.<br /><br /><span style="font-weight: bold;">Operating System Specific Settings</span><br /><br />Next comes the critical logic of dynamically selecting configuration files depending on the operating system and the specific host. The magic of determining the OS is done with the <span style="font-family:courier new;">uname -s</span> command. Notice that the output of this will specify the OS, but the actual file that gets called as a result is defined by the user. I have mapped various Windows operating systems to the single file <span style="font-family:courier new;">bashrc-os-win</span> (although a different message gets printed for each).<br /><br />Note that this list of operating systems is incomplete. It reflects only those systems that I have tested this framework on. Other users would extend it to the other systems they are using. It it encounters a undefined system, the output will display the string to add to the case statement.<br /><br /><span style="font-weight: bold;">Host Specific Settings</span><br /><br />Although the OS requires a specific collection of settings, it may be necessary to further customize the environment to have settings for an individual host. This is done by the next step. It only requires that the settings be given in the file named <span style="font-family:courier new;">bashrc-host-</span>HOSTNAME (where HOSTNAME is replaced by the value which that environment variable will have on that system).<br /><br /><span style="font-weight: bold;">Final Common Settings</span><br /><br />The final step is to call a file containing common settings that are dependent on earlier settings that come from OS-dependent of host-dependent files. To understand this, consider this example.<br /><pre>export JDK_HOME=${JAVA_HOME}<br /></pre>This will ensure that JDK_HOME has the same value of JAVA_HOME, but the latter will likely be defined in a host specific file. Since we want these two variables to have the same value on all systems, we want this to be in a common section.<br /><br /><span style="font-weight: bold;">Output</span><br /><br />You may have noticed several <span style="font-family:courier new;">printf</span> statements in the listing. The purpose of these is to provide some minimal information about the environment while allowing the ability of doing some basic debugging. Consider the output that I see on my main system:<br /><pre>Bourne Again Shell on Linux.<br />Customization for host: king</pre>The first line is always printed starting with "Bourne Again Shell" as an indication that the framework was triggered. It ends naming the OS customization it is using (or that it could not match the OS). The second line states which host-specific file will be used.<br /><br />The usefulness of this output comes from the fact that the OS and host are the variables that change from system to system. You can understand how those variables were resolved from these two lines.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Why Use this Framework</span></span><br /><br />The power of this framework is realized when a fresh new environment needs to be set up on a new host. That is done simply by copying all files from one existing system to the new one, and then creating a new host specific file from another existing one.<br /><br />This is easy to do because of the following advantages of this framework:<br /><ul><li>All files are in a separate subdirectory. They are not intermixed with other, unrelated files in the user's home directory.</li><li>All the files are not-hidden. Searching through and debugging a set of hidden files can be inconvenient.</li><li>Exactly the same set of files (with same content) used on different systems.</li></ul><span style="font-size:130%;"><span style="font-weight: bold;">How to Use this Framework</span></span><br /><br />Implementing this yourself can be done in the following steps.<br /><ol><li>Create a directory for all the setup files. (This will be referenced in the scripts as <span style="font-family:courier new;">ENVSETUP</span>.)</li><li>Create the file <span style="font-family:courier new;">bashrc-common</span> and copy the contents from above.</li><li>Add the two lines to the end of <span style="font-family:courier new;">.bashrc</span> as described above. (At this point, the bare framework is working.)</li><li>Create the setup files for your operating system and host. You may need to customize <span style="font-family:courier new;">bashrc-common</span> if it does not recognize your operating system.</li><li>Move all your customizations to the appropriate setup files.<br /></li></ol><span style="font-size:130%;"><span style="font-weight: bold;">More Debugging Details</span></span><br /><br />If it is unclear which files are being read in which order, then the following command can be executed to find out for sure. (Note that the tr command is used to split the single-line output into multiple lines.)<pre>echo $SOURCE_LOG | tr : \\n</pre>On my main system, this produces the following. It displays the exact sequence.<br /><pre>bashrc-common-aliases<br />bashrc-common-functions<br />bashrc-os-linux<br />bashrc-host-king<br />bashrc-common-final</pre><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/7BmyCji6Vl0" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2011/10/shell-trick-dynamic-bashrc-setup.htmltag:blogger.com,1999:blog-6345163145511135731.post-27005350111605260752011-08-05T14:57:00.000-07:002011-10-14T17:48:17.878-07:00Shell Trick: removing all empty directoriesGiven a directory structure (a subtree) that contains files and directories, I sometimes want to quickly delete all empty directories. There is a simple one-line command that will do this.<br /><br />There are also many incorrect solutions that are suggested in online discussions. Before presenting my script that does this, let us examine some of the other suggested ways to do it. They are not completely wrong, they just work only for some special cases.<br /><a href="http://3.bp.blogspot.com/-ivwA7DGG0TQ/TjxpAUhH0BI/AAAAAAAABoo/_DobfK63RJI/s1600/empty_dirs.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 218px; height: 285px;" src="http://3.bp.blogspot.com/-ivwA7DGG0TQ/TjxpAUhH0BI/AAAAAAAABoo/_DobfK63RJI/s400/empty_dirs.jpg" alt="" id="BLOGGER_PHOTO_ID_5637496287789568018" border="0" /></a><br />Consider the example at left. The command will need to walk down the directory tree and search for directories. Any directories that are empty need to be removed.<br /><br />We want to start examining at <span style="font-weight: bold;">d1</span>. Notice that we will find 3 empty subdirectories — <span style="font-weight: bold;">d3</span>, <span style="font-weight: bold;">d6</span>, and <span style="font-weight: bold;">d7</span>. If we remove those, we will create one newly empty directory — <span style="font-weight: bold;">d5</span>. Removing that will cause <span style="font-weight: bold;">d4</span> to become empty. Then <span style="font-weight: bold;">d4</span> can be removed and there will be no empty directories remaining.<br /><br />I have found some suggested solutions but they have drawbacks:<br /><pre> find d1 -type d -empty -exec rmdir {} \;</pre>This cannot work because it traverses the tree top-down. It will find the first pass of empty directories, but not the ones that become empty when their subdirectories are removed.<br /><pre> find d1 -type d -empty -exec rmdir -p {} \;</pre>Actually this almost works. The difference with the previous flavor is that it knows that it is operating only on leaf directories, but it also then tries to remove empty parent directories. But it has the problem of not knowing where to stop. There is no bound on the root, so it could remove a directory at a level higher than what we specify.<br /><br />The correct solution is<br /><pre> find d1 -depth -type d -empty -exec rmdir {} \;</pre>The important aspect is that we need to do a depth-first traversal of the sub-tree. This will allow us to handle the cases where previously non-empty directories become empty.<br /><br />So I came up with the following script and named it "rmEmpty.sh". Because the action can be done with a single-line command, it really is not strictly necessary to encapsulate it in a script. But notice how many moving parts are working in the argument list to 'find'. Putting it in a script saves quite some typing. Besides the convenience of this, it also adds a factor of safety.<br /><pre>#!/bin/bash<br />#-----------------------------------------------------------------------------<br /># Remove all empty directories under and including the given root(s).<br />#-----------------------------------------------------------------------------<br /><br /># If no args given, then root at $PWD.<br />[ $# == "0" ] &amp;&amp; set "$PWD"<br /><br /># Process each arg as a root to examine.<br />for root in "$@"; do<br /> if [ -d "${root}" ]; then<br /> find "${root}" -depth -type d -empty -exec rmdir -v {} \;<br /> else<br /> printf "Not a directory: [%s]\n" "${root}"<br /> fi<br />done<br /></pre>So notice that the action can now be invoked simply as <pre>rmEmpty.sh d1</pre>A couple points:<br /><ul><li>Notice that the quoting of variables as shown is strictly necessary to handle directory names which may contain spaces.</li><li>The script is verbose about each removal so you can explicitly see all the actions. The "-v" can be removed from the call to 'rmdir' to reduce verbosity.</li><li>One curiosity of this script is that if you run it with no args, it starts processing from your current working directory. It could end up deleting your current directory. This is not an error condition! You can change to another existing directory and continue working.</li></ul><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/1uZz5vsUaF4" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2011/08/shell-trick-removing-all-empty.htmltag:blogger.com,1999:blog-6345163145511135731.post-35330301911739594642011-06-27T15:44:00.000-07:002011-06-27T15:59:25.792-07:00Shell Trick: cron script indirection<span style="font-weight:bold;font-size:130%;" >The Problem</span><br /><br />When you add crontab entries, it is easy to forget that they will be executed in a different environment than your usual shell. None of the settings from your <span style="font-family:courier new;">.bashrc</span> will be available. The environment will not be empty, but it will be pretty bare. If your command depends on any of these settings, the will fail. The different environment also makes it trick to test. You might end up with a long, complicated one-line command that is hard to maintain.<br /><span style="font-size:130%;"><br /></span><span style="font-weight:bold;font-size:130%;" >The Solution</span><br /><br />One solution is to use the '<a href="http://www.linuxcommand.org/man_pages/env1.html">env</a>' command to explicitly build the environment for your command. This can work, but it can make your entry very long and hard to read (and thus hard to test/debug/maintain).<br /><br />My favored approach is to make all crontab entries simply invoke a script that defines all the settings and then invokes the desired command. I create the directory <span style="font-family:courier new;">${HOME}/shrawenv</span> to hold all these scripts. (I chose this name to denote "for shell with raw environment".)<br /><br />For example, I would have a crontab entry like<br /><pre>00 5 * * * sh /home/user/shrawenv/update_project.sh</pre>Then I would define the 'update_project.sh' script as<br /><pre>#!/bin/sh<br /><br />export APP_HOME=/usr/share/app-1.1<br />export SOME_LIB=/usr/share/lib-2.3<br />$APP_HOME/bin/projectApp arg1 arg2</pre>This dummy example merely demonstrates how to set environment variables before invoking a command. It is a good idea to always use full paths in crontab because <span style="font-family:courier new;">PATH</span> may not be what you are used to.<br /><br />In my crontab, all the commands are just invoking scripts from <span style="font-family:courier new;">shrawenv</span>. I want to to list only a simple command and the time of execution. I find it much cleaner to have all the details of the command separately in the script file.<br /><br />Note that <span style="font-family:courier new;">shrawenv</span> is useful not just for cron, but for other contexts where execution is not happening in the usual shell command line. I also use it to house scripts to be called by desktop launchers.<br /><br /><span style="font-weight:bold;font-size:130%;" >Advantages</span><br /><dl><dt>Readability</dt><dd>Because this is a script file, it can edited and formated for elegance. Using 'env' would force it all into one line, and may require some syntactic gymnastics.</dd><br /><dt>Maintainability</dt><dd>If I have to make any adjustments to the logic, I do not edit the crontab. Instead I just modify the internals of the script as needed.</dd><br /><dt>Testability</dt><dd>You do not have to wait for cron to run to see what the script does. You can simply execute the script. There is an explanation of how to simulate the cron environment <a href="http://matthew.mceachen.us/blog/howto-simulate-the-cron-environment-1018.html">here</a>.</dd><br /><dt>Archivability</dt><dd>I keep my <span style="font-family:courier new;">shrawenv</span> under revision control. (I use subversion locally.) If I took the approach of putting all the logic in the crontab, I would not have any history.</dd><br /></dl><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/UKlj8bbur4Y" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2011/06/shell-trick-cron-script-indirection.htmltag:blogger.com,1999:blog-6345163145511135731.post-52389269278518194222011-06-22T11:32:00.000-07:002011-06-27T16:11:03.307-07:00SVN hack: insert missing 'trunk' root directory<span style="font-weight:bold;font-size:130%;" >The Problem</span><br /><br />One of my Sourceforge projects got its Subversion (SVN) repository created incorrectly. The root was missing the 'trunk', 'branches', and 'tags' subdirectories. All the content that should have been under 'trunk' was in the root.<br /><br />I did not have time to fix this right away, so some development was done while still structured like this. Now I need to fix it (the maven release plugin requires the correct SVN setup). I wanted to find a way to create a 'trunk' directory and move all content into it.<br /><br />I will not cover details of the fix of the Sourceforge-hosted repository. The instructions for that are given <a href="http://sourceforge.net/apps/trac/sourceforge/wiki/SVN%20adminrepo">here</a>.<br /><br />This involves:<br /><ol><li>Create a local backup of the Sourceforge SVN repository.</li><li>Load the repository locally and fix it.</li><li>Create the SVN repository image on an Sourceforge shell server.</li><li>Replace it with the fixed image.</li><li>Commit the new image.</li></ol>The Sourceforge instructions are for steps 1, and 3 through 5, so I will document step 2 below. This will be the step of relevance for most people.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">The Solution</span></span><br /><br />The simplest option is to just create that new 'trunk' subdirectory, and then move all the files there. This solution to the problem is well documented <a href="http://www.thotspots.com/when-a-subversion-repository-is-missing-the-standard-trunktagsbranches-base-folders/">here</a>, along with the drawbacks. (A less detailed discussion of this approach was also found <a href="http://www.svnforum.org/threads/39304-How-to-create-a-trunk-in-an-existing-project">here</a>.)<br /><br />What I did not like about this method is that it is basically a copying of the files to a new location and corresponding removals from the original location, which I found inelegant. The complete histories of the files will end up split between the root directory and 'trunk' at the time of the move. I wanted a solution where if someone looked at the full history using my method, they would not see that it was ever structured incorrectly.<br /><br />I found what I felt to be a cleaner (from my perspective) approach which was roughly:<br /><ol><li>Create a dump of the original repository.</li><li>Edit/filter the contents to prepend 'trunk' to all paths.</li><li>Create a new repository and load the dump into it.</li></ol>This approach is actually mentioned in the <a href="http://svnbook.red-bean.com/en/1.5/svn.reposadmin.maint.html#svn.reposadmin.maint.filtering">official SVN documentation</a>. So this should not really be called a hack.<br /><br />It took some experimenting to deal with problems related to specific details, but the overall approach worked. Here are the specific commands that did it, (omitting the output since that is voluminous). I abbreviate the path to the repositories base on the local file system as $SVN_REPO_BASE.<br /><br /><span style="font-size:100%;"> <span style="font-weight: bold;">Perform the dump and rewrite the file paths</span></span><br /><pre>&gt; svnadmin dump $SVN_REPO_BASE/oldrepo | sed "s/^Node\(-copyfrom\)\?-path: /Node\1-path: trunk\//" &gt; filtered.dump</pre><br />This command uses the standard "svnadmin dump" utility, but passes the data through 'sed' to process the contents before writing the actual the dump file <a href="http://www.blogger.com/post-edit.g?blogID=6345163145511135731&amp;postID=5238926927851819422&amp;from=pencil#details-1"><sup><em>details-1</em></sup></a>.<br /><span style="font-size:100%;"><br /></span><span style="font-size:100%;"><span style="font-weight: bold;">Create a new repository for the fixed content</span></span><br /><pre>&gt; svnadmin create $SVN_REPO_BASE/newrepo<br />&gt; svn mkdir -m "Create root-level directories" file://$SVN_REPO_BASE/newrepo/trunk file://$SVN_REPO_BASE/newrepo/branches file://$SVN_REPO_BASE/newrepo/tags</pre><br />We create a bare new repository which will be loaded. I originally missed the 'svn mkdir' command, and that caused the upcoming "load" command to fail <a href="http://www.blogger.com/post-edit.g?blogID=6345163145511135731&amp;postID=5238926927851819422&amp;from=pencil#details-2"><sup><em>details-2</em></sup></a>. All of the files in the dump will be loaded in the 'trunk' directory, but nothing in that dump file itself actually creates that directory. So we have to do that by hand before loading.<br /><br /><span style="font-size:100%;"><span style="font-weight: bold;">Load the new repository</span></span><br /><pre>&gt; svnadmin load $SVN_REPO_BASE/newrepo &lt; filtered.dump</pre><br />Now the new repository is ready to use. If it is intended to be used somewhere else (e.g. Sourceforge) then a regular dump can be made of THIS repository and loaded elsewhere.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Caveats</span></span><br /><ul><br /><li>This approach probably works best for relatively young projects, since the dumps for active, mature projects are likely to be huge and cumbersome to process.</li><br /><li><a href="http://blog.fredrikbostrom.net/2007/03/14/migrating-part-of-repository-in-svn">This post</a> mentions a need to use 'svndumpfilter' before piping through 'sed', on the notion that it was necessary to handle binary data. I did not need that to make the load work, even though I did have binary files in the repository.</li><br /><li>The "sed" patterns are designed to match only the metadata headers in the dump, but there is an unlikely chance that the content will be matched and altered. This error can only happen if any file content exactly matches a dump header.</li><br /></ul><br /><hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">Notes</span></span><br /><dl><br /> <a name="details-1"></a><dt><sup><em>details-1</em></sup></dt><dd>The file paths are encoded in the "Node-path:" and "Node-copyfrom-path:" fields in the dump, so these are rewritten to have 'trunk' prepended. The final result is the processed dump file.</dd><br /> <a name="details-2"></a><dt><sup><em>details-2</em></sup></dt><dd>The error I was getting when trying the load without first the 'mkdir' was<pre> svnadmin: File not found: transaction '0-0', path 'trunk/pom.xml'</pre>I found the explanation of this error <a href="http://www.celticwolf.com/blog/2010/05/03/subversion-svnadmin-load-error-file-not-found-transaction-0-1-path/">here</a>.</dd><br /></dl><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/SfJxfVR_n9A" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2011/06/svn-hack-insert-missing-trunk-root.htmltag:blogger.com,1999:blog-6345163145511135731.post-50856299009282514952011-05-28T10:27:00.000-07:002011-06-27T15:57:51.443-07:00Shell Trick: Create a directory and change to it in one commandOften when you create a directory, you are also intending to change to that directory to continue with other operations. This is usually done as two consecutive commands<br /><pre>&gt; mkdir newdir<br />&gt; cd newdir<br /></pre>But wouldn't this be more easily done in one single command? After all, the same directory name is used in each command.<br /><br />I created a bash shell function<a href="#details-1"><sup><em>details-1</em></sup></a> that does exactly this. I called it "mkcd", since it is basically a combination of "mkdir" and "cd". It is defined as<br /><pre>mkcd () {<br /> if [ $# == 1 ]; then<br /> dir="$1"<br /> printf "mkcd %s\n" "${dir}"<br /> if [ -d "${dir}" ]; then<br /> cd "${dir}"<br /> else<br /> mkdir -p ${dir}<br /> if [ $? == 0 ]; then<br /> cd "${dir}"<br /> fi<br /> fi<br /> else<br /> printf "Usage: mkcd &lt;dir&gt;\n"<br /> fi<br />}</pre><span style="font-size:85%;"><span style="font-style: italic;">This would be defined in a .bashrc environment initialization file.</span></span><br /><br />This will skip the directory creation if it already exists. It actually uses "mkdir -p" so that it also creates any intermediate directory levels needed. It also only performs the "cd" if directory creation had no errors.<br /><br />The operation can now be done as<br /><pre>&gt; mkcd newdir<br /></pre><br /><hr><span style="font-size:130%;"><span style="font-weight: bold;">Notes</span></span><br /><dl><a name="details-1"></a><dt><sup><em>details-1</em></sup></dt><dd>Note that this is a <em>function</em> and not a shell <em>script</em>. Doing it as a script will not work because that runs as a child process, so the directory change happens in a different process and you will remain in the same directory in which you started.</dd><br /></dl><img src="http://feeds.feedburner.com/~r/DotSlashStar/~4/AsMeUPpA2m8" height="1" width="1" alt=""/>Murali Krishnanhttp://www.blogger.com/profile/09817713898431409065noreply@blogger.com0http://dotslashstar.blogspot.com/2011/05/create-directory-and-change-to-it-in.html