Dustin Getz
Dustin Getz

Reputation: 21801

how do i convert tabs to spaces on many files with bash

How can I convert tabs to spaces in in all .js files in a directory in one command?

Upvotes: 0

Views: 463

Answers (3)

beroe
beroe

Reputation: 12316

Simpler syntax:

for F in *.js; do sed -iE 's|\t|    |g' $F; done

(Caution, edits files in place.) Could be made to rename edited copy, or placed into a function if you do this often.

Upvotes: 0

konsolebox
konsolebox

Reputation: 75548

This would convert tabs to four spaces:

find /path/to/directory -type f -iname '*.js' -exec sed -ie 's|\t|    |g' '{}' \;

Change the space part in sed between the next two | to have a custom number of spaces you like.

Another way is to process all files to one sed call at once with +:

find /path/to/directory -type f -iname '*.js' -exec sed -ie 's|\t|    |g' '{}' '+'

Just consider the possible limit of arguments to a command by the system.

Upvotes: 1

Dustin Getz
Dustin Getz

Reputation: 21801

find . -type f -iname "*.js" -print0 | xargs -0 -I _FILE_ tab2space _FILE_ _FILE_

Upvotes: 1

Related Questions