How to search directory for specific files?

How to search a a directory tree in TCL for files that end in .so or .a and build a list of those directories

  • I'm trying to write a set of TCL scripts that helps setup a user's environment for a set of libraries that are not in their standard LD_LIBRARY_PATH as part of a support solution. Since the system is rather sparse in terms of what I can install, I don't really have access to any TCL extensions and am trying to do this in base TCL as much as possible. I'm also relatively new to TCL as a language. What I'd like to do is search through a directory structure and locate the directories that have .so and .a files in them, build a list of those, and, eventually add them to the user's $LD_LIBRARY_PATH variable. If I were doing this in a shell, I'd just use find with something like this: find /dir -type f \( -name "*.so" -o -name "*.a" \) | awk -F/ 'sub(FS $NF,x)' | sort -u I could hard-code the paths, but we want a single set of scripts that can manage several different applications. Any ideas would be very much appreciated.

  • Answer:

    http://core.tcl.tk/tcllib/home has a http://core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/modules/fileutil/fileutil.html module that does a recursive find: package require fileutil set filenames [::fileutil::findByPattern /dir -glob {*.so *.a}] foreach filename $filenames { if {[file isfile $filename]} { set dirs([file dirname $filename]) 1 } } puts [array names dirs] If you want to use this, but can't install something, you can just take the procedures and add them to your code (with the appropriate attribution) -- http://core.tcl.tk/tcllib/dir?ci=63d99a74f4600441&name=modules/fileutil Otherwise, just call the system's find command and parse the output (assume that your filenames do not contain newlines). set filenames [split [exec find /dir -type f ( -name *.so -o -name *.a )] \n] And the loop to extract the unique directories is similar. As a side benefit, the find invocation is actually easier to read because you don't have to escape all the chars that are special to the shell.

stareja at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.