MiniTrue
A file-view, file-search, and find/replace utility

MiniTrue is an overlooked search/replace utility, available for both MS-DOS and Linux. The DOS versions come in 16-bit (mt.exe) and 32-bit (mtr.exe) varieties. MiniTrue (mtr) was written by Andrew Pipkin and is free software, distributed with the source code under the GNU General Public License. Yes, it has a weird name. From my experience with Win32 systems, MiniTrue works best under Windows 9x and sorta half-way under Winows 2000. I have used it to change thousands of web files on a Win2K box, so it is a usable piece of software. I have not tested it on WinXP or WinME at all. You can download precompiled binaries (executables) and the source code here:

http://www.idiotsdelight.net/minitrue/

The following comments refer to MiniTrue v2.0.2 (Alpha #3), which is the latest release version available.

Three Basic Uses of MiniTrue (mtr)

  1. Find-and-replace in files (like "sed")
    • Defaults to prompted mode: highlight each match in context, query for replace
    • Defaults: regular expressions ON, case-insensitive, make numbered backup files
    • Option: directory recursion, traversing the directory tree to find matches
    • Options: whole-word and case-sensitive matching, automatic replacements
    • Options: statistics, log files, replacement scripts, binary input, more
  2. View file contents in pager (like "less")
    • Defaults: regular expressions ON, case-insensitive matching
    • Matched expressions are highlighted, toggle ASCII/hex mode
    • Page forward, backward across the file; embedded help file
    • Options: control video mode, control placement of match on screen, expand tabs
    • Options: search inside .ZIP files, accept list of filenames, boolean proximity
  3. Send matching lines to console (like "grep")
    • Defaults: regular expressions ON, case-insensitive matching
    • Options: list of search terms, list of filenames, whole-word searching, inverse match
    • Options: number lines, list filenames but not matches, omit all context, more

General syntax

mtr -swit file1 file2... - find = replace  # find-and-replace in pager
mtr -no+ file1 file2... - "find" = "repl"  # automatic replace like sed

Display file1, highlighting the find term if it exists, and prompt the user to replace here or skip to the next match. A backup file is made by default. After the first file is processed, proceed to file2 and so on. One hyphen separates the files from the find strings. Option switches are indicated by -swit.

-n means "replace automatically" (no prompting)
-o sends the changed lines to stdout instead of to the pager.
-o+ sends both the changed and the unchanged lines to stdout, just like sed.

mtr file1 file2... - find         # view files in pager
mtr -o file1 file2... - "regex"   # view lines like grep

Display file1, highlighting the find term if it exists. At the end of file1, proceed to display file2 and so on. No backup file is created, since the files are not altered. Use "double quotes" if the search term contains angle brackets, pipes (|), or spaces.

mtr -swit find [= replace] -- file1 file2...

Alternate syntax. Note that two hyphens allow the user to put the find or find = replace pattern before the list of files. For a list of all options, go here.

Defaults

Real examples I've used at work

mtr -ro# f:\dev-sem\* - "home\.html"

I need to find out how often and where home.html occurs in my files
Recurse through subdirectories (-r)
Send results to stdout, not to viewer (-o), add line numbers (-o#)
This command is identical to:   grep -inr "home\.html" *


mtr -nr *.ht* - "home\.html" = "news.html"

After checking first, I do the replacement
No prompting, replace automatically (-n)
Recurse through subdirectories (-r).
Numbered backups always created (info~1.ht1, main~1.ht1 ...) on find/replace (yes, I lose the LFN on backup files)


mtr -x- -kn -b:..BAK t*.html - ".../" = "../../"

Some joker used three periods .../ to navigate up two levels, so I gotta fix it.
Turn off regex expansion (-x-)
Keep a list of hits and results (-k)
No prompting, replace automatically (-n)
Backup file has .BAK replace original filetype (-b)

  • Numbered back-up files are created by default; originals will keep LFN
  • Backup files usually change to short filenames running on Win32, docum~1.ht2
  • -b:BAK appends .BAK to the filename
  • -b:..BAK replaces the original extension with .BAK
  • -b+:NEW sends the altered version to the newfile extension
  • -b- disables the creation of backup files


mtr -wc+ myfile - "Adolph" = "Adolf"

Somebody misspelled Adolf's name. Fix it, even if it's capitalized.
Whole word match (-w)
Correspond case throughout whole word (-c+), so
    adolph --> adolf, Adolph --> Adolf,  and  ADOLPH --> ADOLF.
Or, correspond case only on first letter (-c~), so
    adolph -->adolf, Adolph --> Adolf,  but  ADOLPH --> Adolf (This isn't one I use much)


mtr -i:script.txt -@:FILELIST

I have a script file (script.txt) for my changes, plus a list of files to alter in FILELIST
Get find/replace strings from input file script.txt (-i).
If the script file is omitted, get find/replace pairs from console.
Use FILELIST to get names of files to process (-@).
If the filelist is omitted, get list of names from console.


mtr -ko -i:STRINGS string --

Keep a list of hits and search data (-k)
Send results to console, not to viewer (-o)
Use input list of STRINGS to find or replace



Annoyances and Workarounds with MiniTrue

Though MiniTrue is a great program, it has aggravating misbehaviors under Windows 2000, which is what I use at work. Fortunately, those misbehaviors are predictable, and there are workarounds.

1. It doesn't handle wildcards appended to Long File Names

This means that if you type a command like:

       mtr SomeValidLongname*.htm - only = merely

which ought to expand normally under Windows 9x or Windows 2000 (assuming those files exist), nothing will happen at all. To properly handle the files, you must use 8.3 filename conventions, like so:

     mtr Some*.htm - only = merely
     mtr some* - only = merely

Then, SomeValidLongname01.htm, SomeValidLongname02.htm, and SomeValidLongname03.htm will all be matched and changed. The new files will receive the long filename, but the backup files be renamed to an 8.3 format (See point #3 below).

2. It doesn't handle keyboard input properly under Windows 2000

With mtr, the 32-bit version of MiniTrue, you will get a flashing cursor on the next line, but nothing else. (This bug appears in Windows 2000, but not in Windows 98). You may think that MiniTrue is hung up, but the workaround is to press the command key followed by the ENTER key. If viewing the help menus, you must press the movement key twice (i.e., instead of pressing space to page down, press space-space. Instead of pressing b to move backwards a page, press bb). Sometimes you have to press a command key followed by the ENTER key. This behavior is somehow related to how Win2K handles the console, which is not the same as how Win9x handles the console. (Maybe the author can borrow some code from the Windows version of less or something.)

The simplest fix for this problem was sent to me by another user. Just use mt, the 16-bit version of MiniTrue, instead. He reports that the 16-bit version will not handle as many files as the 32-bit version (I don't know, since I haven't tested it myself), but I can confirm that on a Win2000 system, the 16-bit version does handle keyboard input properly.

3. It munges Windows long filenames on backup copies

The following is not complete, but since it may be a long time before I have time to finish it, I thought it best to post what I have in its provisional form.

The worst thing about MiniTrue is that if you tell it to fix a bunch of files on a system that uses long filenames, the new file still keeps the long filename all right (such as stewardship.html), but the backup (original) file often gets changed to an 8.3 name like stewar~1.ht1. So as long as your replacements are perfect, you have nothing to worry about.

But if you should make a mistake and want to revert to your original filenames, you may find yourself in trouble. This happened to me once, after changing several hundred files with long filenames. I accidentally changed hundreds of *.html files with an untested pattern. I had guessed wrong. The new/changed files had the right long filenames, but the changes were in the wrong place.

I don't remember exactly what I did to recover from that disaster, but it was recoverable. I think that none of the short filenames used ~2 or ~3, so I could rely on the fact that the first 6 characters of the "bad" files and the "good" files were always the same, and then I did a "for..in..do" loop involving file renaming and substring matching.

The lesson here is to use short (8.3) filenames as much as possible, if you can. If you must change a group of files, before you use the -n (no promping) switch, use the selective-preview mode (the default mode for MiniTrue) on a few files, and then when you are sure the replacements are what you expect, you can invoke MiniTrue with the -n switch on the rest.

I think there is a work-around that will preserve long filenames in the backup files that MiniTrue creates, but I don't recall what it is. If someone else finds out, let me know. Thanks.


Last updated: Dec. 3, 2003

E-mail your comments or corrections to: Eric Pement