Menu

Directory recursion

Examples
2019-02-22
2019-02-23
  • David McCracken

    David McCracken - 2019-02-22

    This example is from https://stackoverflow.com/questions/245840/rename-files-in-sub-directories. The question is "Is there any way of batch renaming files in sub directories?" for example *.html to *.htm. The operating system is not specified. rene can do this in Windows and Linux. The option is -R. Without qualifying parameters, rene will apply the rename to the current directory and every sub-directory. Parameters can limit the depth of recursion and exclude (or include) specified directory names.
    rene *.html *.htm -R
    would achieve the stated goal. However, in most situations -R parameters are helpful. For example:
    rene *.html *.htm -R/.*/back/temp
    excludes all dot prefix directories and those named back or temp.
    rene's single-directory undo option -U reverses the last set of changes in one directory. -UR reverses them in all directories affected by recursive rename.
    By default, rene shows all of the changes that would be made in each directory and asks whether to do them. Pressing just the Y key (direct console input in Windows and Linux avoids having to press Y and then Enter) is not inconvenient for a single directory but can become tedious in recursion, especially for an unambiguous rename such as this example. rene's action option -AR tells it to rename without asking (it still shows the changes). If renaming doesn't produce the expected results, the undo -UR option conveniently restores the original names.

     

    Last edit: David McCracken 2019-02-23
  • David McCracken

    David McCracken - 2019-02-23

    This example is from my own experience and was one of the motivations for writing rene. I had a large website with hundreds of pages and a deep tree where both directories and files had camel-case names. I had developed this in Windows and it was hosted on a Windows server. Hundreds of links broke when I moved it to a Linux server because case-insensitive Windows allowed me to be very casual about case. For example, an html file would have a link including the directory "recentPublications" but the directory was actually named "RecentPublications". I decided to not just correct the links but to eliminate the ambiguity once and for all by following Mozilla's recommendation to use only lower-case and no underscore (I had many of these as well).
    rene -SL/_-/ -Fd -R -AR
    updated the entire tree in a couple of seconds.
    -S is rene's substitution mode option. -SL/_-/ says to lower-case letters and replace _ with -
    -F is the filter option.-Fd says to rename directories as well as files (default is only files)
    -R tells to recurse the entire tree
    -AR tells to make the changes without asking
    I still had to edit the hundreds of files with failing links but I was using emacs and wrote a macro to automatically do that recursively, fixing everything in a few seconds.
    rene's bottom-up (leaves-to-root) recursive undo -UR works even when directories are renamed.

     

    Last edit: David McCracken 2019-02-23

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.