Combinations of the
sed commands allow you to recursively search for, and replace, strings of text in your files. The
find command prints out names of files, and the
xargs command reads them and passes them as arguments to another command (e.g.
sed). In order to handle filenames with spaces and other special characters in them, the options
-0 are used.
For example, we're currently migrating home directories to new filesystems, and users with
/n/home hardcoded in scripts will have to modify them. In the following, the string
/n/home/$USER\b is a regular expression that matches the string
$USER will automatically be filled in by your username, followed by a word boundary (i.e., if my username is
joe, it won't match
To recursively search all the files in the current working directory for all occurrences of your former home directory explicity written out, you can use this command:
find . -type f -print0 | xargs -0 grep --color "/n/home/$USER\b"
grep command searches text for strings matching regular expressions.
Add the option
grep if you only want to list the names of the files that match, as opposed to print the full line of text that contains the match.
To replace all those occurrences with the string
~, you can use the following:
find . -type f -print0 | xargs -0 sed -i "s?/n/home/$USER\b?~?g"
sed command is used to make the text substitution — the stuff between the first two ?s is what to replace, and the stuff between the second two ?s is what to replace it with. The
g after that says replace all occurences, not just the first on each line.
As with any operation that could modify all your files, use this with care, maybe on some test files first, to make sure it's doing what you expect it to do.
-exec option, which you may see documented in other contexts, is an alternative to combining it with
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Permissions beyond the scope of this license may be available at Attribution.