I have a text file fileslist containing a list of names of files, or rather relative paths to those files and their names from a certain parent directory. There is only one file per line in fileslist. From that parent directory I want to run a command on every single file one by one. This command should search the entire every single one of those files for a pattern, and replace it with another pattern. Each of these text files may or may not contain the pattern College Documents. I want to replace it with College_Documents. How can I do that?


After some researching, I found that this command works well for my purposes.

cat fileslist | while read in; do sed -i 's/College Documents/College_Documents/g' $in; done

I'd recommend xargs for this:

xargs sed -i 's/College Documents/College_Documents/g' < filelist

This tells xargs to read its input from filelist (where it would normally read from stdin; we're giving it the contents of the file, instead). It fills up sed's parameter list with as many files as will fit, executing for example:

sed -i 's/College Documents/College_Documents/g' file1 file2 file3 ...
sed -i 's/College Documents/College_Documents/g' filen filem fileo ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.