Find and replace action accross many files/ folders


Two Wire

Recommended Posts

Does anyone know of a program that will search a directory and its sub directories for a string in xml files and replace it with another string.

 

I have many folders to search, each containing two/three xml files. What I am looking for is a 'find and replace' action to an identified file and then moving  on to the next file or sub folder.

Link to comment

Linux just happens to have such a function, it's called find!  If you look for examples, you will often see it with exec and a command that is executed for each file that matches the filespec.  sed is the command that will find and replace strings within a file.  We have some excellent gurus among us that could give you exact syntax, I'm not one of them though!  find and sed are very powerful, should do anything you want.  Google their man files - 'man find' and 'man sed'.

Link to comment

I have some unRAID shares mapped to my Windows machine, it allows me to run windows utilities on them.

 

I am using a tool called "Find And Replace Text" which abbreviates to the nice word FART (but don't get fooled by its name).

 

FART is CLI command tool with powerful find and replace functionality. See this side for a download

 

Below the help screen of FART

Usage: FART [options] [--] <wildcard>[,...] [find_string] [replace_string]

Options:
-h, --help          Show this help message (ignores other options)
-q, --quiet         Suppress output to stdio / stderr
-V, --verbose       Show more information
-r, --recursive     Process sub-folders recursively
-c, --count         Only show filenames, match counts and totals
-i, --ignore-case   Case insensitive text comparison
-v, --invert        Print lines NOT containing the find string
-n, --line-number   Print line number before each line (1-based)
-w, --word          Match whole word (uses C syntax, like grep)
-f, --filename      Find (and replace) filename instead of contents
-B, --binary        Also search (and replace) in binary files (CAUTION)
-C, --c-style       Allow C-style extended characters (\xFF\0\t\n\r\\ etc.)
     --cvs           Skip cvs dirs; execute "cvs edit" before changing files
     --svn           Skip svn dirs
     --remove        Remove all occurences of the find_string
-a, --adapt         Adapt the case of replace_string to found string
-b, --backup        Make a backup of each changed file
-p, --preview       Do not change the files but print the changes

 

In your case a command like (where d: is the mapped share folder) can do the trick

fart -r d:\myfiles\*.xml "old string" "new string"

 

Link to comment

If you're not into getting down and dirty with the command line, then you could try either the Dolphin or Krusader docker containers that provide a GUI to the file system.

 

I've played with both of them myself but always find myself back to using a combination of command line and Midnight Commander..

 

But may fit the bill for you..

Link to comment

I have some unRAID shares mapped to my Windows machine, it allows me to run windows utilities on them.

 

I am using a tool called "Find And Replace Text" which abbreviates to the nice word FART (but don't get fooled by its name).

 

FART is CLI command tool with powerful find and replace functionality. See this side for a download

 

There's something fitting mentioning a Windows solution called FART on a Linux based forum..  ;D

(I'm a Windows user as well as Linux though - no hate here!)

Link to comment

Hey guys, thanks for all the input. I will look at these options to see which one I feel comfortable using. As I probably have over 1000 files to which I need to change a path string, I'm hopping to save hours/days using one of these options. Fart looks interesting, especially when the command line was given.

 

Thanks. again

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.