21

How would one go about using sed in order to insert

rm -rf

at the start of each line of a file?

3 Answers 3

52
sed 's/^/rm -rf /' filename

EDIT

Xargs would be simpler way to delete all of the files listed in another file

xargs -a filename rm -rf
1
  • @Dennis Williamson, nice tip, I pretty much only ever call xargs, when I'm piping in output from another program, so I would never have bothered figuring that out.
    – mikerobi
    Nov 2, 2010 at 18:50
0

Sometimes you have to go with the original method using sed, especially when you want to do things like

csf -d ipaddr.

xargs doesn't seem to like the output created by some commands and gives up after the first line. ie:

sed 's/^/csf -d /' hacks >>hacks.sh
0

You should use

    #if you want to add only to each line matching string
    sed 's/\(^matchingstring.*$\)/"yourtext" \1/' myfile.txt
    
   #if you want to add to each line
    sed 's/^/"yourtext"/' myfile.text 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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