fileutil(3tcl)

πŸ“¦
version 2.0 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸŽ€ NAME πŸŽ€

fileutil β€” procedures implementing some file utilities

(also, hi, this is the fileutil(3tcl) page, we're doing this together bestie πŸ’–)


✨ 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 toolbox of implementations of standard unix utilities, bestie. That's the vibe. Let's go one by one, no skips, no cap. 🌸


πŸ¦‹ ::fileutil::lexnormalize path

This one does purely lexical normalization on the path and hands you back the changed path as its result. Important: symbolic links in the path are not resolved. Like, it's just doing the text math, it is not out here chasing symlinks. πŸ™ˆ

Examples, so you can see it:

fileutil::lexnormalize /foo/./bar
=> /foo/bar

fileutil::lexnormalize /foo/../bar
=> /bar

🧚 ::fileutil::fullnormalize path

This one resolves all symbolic links in the path and returns the changed path as its result. And here's the tea β€” in contrast to the builtin file normalize, this command resolves a symbolic link in the last element of the path as well. That's the whole difference, and it's iconic of it tbh. πŸ’…


🌈 ::fileutil::test path codes ?msgvar? ?label?

A whole command dedicated to testing several properties of a path. Slay. The properties you want to test go in codes, and you can give them either as:

  • a list of keywords describing the properties, or
  • a string where each letter is a shorthand for a property to test

The recognized keywords, shorthands, and their associated properties are in the list below, and the tests get executed in the order given to the command. Order matters, bestie, don't forget.

  • read β€” file readable
  • write β€” file writable
  • exists β€” file exists
  • exec β€” file executable
  • file β€” file isfile
  • dir β€” file isdirectory

The result of the command is a boolean value, and it's true if and only if the path passes all the specified tests. If the path doesn't pass one or more tests, the first failing test will leave a message in the variable referenced by msgvar, if such is specified. That 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. Not even a little. Zero contact. πŸ’€

πŸ“ ::fileutil::cat (?options? file)...

A Tcl implementation of the UNIX cat command, and honestly? She ate. 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
  • --

With the exception of the last one, 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. (Classic dash shenanigans, iykyk. 😭)

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. Chain of inheritance, main character energy fr fr. πŸ‘‘


πŸ’– ::fileutil::writeFile ?options? file data

The command replaces the current contents of the specified file with data, with the process configured by the options. It accepts the same options as ::fileutil::cat.

And here's the cute part β€” 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'll build the whole path for you, don't even worry." 🫢


πŸ¦‹ ::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 β€” it'll throw an error instead. No shortcuts, bestie. πŸ™…

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. So like, both rulebooks apply, don't try to sneak around them. 😌

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 invokation back as the new contents of the file.

If the executed command throws an error, the file is not changed. Safety first, always. πŸ›‘οΈ

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". It's giving detective work and honestly she's so good at it. πŸ•΅οΈβ€β™€οΈ

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
  • link

It further detects doctools, doctoc, and docidx documentation files, and tklib diagrams. Obsessed. πŸ’–


🧚 ::fileutil::find ?basedir ?filtercmd??

An implementation of the unix command find. Adapted from the Tcler's Wiki. Takes at most two arguments:

  1. the path to the directory to start searching from, and
  2. 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. (Iconic behavior, honestly, so many tools just fall apart there. πŸ’€)

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. Slay, upgrade. ✨

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. (So you can be a little chaotic with your filenames and she'll still handle it. 🫢)

πŸ¦‹ ::fileutil::foreachLine β€” the line-by-line bestie

okay so this one is literally the "read every line and do a thing" queen. ::fileutil::foreachLine takes a var, a filename, and a cmd. It reads the file at filename and runs the script cmd for every single line in that file. While the script is running, the variable var gets set to the contents of the current line. And the return value? It's the result of the last invocation of the script cmd, or the empty string if the file was empty. periodt. ✨


πŸ’… ::fileutil::grep β€” grep but make it Tcl

::fileutil::grep pattern ?files? is a whole implementation of grep, adapted from the Tcler's Wiki. The first argument is the pattern to search for. After that comes a list of files to search through. That list is optional β€” and if you skip it, stdin gets used instead, no cap. 🫢

The result of the procedure is a list containing the matches. Each match is a single element of the list and contains the filename, number and contents of the matching line, separated by colons. iykyk.


πŸŽ€ ::fileutil::install β€” install, but ours

::fileutil::install ?-m mode? source destination is similar in functionality to the install command found on many unix systems, or the shell script distributed with many source distributions (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 β€” bye bye, first n bits

::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, an empty string is returned. 🌸

The number of components in a given path may be determined by performing llength on the list returned by file split.


🧚 ::fileutil::stripPwd β€” pwd? stripped.

::fileutil::stripPwd path β€” if, and only if, the path is inside of the directory returned by [pwd] (or the current working directory itself) it gets made relative to that directory. In other words, the current working directory is stripped from the path. πŸ’–

The possibly modified path is returned as the result of the command. And if the current working directory itself was specified for path, the result is the string ".".


🌈 ::fileutil::stripPath β€” same energy, but with a prefix

::fileutil::stripPath prefix path β€” if, and only if, the path is inside of the directory "prefix" (or the prefix directory itself) it gets made relative to that directory. In other words, the prefix directory is stripped from the path. πŸ¦‹

The possibly modified path is returned as the result of the command. And if the prefix directory itself was specified for path, the result is the string ".".


πŸ”₯ ::fileutil::jail β€” keep it contained, bestie

::fileutil::jail jail path makes sure that the path is not escaping the directory jail. It always returns an absolute path derived from path which is within jail. no escaping on my watch πŸ’€

  • 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 of it.
  • Before adding the jail prefix, the path is lexically normalized to prevent the caller from using .. segments in path to escape the jail. (sneaky sneaky, not today 🚫)

πŸ‘‘ ::fileutil::touch β€” the icon

::fileutil::touch ?-a? ?-c? ?-m? ?-r ref_file? ?-t time? filename ?...?

This is an implementation of touch. It alters the atime and mtime of the specified files. Let's break down the flags, bestie:

  • If -c, do not create files if they do not already exist.
  • If -r, use the atime and mtime from ref_file.
  • If -t, use the integer clock value time.
  • It is illegal to specify both -r and -t. (pick a lane πŸ’…)
  • If -a, only change the atime.
  • If -m, only change the mtime.
⚠️ This command is not available for Tcl versions less than 8.3.

🌸 ::fileutil::tempdir β€” where the temp files live

The command returns the path of a directory where the caller can place temporary files, such as "/tmp" on Unix systems. The algorithm we use to find the correct directory is as follows:

  1. The directory set by an invokation of ::fileutil::tempdir with an argument. If this is present it is tried exclusively and none of the following items are tried.
  2. The directory named in the TMPDIR environment variable.
  3. The directory named in the TEMP environment variable.
  4. The directory named in the TMP environment variable.
  5. A platform specific location:
    • Windows β€” "C:\TEMP", "C:\TMP", "\TEMP", and "\TMP" are tried in that order.
    • (classic) Macintosh β€” The TRASH_FOLDER environment variable is used. This is most likely not correct.
    • Unix β€” The directories "/tmp", "/var/tmp", and "/usr/tmp" are tried in that order.

The algorithm utilized is mainly that 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 β€” you're the boss now

In this mode the command sets the path as the first and only directory to try as a temp. directory. See the previous item for the use of the set directory. The command returns the empty string. 🫢


πŸ’œ ::fileutil::tempdirReset β€” nvm, forget that

Invoking this command clears the information set by the last call of [::fileutil::tempdir path]. See the last item too.


πŸ¦‹ ::fileutil::tempfile β€” a fresh new file just for you

::fileutil::tempfile ?prefix? generates a temporary file name suitable for writing to, and 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 will be returned as the result of the command. ✨

The code was taken from http://wiki.tcl.tk/772, attributed to Igor Volobouev and anon. πŸ’–


🌈 ::fileutil::maketempdir β€” temp dir speedrun

::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 will be returned as the result of the command.

The three options can be used to tweak the behaviour of the command:

  • -prefix str β€” The initial, fixed part of the directory name. Defaults to tmp if not specified.
  • -suffix str β€” The fixed tail of the directory. Defaults to the empty string if not specified.
  • -dir str β€” The directory to place the new directory into. Defaults to the result of fileutil::tempdir if not specified.

The initial code for this was supplied by Miguel Martinez Lopez [mailto:aplicacionamedida@gmail.com]. πŸ‘‘


🧚 ::fileutil::relative β€” the relative-path girlie

::fileutil::relative base dst takes two directory paths β€” both either absolute or relative β€” and computes the path of dst relative to base. This 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.

πŸ’… ::fileutil::relativeUrl β€” same, but for your browser era

::fileutil::relativeUrl base dst takes two file paths β€” both either absolute or relative β€” and computes the path of dst relative to base, as seen from inside of the base. This is the algorithm how a browser resolves 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. 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(3tcl) due to its brokenness. 😭

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 doc? and the whole package it's describing? yeah, they will have bugs. literally no cap. bugs, glitches, weird little problems, all of it. it's giving "software," you know? 😭

so if you find one, pls pls pls report it in the category fileutil of the Tcllib Trackers over at [http://core.tcl.tk/tcllib/reportlist]. πŸŽ€

and not just bugs β€” if you've got ideas for enhancements, whether it's for the package itself or for this documentation, drop those there too. both are welcome, fr fr. ✨

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

and heads up on this one: attachments are strongly preferred over inlined patches. like, lowkey a big deal. πŸ’… here's how you do it:

  1. make your ticket 🌸
  2. go straight to the Edit form of the ticket, immediately after its creation
  3. use the left-most button in the secondary navigation bar to attach your stuff πŸ¦‹

boom. done. iconic.


🏷️ KEYWORDS 🏷️

  • cat
  • file utilities
  • grep
  • temp file
  • test
  • touch
  • type

πŸ‘‘ CATEGORY πŸ‘‘

Programming tools


and that's the whole vibe check, bestie πŸ’œ this page ships with tcllib 1.16.3, and the page itself is dated fileutil(3tcl) β€” go forth and cat, grep, touch, and slay πŸ”₯πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from