search and replace in files

By: Johnathon Wright on: September 17, 2009

For all files under app/views, replace all tabls with two spaces.

find app/views -type f -print0 | xargs -0 sed -i 's/\t/ /g'

broken down:

find app/views -type f

'find' lists all the files (with relative path) at or below the specified path. 'type -f' gives you 'files' ie not directories. It also exclude symlinks, etc. [ see "list of file types": ]

'-print 0' is a hack for cross-system compatibility issues. [ see "documentation on -print option": File-Name ]

xargs -0 ...

xargs simply takes whatever list is passed to it and executes an executable you specify once for each line it receives. the -0 tells xargs to handle the result of find -print0

sed -i

the -i argument means to edit the file inline... ie overwrite it rather than writing the contents to another file. " "Danger Will Robinson!": " ... this will rewrite your files ... if you screw this up and aren't using a repository, this could be a disaster for you.


this is the same substitution format as found in vim. replace regex at 'needle' with string 'replacement'. /s is substitute, g is global, ie not just the first occurrence.


Just checking that you are human. What would be the result of this code?

a = 3*(4/2); b = 1; a+b