Question

I am looking for a simple command line script or shell script I can run that, with a given directory, will traverse all directories, sub directories and so on looking for files with .rb and indent them to two spaces, regardless of current indentation.

It should then look for html, erb and js files (as well as less/sass) and indent them to 4.

Is this something thats simple or am I just over engineering it? I dont know bash that well, I have tried to create something before but my friend said to use grep and I am lost. any help?

Was it helpful?

Solution

If you've got GNU sed with the -i option to overwrite the files (with backups for safety), then:

find . -name '*.rb' -exec sed -i .bak  's/^/  /' {} +

find . -name '*.html' -exec sed -i .bak 's/^/    /' {} +

Etc.

The find generates the list of file names; it executes the sed command, backs up the files (-i .bak) and does the appropriate substitutions as requested. The + means 'do as many files at one time as is convenient. This avoids problems with spaces in file names, amongst other issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top