Tag Archives: sed

replacing text in multiple (non-hidden) files and file names

I want to change ‘x_y’ in numerous files and filenames to ‘xy’ or something like that.

The condition is that I want to do it only in the non-hidden files, as I have got some svn folders that I do not want to meddle with

CHANGING FILE NAMES
So, first to select all the files recursively in a folder (assuming you are already in the folder you want to search for) and rename the files. the results of find are piped to rename using xargs. The -n in rename is if you want a test run. Change it to -v for actual renaming

find . \( ! -regex '.*/\..*' \) -type f | xargs rename -n 's/x_y/xy/g'

REPLACING THE TEXT INSIDE FILES
First, I want to see which files have ‘x_y’ in the files (only non-hidden)
find . \( ! -regex '.*/\..*' \) -type f | xargs grep -l 'x_y'

IF THE LIS OF FILES IS SATIASFACTORY, TIME TO CHANGE THE TEXT (I take these precautions as this is the first time and don’t want to corrupt files and lose my work, spend another day retrieving them-if that is possible at all)
find . \( ! -regex '.*/\..*' \) -type f | xargs sed -i 's/x_y/xy/g'

Advertisements