# Fold (Unix)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Fold_(Unix)
> Markdown URL: https://mediated.wiki/source/Fold_(Unix).md
> Source: https://en.wikipedia.org/wiki/Fold_(Unix)
> Source revision: 1336332465
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Unix command}}
{{lowercase title}}
{{Infobox software
| name                   = fold
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| author                 = [Bill Joy](/source/Bill_Joy)
| developer              = 
| released               = {{Start date and age|1977}}
| latest release version = 
| latest release date    = 
| operating system       = [Unix](/source/Unix) and [Unix-like](/source/Unix-like)
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = [coreutils](/source/coreutils): [GNU GPL v3](/source/GNU_General_Public_License)
| website                = 
}}
'''fold''' is a [Unix](/source/Unix) command used for making a [file](/source/File_(computing)) with long lines more readable on a limited width [computer terminal](/source/computer_terminal) by performing a [line wrap](/source/line_wrap).

Most [Unix](/source/Unix) terminals have a default screen width of 80, and therefore reading files with long lines could get annoying. The fold command puts a [line feed](/source/line_feed) every X characters if it does not reach a new line before that point. If the <code>-w</code> argument is set, the fold command allows the user to set the maximum length of a line.

== History ==
The utility first appeared in [1BSD](/source/Berkeley_Software_Distribution) of 1977 and was originally written by [Bill Joy](/source/Bill_Joy).<ref>{{man|1|fold|FreeBSD}}</ref><ref>{{Cite web|url=https://minnie.tuhs.org/cgi-bin/utree.pl?file=1BSD/s6/fold.c|title=fold.c – 1BSD|website=minnie.tuhs.org}}</ref>

{{code|fold}} is part of the [X/Open](/source/X%2FOpen) Portability Guide since issue 4 of 1992. It was inherited into the first version of POSIX.1 and the [Single Unix Specification](/source/Single_Unix_Specification).<ref>{{man|cu|printf|SUS}}</ref>

The version of <code>fold</code> bundled in [GNU](/source/GNU) [coreutils](/source/coreutils) was written by David MacKenzie.<ref>{{Cite web|url=https://linux.die.net/man/1/fold|title=fold(1) - Linux man page|website=linux.die.net|access-date=2019-02-24|archive-date=2021-04-12|archive-url=https://web.archive.org/web/20210412194050/https://linux.die.net/man/1/fold|url-status=live}}</ref>

The command is available as a separate package for [Microsoft Windows](/source/Microsoft_Windows) as part of the [UnxUtils](/source/UnxUtils) collection of [native](/source/Native_(computing)) [Win32](/source/Windows_API) [ports](/source/porting) of common GNU Unix-like utilities.<ref>{{Cite web|url=https://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net|access-date=2025-08-09|archive-date=2006-02-09|archive-url=https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/|url-status=live}}</ref>

==Example==
As a usage example, to fold a file named <code>file.txt</code> to have a maximum of 50 characters per line, one could run the following command:
<syntaxhighlight lang="console" highlight="16">
$ cat file.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur dignissim
venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non,
purus. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare
dictum. Praesent consectetuer vehicula ipsum. Praesent tortor massa, congue et,
ornare in, posuere eget, pede.

Vivamus rhoncus. Quisque lacus. In hac habitasse platea dictumst. Nullam mauris
tellus, sollicitudin non, semper eget, sodales non, pede. Phasellus varius
ullamcorper libero. Fusce ipsum lorem, iaculis nec, vulputate vitae, suscipit
vel, tortor. Cras varius.

Nullam fringilla pellentesque orci. Nulla eu ante pulvinar velit rhoncus
lacinia. Morbi fringilla lacus quis arcu. Vestibulum sem quam, dapibus in,
fringilla ut, venenatis ut, neque.
$ fold -w 50 file.txt
Lorem ipsum dolor sit amet, consectetuer adipiscin
g elit. Curabitur dignissim
venenatis pede. Quisque dui dui, ultricies ut, fac
ilisis non, pulvinar non,
purus. Duis quis arcu a purus volutpat iaculis. Mo
rbi id dui in diam ornare
dictum. Praesent consectetuer vehicula ipsum. Prae
sent tortor massa, congue et,
ornare in, posuere eget, pede.

Vivamus rhoncus. Quisque lacus. In hac habitasse p
latea dictumst. Nullam mauris
tellus, sollicitudin non, semper eget, sodales non
, pede. Phasellus varius
ullamcorper libero. Fusce ipsum lorem, iaculis nec
, vulputate vitae, suscipit
vel, tortor. Cras varius.

Nullam fringilla pellentesque orci. Nulla eu ante 
pulvinar velit rhoncus
lacinia. Morbi fringilla lacus quis arcu. Vestibul
um sem quam, dapibus in,
fringilla ut, venenatis ut, neque.
</syntaxhighlight>

==See also==
* [coreutils](/source/coreutils)
* [fmt (Unix)](/source/fmt_(Unix))
* [Par (command)](/source/Par_(command))
* [List of Unix commands](/source/List_of_Unix_commands)
* {{section link|Text mode|PC common text modes}}

==References==

{{Reflist}}

==External links==
{{Wikibooks|Guide to Unix|Commands}}
* {{man|1|fold|OpenBSD}}
* {{man|1|fold|FreeBSD}}

{{Unix commands}}
{{Core Utilities commands}}

Category:Unix text processing utilities
Category:Unix SUS2008 utilities

---
Adapted from the Wikipedia article [Fold (Unix)](https://en.wikipedia.org/wiki/Fold_(Unix)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Fold_(Unix)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
