# fileutil(n) 1.4 fileutil "file utilities"

## NAME

fileutil - Procedures implementing some file utilities

## SYNOPSIS

package require Tcl 8
package require fileutil ?1.4?

::fileutil::cat filename
::fileutil::fileType filename
::fileutil::find ?basedir ?filtercmd??
::fileutil::findByPattern basedir ?-regexp|-glob? ?--? patterns
::fileutil::foreachLine var filename cmd
::fileutil::grep pattern ?files?
::fileutil::stripN path n
::fileutil::stripPwd path
::fileutil::touch ?-a? ?-c? ?-m? ?-r ref_file? ?-t time? filename ?...?

## DESCRIPTION

This package provides implementations of standard unix utilities.

::fileutil::cat filename
  A tcl implementation of the UNIX cat command. Returns the contents of the specified file. The first argument is the name of the file to read.
::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, &quot;binary&quot; or &quot;text&quot;) to most specific (eg, &quot;gif&quot;). For example, the return value for a GIF file would be &quot;binary graphic gif&quot;. The command will detect the following types of files: directory, empty, binary, text, script (with interpreter), executable elf, graphic gif, graphic jpeg, html, xml (with doctype if available), message pgp, and link.
::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 loose itself in infinite loops upon encountering circular link structures. The result of the command is a list containing the paths to the interesting files.
::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
  The command reads the file filename and executes the script cmd for every line in the file. During the execution of the script the variable var is set to the contents of the current line. The return value of this command is the result of the last invocation of the script cmd or the empty string if the file was empty.
::fileutil::grep pattern ?files?
  Implementation of grep. Adapted from the Tcler's Wiki. The first argument defines the pattern to search for. This is followed by a list of files to search through. The list is optional and stdin will be used if it is missing. The result of the procedures is a list containing the matches. Each match is a single element of the list and contains filename, number and contents of the matching line, separated by a colons.
::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.
::fileutil::stripPwd path
  If the path is inside of the directory returned by [pwd] it is 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.
::fileutil::touch ?-a? ?-c? ?-m? ?-r ref_file? ?-t time? filename ?...?
  Implementation of touch. Alter the atime and mtime of the specified files. 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. If -a, only change the atime. If -m, only change the mtime.

## KEYWORDS

file utilities