21

How would one go about using sed in order to insert

rm -rf

at the start of each line of a file?

THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64

3 Answers3

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
mikerobi
  • 20,527
  • 5
  • 46
  • 42
  • @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 02 '10 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
user692942
  • 16,398
  • 7
  • 76
  • 175
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 
Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25