fileutil(n)
πΈ NAME πΈ
fileutil β Procedures implementing some file utilities. This is the fileutil(n) manual page, bestie, and it's literally all about file utilities. π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require fileutil ?1.16.3?
::fileutil::lexnormalize path
::fileutil::fullnormalize path
::fileutil::test path codes ?msgvar? ?label?
::fileutil::cat (?options? file)...
::fileutil::writeFile ?options? file data
::fileutil::appendToFile ?options? file data
::fileutil::insertIntoFile ?options? file at data
::fileutil::removeFromFile ?options? file at n
::fileutil::replaceInFile ?options? file at n data
::fileutil::updateInPlace ?options? file cmd
::fileutil::fileType filename
::fileutil::find ?basedir ?filtercmd??
::fileutil::findByPattern basedir ?-regexp|-glob? ?--? patterns
::fileutil::foreachLine var filename cmd
::fileutil::grep pattern ?files?
::fileutil::install ?-m mode? source destination
::fileutil::stripN path n
::fileutil::stripPwd path
::fileutil::stripPath prefix path
::fileutil::jail jail path
::fileutil::touch ?-a? ?-c? ?-m? ?-r ref_file? ?-t time? filename ?...?
::fileutil::tempdir
::fileutil::tempdir path
::fileutil::tempdirReset
::fileutil::tempfile ?prefix?
::fileutil::maketempdir ?-prefix str? ?-suffix str? ?-dir str?
::fileutil::relative base dst
::fileutil::relativeUrl base dst
π DESCRIPTION π
Okay so this package is literally a whole set of implementations of standard unix utilities. It's giving toolbox energy, and the vibe check says we're about to go through every single one. Periodt.
::fileutil::lexnormalize path
This command performs purely lexical normalization on the path and returns the changed path as its result. Symbolic links in the path are not resolved. No cap.
Examples:
fileutil::lexnormalize /foo/./bar
=> /foo/bar
fileutil::lexnormalize /foo/../bar
=> /bar
::fileutil::fullnormalize path
This command resolves all symbolic links in the path and returns the changed path as its result. In contrast to the builtin file normalize, this command resolves a symbolic link in the last element of the path as well. Lowkey thorough. π
::fileutil::test path codes ?msgvar? ?label?
A command for the testing of several properties of a path. The properties to test for are specified in codes, either as a list of keywords describing the properties, or as a string where each letter is a shorthand for a property to test. The recognized keywords, shorthands, and associated properties are shown in the list below. The tests are executed in the order given to the command.
readβ file readablewriteβ file writableexistsβ file existsexecβ file executablefileβ file isfiledirβ file isdirectory
The result of the command is a boolean value. It will be true if and only if the path passes all the specified tests. In the case of the path not passing one or more test, the first failing test will leave a message in the variable referenced by msgvar, if such is specified. The message will be prefixed with label, if it is specified. Note that the variable referenced by msgvar is not touched at all if all the tests pass.
::fileutil::cat (?options? file)...
A Tcl implementation of the UNIX cat command. It returns the contents of the specified file(s). The arguments are files to read, with interspersed options configuring the process. If there are problems reading any of the files, an error will occur, and no data will be returned.
The options accepted are -encoding, -translation, -eofchar, and --. With the exception of the last, all options take a single value as argument, as specified by the Tcl builtin command fconfigure. The -- has to be used to terminate option processing before a file if that file's name begins with a dash.
Each file can have its own set of options coming before it, and for anything not specified directly the defaults are inherited from the options of the previous file. The first file inherits the system default for unspecified options.
::fileutil::writeFile ?options? file data
The command replaces the current contents of the specified file with data, with the process configured by the options. The command accepts the same options as ::fileutil::cat. The specification of a non-existent file is legal and causes the command to create the file (and all required but missing directories). It's giving "I got you, bestie" energy. β¨
::fileutil::appendToFile ?options? file data
This command is like ::fileutil::writeFile, except that the previous contents of file are not replaced, but appended to. The command accepts the same options as ::fileutil::cat.
::fileutil::insertIntoFile ?options? file at data
This command is similar to ::fileutil::appendToFile, except that the new data is not appended at the end, but inserted at a specified location within the file. In further contrast, this command has to be given the path to an existing file. It will not create a missing file, but throw an error instead.
The specified location at has to be an integer number in the range 0 ... [file size file]. 0 will cause insertion of the new data before the first character of the existing content, whereas [file size file] causes insertion after the last character of the existing content, i.e. appending.
The command accepts the same options as ::fileutil::cat.
::fileutil::removeFromFile ?options? file at n
This command is the complement to ::fileutil::insertIntoFile, removing n characters from the file, starting at location at. The specified location at has to be an integer number in the range 0 ... [file size file] - n. 0 will cause the removal of the new data to start with the first character of the existing content, whereas [file size file] - n causes the removal of the tail of the existing content, i.e. the truncation of the file.
The command accepts the same options as ::fileutil::cat.
::fileutil::replaceInFile ?options? file at n data
This command is a combination of ::fileutil::removeFromFile and ::fileutil::insertIntoFile. It first removes the part of the contents specified by the arguments at and n, and then inserts data at the given location, effectively replacing the removed content with data. All constraints imposed on at and n by ::fileutil::removeFromFile and ::fileutil::insertIntoFile are obeyed.
The command accepts the same options as ::fileutil::cat.
::fileutil::updateInPlace ?options? file cmd
This command can be seen as the generic core functionality of ::fileutil::replaceInFile. It first reads the contents of the specified file, then runs the command prefix cmd with that data appended to it, and at last writes the result of that invocation back as the new contents of the file.
If the executed command throws an error, the file is not changed.
The command accepts the same options as ::fileutil::cat.
::fileutil::fileType filename
An implementation of the UNIX file command, which uses various heuristics to guess the type of a file. Returns a list specifying as much type information as can be determined about the file, from most general (eg, "binary" or "text") to most specific (eg, "gif"). For example, the return value for a GIF file would be "binary graphic gif". The command will detect the following types of files: directory, empty, binary, text, script (with interpreter), executable elf, executable dos, executable ne, executable pe, graphic gif, graphic jpeg, graphic png, graphic tiff, graphic bitmap, html, xml (with doctype if available), message pgp, binary pdf, text ps, text eps, binary gravity_wave_data_frame, compressed bzip, compressed gzip, compressed zip, compressed tar, audio wave, audio mpeg, and link. It further detects doctools, doctoc, and docidx documentation files, and tklib diagrams. So many types, so little time. π
::fileutil::find ?basedir ?filtercmd??
An implementation of the unix command find. Adapted from the Tcler's Wiki. Takes at most two arguments, the path to the directory to start searching from and a command to use to evaluate interest in each file. The path defaults to ., i.e. the current directory. The command defaults to the empty string, which means that all files are of interest. The command takes care not to lose itself in infinite loops upon encountering circular link structures. The result of the command is a list containing the paths to the interesting files.
The filtercmd, if specified, is interpreted as a command prefix and one argument is added to it, the name of the file or directory find is currently looking at. Note that this name is not fully qualified. It has to be joined with the result of pwd to get an absolute filename.
The result of filtercmd is a boolean value that indicates if the current file should be included in the list of interesting files.
Example:
# find .tcl files
package require fileutil
proc is_tcl {name} {return [string match *.tcl $name]}
set tcl_files [fileutil::find . is_tcl]
::fileutil::findByPattern basedir ?-regexp|-glob? ?--? patterns
This command is based upon the TclX command recursive_glob, except that it doesn't allow recursion over more than one directory at a time. It uses ::fileutil::find internally and is thus able to and does follow symbolic links, something the TclX command does not do. First argument is the directory to start the search in, second argument is a list of patterns. The command returns a list of all files reachable through basedir whose names match at least one of the patterns. The options before the pattern-list determine the style of matching, either regexp or glob. glob-style matching is the default if no options are given. Usage of the option -- stops option processing. This allows the use of a leading - in the patterns.
::fileutil::foreachLine var filename cmd
okay bestie, this one is giving "read every line and do a lil something each time" energy π . The command reads the file filename and executes the script cmd for every single line in it. While the script is running, the variable var gets set to the contents of the current line. And the return value? It's whatever the last invocation of the script cmd handed back β or the empty string if the file was empty (rip, no lines, no vibes π).
::fileutil::grep pattern ?files?
It's literally grep, bestie. This is an implementation of grep, adapted from the Tcler's Wiki β¨. First argument = the pattern you're searching for. Then comes a list of files to search through. That list is optional β if you don't give one, it reads from stdin instead π«Ά. The result of the procedure is a list containing the matches. Each match is a single element of that list and it holds the filename, number, and contents of the matching line, all separated by colons.
::fileutil::install ?-m mode? source destination
This one is similar in functionality to the install command you'll find on many unix systems, or to the shell script distributed with a bunch of source distributions (like unix/install-sh in the Tcl sources, for example). It copies source β which can be either a file or a directory β to destination, which should be a directory, unless source is also a single file. The -m option lets you specify a unix-style mode (either octal or symbolic β see file attributes) π.
::fileutil::stripN path n
Removes the first n elements from the specified path and returns the modified path. If n is greater than the number of components in path, you get back an empty string (sad but true π). Wanna know how many components a given path has? Do llength on the list that file split returns, and there you go πΈ.
::fileutil::stripPwd path
If β and only if β the path is inside the directory returned by [pwd] (or is the current working directory itself), it gets made relative to that directory. In other words, the current working directory is stripped right off the path βοΈ. The possibly-modified path is returned as the result of the command. And if the current working directory itself was what you passed as path? The result is the string "." π
::fileutil::stripPath prefix path
If β and only if β the path is inside the directory "prefix" (or is the prefix directory itself), it gets made relative to that directory. In other words, the prefix directory is stripped off the path βοΈβ¨. The possibly-modified path comes back as the result of the command. If the prefix directory itself was passed as path, the result is the string ".".
::fileutil::jail jail path
This command makes sure that path is not escaping the directory jail π. It always returns an absolute path derived from path which is within jail.
- If path is an absolute path and already within jail, it is returned unmodified.
- An absolute path outside of jail is stripped of its root element and then put into the jail by prefixing it with it.
- The same happens if path is relative, except that nothing is stripped off it.
- Before adding the jail prefix, the path is lexically normalized so the caller can't sneak
..segments into path to escape the jail. no cap, that's the whole point π
::fileutil::touch ?-a? ?-c? ?-m? ?-r ref_file? ?-t time? filename ?...?
It's touch, bestie π. This alters the atime and mtime of the specified files.
-cβ do not create files if they don't already exist.-rβ use theatimeandmtimefrom ref_file.-tβ use the integer clock value time.-aβ only change theatime.-mβ only change themtime.
π¨ It is illegal to specify both-rand-tat the same time. pick a lane bestie.
β οΈ Heads up: this command is not available for Tcl versions less than 8.3.
::fileutil::tempdir
Returns the path of a directory where you can drop temporary files β like "/tmp" on Unix systems π. The algorithm it uses to find the right directory goes like this:
- The directory set by an invocation of
::fileutil::tempdirwith an argument. If this is present, it is tried exclusively and none of the following items are tried. - The directory named in the
TMPDIRenvironment variable. - The directory named in the
TEMPenvironment variable. - The directory named in the
TMPenvironment variable. - A platform specific location:
- Windows β
"C:\TEMP","C:\TMP","\TEMP", and"\TMP"are tried in that order. - (classic) Macintosh β the
TRASH_FOLDERenvironment variable is used. This is most likely not correct π. - Unix β the directories
"/tmp","/var/tmp", and"/usr/tmp"are tried in that order.
- Windows β
The algorithm utilized is mainly the one used in the Python standard library. The exception is the first item β the ability to have the search overridden by a user-specified directory β¨.
::fileutil::tempdir path
In this mode the command sets path as the first and only directory to try as a temp directory. See the previous item for how the set directory gets used. The command returns the empty string.
::fileutil::tempdirReset
Invoking this command clears the information set by the last call of [::fileutil::tempdir path]. Check out the last item too π«Ά.
::fileutil::tempfile ?prefix?
The command generates a temporary file name suitable for writing to, plus the associated file π. The file name will be unique, and the file will be writable and contained in the appropriate system specific temp directory. The name of the file comes back as the result of the command.
The code was taken from http://wiki.tcl.tk/772, attributed to Igor Volobouev and anon π.
::fileutil::maketempdir ?-prefix str? ?-suffix str? ?-dir str?
The command generates a temporary directory suitable for writing to. The directory name will be unique, and the directory will be writable and contained in the appropriate system specific temp directory. The name of the directory is returned as the result of the command π.
The three options can be used to tweak the behaviour of the command:
-prefixstr β the initial, fixed part of the directory name. Defaults totmpif not specified.-suffixstr β the fixed tail of the directory. Defaults to the empty string if not specified.-dirstr β the directory to place the new directory into. Defaults to the result offileutil::tempdirif not specified.
The initial code for this was supplied by Miguel Martinez Lopez [mailto:aplicacionamedida@gmail.com] πΈ.
::fileutil::relative base dst
This command takes two directory paths β both either absolute or relative β and computes the path of dst relative to base β¨. That relative path is returned as the result of the command. As implied in the previous sentence, the command is not able to compute this relationship between the arguments if one of the paths is absolute and the other relative. pick one vibe, bestie.
π Note: The processing done by this command is purely lexical. Symbolic links are not taken into account.
::fileutil::relativeUrl base dst
This command takes two file paths β both either absolute or relative β and computes the path of dst relative to base, as seen from inside of base π¦. This is the algorithm a browser uses to resolve a relative link found in the currently shown file.
The computed relative path is returned as the result of the command. As implied in the previous sentence, the command is not able to compute this relationship between the arguments if one of the paths is absolute and the other relative.
π Note: The processing done by this command is purely lexical. Symbolic links are not taken into account.
β οΈ WARNINGS AND INCOMPATIBILITIES β οΈ
1.14.9 In this version fileutil::find's broken system for handling symlinks was replaced with one working correctly and properly enumerating all the legal non-cyclic paths under a base directory.
While correct, this means that certain pathological directory hierarchies with cross-linked sym-links will now take about O(n**2) time to enumerate, whereas the original broken code managed O(n) due to its brokenness π. yes, the old bug was lowkey a performance feature. iconic behaviour.
A concrete example and extreme case is the "/sys" hierarchy under Linux, where some hundred devices exist under both "/sys/devices" and "/sys/class" with the two sub-hierarchies linking to the other, generating millions of legal paths to enumerate π. The structure, reduced to three devices, roughly looks like
/sys/class/tty/tty0 --> ../../dev/tty0
/sys/class/tty/tty1 --> ../../dev/tty1
/sys/class/tty/tty2 --> ../../dev/tty1
/sys/dev/tty0/bus
/sys/dev/tty0/subsystem --> ../../class/tty
/sys/dev/tty1/bus
/sys/dev/tty1/subsystem --> ../../class/tty
/sys/dev/tty2/bus
/sys/dev/tty2/subsystem --> ../../class/tty
The command fileutil::find currently has no way to escape this. When having to handle such a pathological hierarchy, it is recommended to switch to package fileutil::traverse and the same-named command it provides, and then use the -prefilter option to prevent the traverser from following symbolic links, like so π«Ά:
package require fileutil::traverse
proc NoLinks {fileName} {
if {[string equal [file type $fileName] link]} {
return 0
}
return 1
}
fileutil::traverse T /sys/devices -prefilter NoLinks
T foreach p {
puts $p
}
T destroy
ππ BUGS, IDEAS, FEEDBACK ππ
okay bestie, real talk for a sec. This document β and the whole package it's describing β will undoubtedly contain bugs and other problems. Like, no cap, that's just how software is. It's not a vibe failure, it's a fact of life. π
So if you find something janky, please report it β and here's exactly where:
- Head to the category
fileutilof the Tcllib Trackers athttp://core.tcl.tk/tcllib/reportlistand drop it there. π«Ά
And not just bugs, queen β if you have ideas for enhancements you want for the package and/or the documentation, send those too. Manifesting better docs is a group project. β¨
π οΈ if you're sliding in with code changes
When you're proposing code changes, please provide unified diffs β aka the output of diff -u. That's the format we can actually read, so it's giving helpful. π
β οΈ Also note, and this one matters: attachments are strongly preferred over inlined patches. Like, strongly. Don't paste the whole thing into the message body, babe, it's a mess.
How to attach, step by step, because I got you:
- Create the ticket first. π
- Immediately after its creation, go to the Edit form of the ticket.
- Use the left-most button in the secondary navigation bar β that's the one, that's the button. π
Do that and the maintainers will be obsessed with you. π
β¨ KEYWORDS β¨
cat, file utilities, grep, temp file, test, touch, type
like, that's the whole search-engine aura of this page. iykyk. ππ
π CATEGORY π
Programming tools β yeah bestie, we live in the tools section. very main character of us. π
and that's the whole page, babes!! π¦ from the fine folks behind tcllib, version 1.16.3 β this was fileutil(n), and honestly? she ate. go touch some files, safely. mwah. ππ
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms