I am using cygwin and have a lot of files in which I want to convert tabs to spaces (four spaces per tab) and remove all space characters that exist at end of line. Is there a utility for this in Linux?

  • 2
    You are searching for sed
    – Robert
    Jun 23, 2021 at 1:43

1 Answer 1


sed is your friend for that. Comes for free with all Linux distros, as far as I know. It's the "Stream EDitor", and works with regular expressions. To apply it to your example:

sed -E 's/\t/    /g;s/\s*($)/\1/g' infile outfile

Each tab (\t) is replaced by 4 spaces, and trailing spaces (actually, all whitespace characters up to the end of the line ($) are removed. If you don't want the result in a new file, you can tell sed to do the replacements inline (-i):

sed -i -E 's/\t/    /g;s/\s*($)/\1/g' file

In a lot of files? No prob:

sed -i -E 's/\t/    /g;s/\s*($)/\1/g' *

(just pass a "mask", like *.txt or whatever files should be matched).

You can get sed from your distro's package management, e.g. apt install sed.

  • What language is 's/\t/ /g;s/\s*($)/\1/g'? Is that a regular expression? I don't see the digit 4 anywhere in the regular expression. What is going on here?
    – quantum231
    Jun 24, 2021 at 15:02
  • Yes, it's regular expression. And no, the question wasn't asking for digits – but the replacement part has 4 spaces as requested. Just try it out with some sample files (I did, and it does what you asked for). The trailing /g makes sure it's applied Globally, so all tabs are replaced by 4 spaces each.
    – Izzy
    Jun 25, 2021 at 7:20
  • I am just curious to know, how did you specify the "4 spaces" part?
    – quantum231
    Jun 25, 2021 at 10:42
  • By using 4 spaces? The big gap following the \t/ are 4 spaces. sed syntax is s/<RegExToMatch>/<replacement>/<op>, basically meaning search for this <RegEx> and replace it according to <replacement> applying <op>. You can concatenate multiple operations using a ;. So taking the first part, it means search for a \tab, replace it by 4 spaces, globally. The second part is search for white-\spaces up to the EOL ($) and replace that with the EOL (back reference: \1 is the first (match)).
    – Izzy
    Jun 25, 2021 at 21:13

Your Answer

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

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