{"id":4940,"date":"2018-06-18T20:45:43","date_gmt":"2018-06-18T16:45:43","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-unexpand-command\/"},"modified":"2018-06-18T20:45:43","modified_gmt":"2018-06-18T16:45:43","slug":"linux-unexpand-command-explained-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-unexpand-command-explained-for-beginners-with-examples\/","title":{"rendered":"Linux unexpand Command Explained for Beginners (with Examples)"},"content":{"rendered":"<p>Working on the Linux command line requires to learn how to quickly perform some repetitive tasks. One such task that you may find yourself involved in sooner or later is converting blanks to tabs or vice-versa. In this tutorial, we will discuss a command line utility &#8211; dubbed <strong>unexpand<\/strong> &#8211; that&#8217;ll be of great help in such situations.\u00a0<\/p>\n<p>But before we start, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-unexpand-command\">Linux unexpand command<\/h2>\n<p>The unexpand command lets you convert spaces into tabs. Following is its syntax:<\/p>\n<p class=\"command\">unexpand [OPTION]&#8230; [FILE]&#8230;<\/p>\n<p>And here&#8217;s how the man page describes it:<\/p>\n<pre>Convert blanks in each FILE to tabs, writing to standard output. With no FILE, or when FILE is -, read standard input.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the unexpand command works.<\/p>\n<h2 id=\"q-how-to-use-unexpand-command\">Q1. How to use unexpand command?<\/h2>\n<p>Suppose you have a file with text containing tabs. Now, for whatever reason, you convert those tabs into spaces using the <a href=\"https:\/\/www.howtoforge.com\/linux-expand-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">expand command<\/a>, and now you want to convert spaces into tabs again, then in this case, you can use the unexpand command.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">unexpand file1<\/p>\n<p>The tool produces the converted output on stdout. So to save it, \u00a0you can redirect it to some file. For example:<\/p>\n<p class=\"command\">unexpand file1 &gt; file2<\/p>\n<p>It&#8217;s worth mentioning that by default this tool only works on initial blanks. You can use the -a option to tell the utility that you want to convert all spaces\/blanks into tabs.<\/p>\n<h2 id=\"q-how-to-force-unexpand-convert-only-initialleading-blanks\">Q2. How to force unexpand convert only initial\/leading blanks?<\/h2>\n<p>The tool provides an option that forces it to convert only initial blanks or spaces (even if the -a option is present). The option in question is <strong>&#8211;first-only<\/strong>.<\/p>\n<p class=\"command\">unexpand &#8211;first-only -a file1 &gt; file2<\/p>\n<h2 id=\"q-what-the-t-option-does\">Q3. What the -t option does?<\/h2>\n<p>Well, the -t option has multiple roles. It accepts either a single number, or a comma-separated list of numbers. When you pass a single number (say &#8216;n&#8217;), the unexpand command makes sure it compresses spaces in the file into tabs every &#8216;n&#8217; positions.<\/p>\n<p>For example, to make the tool compress blanks into tabs every 3 positions, use this option in the following way:<\/p>\n<p class=\"command\">unexpand -t 3 file1 &gt; file2<\/p>\n<p>On the other hand, if this option is given a command separated list of numbers, the tool places tab stops at those specific positions.<\/p>\n<p class=\"command\">unexpand -t 1,2,3 file1 &gt; file2<\/p>\n<h2 id=\"q-how-to-make-unexpand-read-input-from-stdin\">Q4. How to make unexpand read input from STDIN?<\/h2>\n<p>This is fairly easy &#8211; just run the command without any file name as input (use ctrl+D to tell the utility that you&#8217;re done).<\/p>\n<p>Here&#8217;s what I am talking about:<\/p>\n<p class=\"command\">unexpand<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Depending upon the kind of work you do, the unexpand command could be of great help. In this tutorial, we discussed the majority of the command line option this tool offers. To know more, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/unexpand\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-unexpand-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-unexpand-command-explained-for-beginners-with-examples.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-unexpand-command%2F&amp;text=Linux+unexpand+Command+Explained+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-unexpand-command-explained-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-unexpand-command-explained-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-unexpand-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-unexpand-command-explained-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Working on the Linux command line requires to learn how to quickly perform some repetitive tasks. One such task that you may find yourself involved in sooner or later is converting blanks to tabs or vice-versa. In this tutorial, we will discuss a command line utility &#8211; dubbed unexpand &#8211; that&#8217;ll be of great help [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-4940","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=4940"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4940\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}