{"article":{"slug":"moreutils-the-utilities-package-every-unix-linux-mac-os-developer-should-know","title":"moreutils: the utilities package every UNIX/Linux/Mac OS developer should know","subtitle":null,"summary":"Miguel Rentes walks through the moreutils package of Unix command-line tools, explaining with examples what chronic, combine, ifne, pee, sponge, ts, vidir, vipe, zrun and the other utilities do and how to install them.","content_type":"tutorial","language":"en","canonical_url":"https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/","author":{"name":"Miguel Rentes","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Clean Coding","url":"https://rentes.github.io/","listing_slug":null,"listing":null},"topics":[{"name":"Linux","slug":"linux","url":"https://listedarticles.com/topics/linux"},{"name":"Developer Tools","slug":"developer-tools","url":"https://listedarticles.com/topics/developer-tools"},{"name":"Tutorials","slug":"tutorials","url":"https://listedarticles.com/topics/tutorials"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":2541,"reading_minutes":11,"published_at":"2015-07-27T00:00:00.000Z","added_at":"2026-10-24T02:07:26.082Z","updated_at":"2026-10-24T02:07:26.082Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/moreutils-the-utilities-package-every-unix-linux-mac-os-developer-should-know","markdown_url":"https://listedarticles.com/articles/moreutils-the-utilities-package-every-unix-linux-mac-os-developer-should-know.md","example":false,"citation":"Miguel Rentes, Clean Coding. \"moreutils: the utilities package every UNIX/Linux/Mac OS developer should know.\" 27 Jul 2015. https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/"},"body_markdown":"# moreutils: the utilities package every UNIX/Linux/Mac OS developer should know\n\n*July 27, 2015 · Miguel Rentes*\n\nMost UNIX professionals know about the [GNU core utilities](https://en.wikipedia.org/wiki/GNU_Core_Utilities), from the “used-everyday” ls, cp, ln, rm, touch, tail, wc, etc., to the “not-so-used” shell commands such as tsort, tac, factor, or seq. Among the UNIX utilities available, there is one package that includes some awesome utilities that makes our life’s a lot easier: the [moreutils](https://packages.debian.org/unstable/utils/moreutils) package.\n\n- [Installing](#installing)\n- [The moreutils tools](#the-moreutils-tools)\n  - [chronic: *runs a command quietly unless it fails*](#chronic)\n  - [combine: *combine the lines in two files using boolean operations*](#combine)\n  - [errno: *look up errno names and descriptions*](#errno)\n  - [ifdata: *get network interface info without parsing ifconfig output*](#ifdata)\n  - [ifne: *run a program if the standard input is not empty*](#ifne)\n  - [isutf8: *check if a file or standard input is utf-8*](#isutf8)\n  - [lckdo: *execute a program with a lock held*](#lckdo)\n  - [mispipe: *pipe two commands, returning the exit status of the first*](#mispipe)\n  - [parallel: *run multiple jobs at once*](#parallel)\n  - [pee: *tee standard input to pipes*](#pee)\n  - [sponge: *soak up standard input and write to a file*](#sponge)\n  - [ts: *timestamp standard input*](#ts)\n  - [vidir: *edit a directory in your text editor*](#vidir)\n  - [vipe: *insert a text editor into a pipe*](#vipe)\n  - [zrun: *automatically uncompress arguments to command*](#zrun)\n\n## Installing\n\nBefore describing each of the utilities included, let me tell you that this package is not exclusive for Linux systems. You can install it also under Mac OS and different UNIX flavours.\n\nUnder Mac OS, just use the following command:\n\n```\n$ brew install moreutils\n```\n\n**Note**: There is a conflict between the GNU parallel utility and the shipped parallel on the moreutils package. If you want to install GNU parallel instead of the moreutils’ parallel, use the following commands:\n\n```\n$ brew install moreutils --without-parallel\n$ brew install parallel\n```\n\nYou can read more about it [here](http://superuser.com/questions/545889/how-can-i-install-gnu-parallel-alongside-moreutils).\n\nUnder Linux, use your favourite package manager or the command line to retrieve this package.\nIf you are using Arch Linux all you have to do is:\n\n```\n$ sudo pacman -S moreutils\n```\n\nUnder Debian/Ubuntu/Mint a similar command is:\n\n```\n$ sudo apt-get install moreutils\n```\n\nAnd on RHEL, Fedora, CentOS:\n\n```\n$ sudo yum install moreutils\n```\n\nDon’t forget to check your Linux distribution to know how to install this package if the above commands are not for your particular Linux distribution.\n\n##The moreutils tools\n\n###chronic\nThis tool runs a command , and arranges for its standard out and standard error to only be displayed if the command fails (exits nonzero or crashes). If the command succeeds, any extraneous output will be hidden.\n\nBefore discovering this tool I would do something like this to hide a command output:\n\n```\n$ command >& /dev/null\n```\n\nbut using chronic is so much simpler, and has the benefit that I know if the command succeeded or not, which I would not know from the line above (all output is redirected to `/dev/null`).\n\nA common use for chronic is for running a cron job. Rather than trying to keep the command quiet, and having to deal with mails containing accidental output when it succeeds, and not verbose enough output when it fails, you can just run it verbosely always, and use chronic to hide the successful output.\n\n```\n0 1 * * * chronic backup # instead of backup >/dev/null 2>&1\n```\n\n### combine\n\nThis tool combines the lines in two files using boolean operations.\n\n```\n$ combine file1 and file2\n\n$ combine file1 not file2\n\n$ combine file1 or file2\n\n$ combine file1 xor file2\n```\n\nDepending on the boolean operation specified, the contents will be combined in different ways:\n\n- **and** Outputs lines that are in file1 if they are also present in file2.\n- **not** Outputs lines that are in file1 but not in file2.\n- **or** Outputs lines that are in file1 or file2.\n- **xor** Outputs lines that are in either file1 or file2, but not in both files.\n\nThe input files need not be sorted, and the lines are output in the order they occur in file1 (followed by the order they occur in file2 for the two “or” operations). This means that the operations are not commutative; “a and b” will not necessarily be the same as “b and a”. To obtain commutative behavior, use the `sort` and `uniq` commands to the result, like this:\n\n```\n$ cat a\none\nten\ntwo\nthree\n$ cat b\nten\ntwelve\none\nthirteen\n$ combine a and b\none\nten\n$ combine b and a\nten\none\n$ combine a and b | sort | uniq\none\nten\n$ combine b and a | sort | uniq\none\nten\n```\n\n### errno\n\nThis tool looks up errno macro names, errno codes, and the corresponding descriptions.\n\n```\n$ errno ENOENT\n\nENOENT 2 No such file or directory\n\n$ errno 100\n\nENETDOWN 100 Network is down\n\n$ errno -l\nEPERM 1 Operation not permitted\nENOENT 2 No such file or directory\nESRCH 3 No such process\nEINTR 4 Interrupted system call\nEIO 5 Input/output error\nENXIO 6 No such device or address\nE2BIG 7 Argument list too long\nENOEXEC 8 Exec format error\nEBADF 9 Bad file descriptor\nECHILD 10 No child processes\nEAGAIN 11 Resource temporarily unavailable\nENOMEM 12 Cannot allocate memory\nEACCES 13 Permission denied\nEFAULT 14 Bad address\nENOTBLK 15 Block device required\nEBUSY 16 Device or resource busy\nEEXIST 17 File exists\nEXDEV 18 Invalid cross-device link\nENODEV 19 No such device\nENOTDIR 20 Not a directory\n(...)\n```\n\nThis is a most valuable tool and a lot better than reading the system header files to figure out the meaning of a particular errno code. If you do a lot UNIX programming you know what I am taking about.\n\n### ifdata\n\nThis tool can be used to check for the existence of a network interface, or to get information about the interface, such as its IP address, all statistics on input and output, number of packets, bytes, errors, drops, incoming and outgoing bit rate, etc.. Unlike `ifconfig` or `ip`, `ifdata` has simple to parse output that is designed to be easily used by a shell script.\n\n```\n$ ifdata -si eth0\n1943188030 8648846 0 16803 0 0 0 0\n$ ifdata -sip eth0\n8649245\n$ ifdata -so eth0\n141940989 953001 0 0 0 0 0 0\n$ ifdata -bips eth0\n3012\n$ ifdata -bops eth0\n66\n```\n\n### ifne\n\nThis tool runs a program if the standard input is not empty. The name stands for “if not empty” and it simply runs the passed command to it, if it receives at least one byte in the `stdin`. You can also use the `-n` flag which causes `ifne` to reverse the operation, e.g., runs the command passed to it if the standard input is empty. Note that if the standard input is not empty, it is passed through ifne in this case. The below command line checks for `core` files on the current directory, and if found (standard inputs is not empty), sends a mail to user root alerting for this fact.\n\n```\n$ find . -name core | ifne mail -s \"Core files found\" root\n```\n\n### isutf8\n\nThis tool checks if a file or standard input are syntactically valid UTF-8. Input is either files named on the command line, or the standard input. Information about files with invalid UTF-8 are printed to standard output, otherwise nothing is printed.\n\n### lckdo\n\n**Note**: Now that package `util-linux` contains a similar command named `flock`, `lckdo` is deprecated, and will be removed from some future version of moreutils.\n\nThe `flock` utility runs a program with a lock held, in order to prevent multiple processes from running in parallel. It locks from within shell scripts or from the command line. It locks a specified file or directory, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, `flock` waits until the lock is available. It can also lock an open file by its file descriptor number. Use it just like `nice` or `nohup`.\n\n```\n(shell1) $ flock /tmp -c cat\n(shell2) $ flock -w .007 /tmp -c echo; /bin/echo $?\n```\n\nThe first command sets an exclusive lock to directory /tmp and the second command will fail.\n\n```\n(shell1) $ flock -s /tmp -c cat\n(shell2) $ flock -s -w .007 /tmp -c echo; /bin/echo $?\n```\n\nThe first command sets a shared lock to directory /tmp and the second command will not fail. Notice that attempting to get exclusive lock with the second command would fail.\n\n```\n$ flock -x local-lock-file echo 'a b c'\n```\n\nThis command grabs the exclusive lock `local-lock-file` before running `echo` with ‘a b c’.\n\n### mispipe\n\nThis command pipes two commands together like the shell does, but unlike piping in the shell, which returns the exit status of the last command; when using mispipe, the exit status of the first command is returned.\n\n**Note**: Modern shells, like `bash` or `zsh`, offer a pipefail option, although that option does not behave the same like mispipe because it makes a failure of **any** command in the pipeline to be returned, not just the exit status of the first. This can be a better alternative to mispipe which only returns the exit status of the first command passed to it.\n\n### parallel\n\nThis tool runs the specified command, passing it a single one of the specified arguments. This is repeated for each argument. Jobs may be run in parallel. The default is to run one job per CPU.\n\n```\n$ parallel sh -c \"echo hi; sleep 2; echo bye\" -- 1 2 3\n```\n\nThe above command runs three subshells that each print a message, delay, and print another message. If your system has multiple CPUs, parallel will run some of the jobs in parallel, which should be clear from the order the messages are output.\n\n```\n$ parallel -j 3 ufraw -o processed -- *.NEF\n```\n\nThe above command runs three ufraw processes at the same time until all of the NEF files have been processed.\n\n```\n$ parallel -j 3 -- ls df \"echo hi\"\n```\n\nThe above command runs three independent commands (`ls`, `df` and the `echo`) in parallel.\n\n**Note**: check the `xargs` command to also run multiple processes at a time, specifically with the `-n` and `-P` flags.\n\n### pee\n\nThis command is similar to how `tee` uses the standard input but for pipes, hence the name. It allows redirecting output to multiple commands at once. Each command is run and fed a copy of the standard input. The output of all commands is sent to `stdout`. While this is similar to `tee`, a copy of the input is not sent to `stdout`, like `tee` does. If that is desired, use `pee cat ...` instead.\n\n```\n$ cat file\n5\n4\n3\n2\n1\n$ cat file | pee 'sort -u > sorted' 'sort -R > unsorted'\n$ cat sorted\n1\n2\n3\n4\n5\n$ cat unsorted\n2\n1\n4\n3\n5\n```\n\nIn this example, `pee` receives two commands: one for sorting the contents of the file, and another for randomly sorting the same file contents, outputting to two different files named sorted and unsorted, respectively.\n\n### sponge\n\nThis tool reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before writing the output file. This means that it only writes to the file once the input has been fully read. This allows constructing pipelines that read from and write to the same file, without needing to redirect the output to temporary files.\n\nInstead of using these commands to sort a file contents:\n\n```\n$ sort filename | uniq > temp\n$ mv temp filename\n```\n\nJust use:\n\n```\n$ sort filename | uniq | sponge filename\n```\n\nNo need to redirecting anymore.\n\n### ts\n\nThis tool adds a timestamp to the beginning of each line of input. This is an awesome tool that I like to use specially on a chain of commands where I can measure how long a given command is taking to complete before passing the control to the next command. It is also a great way of logging the time stamps of a programs’ output.\n\n```\n$ cat script.sh\necho \"The first line\"\nsleep 5s\necho \"The last line\"\n$ chmod a+x script.sh\n$ ./script.sh\nThe first line\nThe last line\n$ ./script.sh | ts\nJul 27 15:50:42 The first line\nJul 27 15:50:47 The last line\n```\n\nOne possible use of the `ts` command is to assert whenever a given machine is up and running. Restart a machine and then use `ping` with the `ts` to know exactly when that machine came up to life!\n\n```\n$ ping <IP address or hostname> | ts\nJul 27 15:53:15 PING EE.FF.GG.HH (EE.FF.GG.HH) 56(84) bytes of data.\nJul 27 15:53:15 From AA.BB.CC.DD icmp_seq=3 Destination Net Unreachable\nJul 27 15:53:18 From AA.BB.CC.DD icmp_seq=7 Destination Net Unreachable\n(...)\nJul 27 15:54:08 64 bytes from EE.FF.GG.HH: icmp_seq=57 ttl=127 time=0.673 ms\n```\n\nThe optional format parameter controls how the timestamp is formatted, as used by the C function `strftime`. The default format is `%b %d %H:%M:%S`. In addition to the regular `strftime` conversion specifications, `%.S` and `%.s` are like `%S` and `%s`, but provide sub-second resolution (i.e., “30.00001” and “1301682593.00001”).\n\n### vidir\n\nThis tool allows editing of the contents of a directory in a text editor (check the `$EDITOR` environment variable, because it will be used for the text editor of your choice). If no directory is specified, the current directory is edited. When editing a directory, each item in the directory will appear on its own numbered line. These numbers are how vidir keeps track of what items are changed. Delete lines to remove files from the directory, or edit filenames to rename files. You can also switch pairs of numbers to swap filenames. Note that if “-“ is specified as the directory to edit, it reads a list of filenames from stdin and displays those for editing. Alternatively, a list of files can be specified on the command line.\n\n### vipe\n\nThis tool allows you to run your editor in the middle of a unix pipeline and edit the data that is being piped between programs (once again, check the `$EDITOR` environment variable for your text editor of choice).\n\n```\n$ fortune | vipe | cowsay\n _________________________________________\n/ \"I am your density.\"                    \\\n|                                         |\n| -- George McFly in \"Back to the Future\" |\n| - This fortune cookie was just edited   |\n\\ by Miguel Rentes (in the future!)       /\n -----------------------------------------\n        \\   ^__^\n         \\  (oo)\\_______\n            (__)\\       )\\/\\\n                ||----w |\n                ||     ||\n```\n\nOn the above command I edited the fortune cookie before passing it to the cowsay command. This is a command that offers you great flexibility and power.\n\n### zrun\n\nThis tool automatically decompresses arguments to command. Prefixing a shell command with `zrun` causes any compressed files that are arguments of the command to be transparently uncompressed to temp files (not pipes) and the uncompressed files fed to the command. This is a quick way to run a command that does not itself support compressed files, without manually uncompressing the files. For example, if you want to `diff` the contents of two archives without having to uncompress them first, use the following command:\n\n```\n$ zrun diff archive1.gz archive2.gz\n```\n\nThis command will uncompress automatically the two .gz files and pass the contents to the diff command. Very cool.\n\nThe following compression types are supported: `gz bz2 Z xz lzma lzo`. If zrun is linked to some name beginning with z, like zprog, and the link is executed, this is equivalent to executing `zrun prog`.\n\nThat’s it! Please don’t forget to read the manpages for greater detail on the above commands. I hope you liked to learn about this fantastic tool package and that it motivated you to use it on your daily UNIX programming tasks! Until next time, have an awesome coding fun!\n","body_html":"<h1 id=\"moreutils-the-utilities-package-every-unix-linux-mac-os-develope\">moreutils: the utilities package every UNIX/Linux/Mac OS developer should know</h1>\n<p><em>July 27, 2015 · Miguel Rentes</em></p>\n<p>Most UNIX professionals know about the <a href=\"https://en.wikipedia.org/wiki/GNU_Core_Utilities\" rel=\"nofollow ugc noopener\">GNU core utilities</a>, from the “used-everyday” ls, cp, ln, rm, touch, tail, wc, etc., to the “not-so-used” shell commands such as tsort, tac, factor, or seq. Among the UNIX utilities available, there is one package that includes some awesome utilities that makes our life’s a lot easier: the <a href=\"https://packages.debian.org/unstable/utils/moreutils\" rel=\"nofollow ugc noopener\">moreutils</a> package.</p>\n<ul><li><a href=\"#installing\">Installing</a></li><li><a href=\"#the-moreutils-tools\">The moreutils tools</a><ul><li><a href=\"#chronic\">chronic: <em>runs a command quietly unless it fails</em></a></li><li><a href=\"#combine\">combine: <em>combine the lines in two files using boolean operations</em></a></li><li><a href=\"#errno\">errno: <em>look up errno names and descriptions</em></a></li><li><a href=\"#ifdata\">ifdata: <em>get network interface info without parsing ifconfig output</em></a></li><li><a href=\"#ifne\">ifne: <em>run a program if the standard input is not empty</em></a></li><li><a href=\"#isutf8\">isutf8: <em>check if a file or standard input is utf-8</em></a></li><li><a href=\"#lckdo\">lckdo: <em>execute a program with a lock held</em></a></li><li><a href=\"#mispipe\">mispipe: <em>pipe two commands, returning the exit status of the first</em></a></li><li><a href=\"#parallel\">parallel: <em>run multiple jobs at once</em></a></li><li><a href=\"#pee\">pee: <em>tee standard input to pipes</em></a></li><li><a href=\"#sponge\">sponge: <em>soak up standard input and write to a file</em></a></li><li><a href=\"#ts\">ts: <em>timestamp standard input</em></a></li><li><a href=\"#vidir\">vidir: <em>edit a directory in your text editor</em></a></li><li><a href=\"#vipe\">vipe: <em>insert a text editor into a pipe</em></a></li><li><a href=\"#zrun\">zrun: <em>automatically uncompress arguments to command</em></a></li></ul></li></ul>\n<h2 id=\"installing\">Installing</h2>\n<p>Before describing each of the utilities included, let me tell you that this package is not exclusive for Linux systems. You can install it also under Mac OS and different UNIX flavours.</p>\n<p>Under Mac OS, just use the following command:</p>\n<pre><code>$ brew install moreutils</code></pre>\n<p><strong>Note</strong>: There is a conflict between the GNU parallel utility and the shipped parallel on the moreutils package. If you want to install GNU parallel instead of the moreutils’ parallel, use the following commands:</p>\n<pre><code>$ brew install moreutils --without-parallel\n$ brew install parallel</code></pre>\n<p>You can read more about it <a href=\"http://superuser.com/questions/545889/how-can-i-install-gnu-parallel-alongside-moreutils\" rel=\"nofollow ugc noopener\">here</a>.</p>\n<p>Under Linux, use your favourite package manager or the command line to retrieve this package.\nIf you are using Arch Linux all you have to do is:</p>\n<pre><code>$ sudo pacman -S moreutils</code></pre>\n<p>Under Debian/Ubuntu/Mint a similar command is:</p>\n<pre><code>$ sudo apt-get install moreutils</code></pre>\n<p>And on RHEL, Fedora, CentOS:</p>\n<pre><code>$ sudo yum install moreutils</code></pre>\n<p>Don’t forget to check your Linux distribution to know how to install this package if the above commands are not for your particular Linux distribution.</p>\n<p>##The moreutils tools</p>\n<p>###chronic\nThis tool runs a command , and arranges for its standard out and standard error to only be displayed if the command fails (exits nonzero or crashes). If the command succeeds, any extraneous output will be hidden.</p>\n<p>Before discovering this tool I would do something like this to hide a command output:</p>\n<pre><code>$ command &gt;&amp; /dev/null</code></pre>\n<p>but using chronic is so much simpler, and has the benefit that I know if the command succeeded or not, which I would not know from the line above (all output is redirected to <code>/dev/null</code>).</p>\n<p>A common use for chronic is for running a cron job. Rather than trying to keep the command quiet, and having to deal with mails containing accidental output when it succeeds, and not verbose enough output when it fails, you can just run it verbosely always, and use chronic to hide the successful output.</p>\n<pre><code>0 1 * * * chronic backup # instead of backup &gt;/dev/null 2&gt;&amp;1</code></pre>\n<h3 id=\"combine\">combine</h3>\n<p>This tool combines the lines in two files using boolean operations.</p>\n<pre><code>$ combine file1 and file2\n\n$ combine file1 not file2\n\n$ combine file1 or file2\n\n$ combine file1 xor file2</code></pre>\n<p>Depending on the boolean operation specified, the contents will be combined in different ways:</p>\n<ul><li><strong>and</strong> Outputs lines that are in file1 if they are also present in file2.</li><li><strong>not</strong> Outputs lines that are in file1 but not in file2.</li><li><strong>or</strong> Outputs lines that are in file1 or file2.</li><li><strong>xor</strong> Outputs lines that are in either file1 or file2, but not in both files.</li></ul>\n<p>The input files need not be sorted, and the lines are output in the order they occur in file1 (followed by the order they occur in file2 for the two “or” operations). This means that the operations are not commutative; “a and b” will not necessarily be the same as “b and a”. To obtain commutative behavior, use the <code>sort</code> and <code>uniq</code> commands to the result, like this:</p>\n<pre><code>$ cat a\none\nten\ntwo\nthree\n$ cat b\nten\ntwelve\none\nthirteen\n$ combine a and b\none\nten\n$ combine b and a\nten\none\n$ combine a and b | sort | uniq\none\nten\n$ combine b and a | sort | uniq\none\nten</code></pre>\n<h3 id=\"errno\">errno</h3>\n<p>This tool looks up errno macro names, errno codes, and the corresponding descriptions.</p>\n<pre><code>$ errno ENOENT\n\nENOENT 2 No such file or directory\n\n$ errno 100\n\nENETDOWN 100 Network is down\n\n$ errno -l\nEPERM 1 Operation not permitted\nENOENT 2 No such file or directory\nESRCH 3 No such process\nEINTR 4 Interrupted system call\nEIO 5 Input/output error\nENXIO 6 No such device or address\nE2BIG 7 Argument list too long\nENOEXEC 8 Exec format error\nEBADF 9 Bad file descriptor\nECHILD 10 No child processes\nEAGAIN 11 Resource temporarily unavailable\nENOMEM 12 Cannot allocate memory\nEACCES 13 Permission denied\nEFAULT 14 Bad address\nENOTBLK 15 Block device required\nEBUSY 16 Device or resource busy\nEEXIST 17 File exists\nEXDEV 18 Invalid cross-device link\nENODEV 19 No such device\nENOTDIR 20 Not a directory\n(...)</code></pre>\n<p>This is a most valuable tool and a lot better than reading the system header files to figure out the meaning of a particular errno code. If you do a lot UNIX programming you know what I am taking about.</p>\n<h3 id=\"ifdata\">ifdata</h3>\n<p>This tool can be used to check for the existence of a network interface, or to get information about the interface, such as its IP address, all statistics on input and output, number of packets, bytes, errors, drops, incoming and outgoing bit rate, etc.. Unlike <code>ifconfig</code> or <code>ip</code>, <code>ifdata</code> has simple to parse output that is designed to be easily used by a shell script.</p>\n<pre><code>$ ifdata -si eth0\n1943188030 8648846 0 16803 0 0 0 0\n$ ifdata -sip eth0\n8649245\n$ ifdata -so eth0\n141940989 953001 0 0 0 0 0 0\n$ ifdata -bips eth0\n3012\n$ ifdata -bops eth0\n66</code></pre>\n<h3 id=\"ifne\">ifne</h3>\n<p>This tool runs a program if the standard input is not empty. The name stands for “if not empty” and it simply runs the passed command to it, if it receives at least one byte in the <code>stdin</code>. You can also use the <code>-n</code> flag which causes <code>ifne</code> to reverse the operation, e.g., runs the command passed to it if the standard input is empty. Note that if the standard input is not empty, it is passed through ifne in this case. The below command line checks for <code>core</code> files on the current directory, and if found (standard inputs is not empty), sends a mail to user root alerting for this fact.</p>\n<pre><code>$ find . -name core | ifne mail -s &quot;Core files found&quot; root</code></pre>\n<h3 id=\"isutf8\">isutf8</h3>\n<p>This tool checks if a file or standard input are syntactically valid UTF-8. Input is either files named on the command line, or the standard input. Information about files with invalid UTF-8 are printed to standard output, otherwise nothing is printed.</p>\n<h3 id=\"lckdo\">lckdo</h3>\n<p><strong>Note</strong>: Now that package <code>util-linux</code> contains a similar command named <code>flock</code>, <code>lckdo</code> is deprecated, and will be removed from some future version of moreutils.</p>\n<p>The <code>flock</code> utility runs a program with a lock held, in order to prevent multiple processes from running in parallel. It locks from within shell scripts or from the command line. It locks a specified file or directory, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, <code>flock</code> waits until the lock is available. It can also lock an open file by its file descriptor number. Use it just like <code>nice</code> or <code>nohup</code>.</p>\n<pre><code>(shell1) $ flock /tmp -c cat\n(shell2) $ flock -w .007 /tmp -c echo; /bin/echo $?</code></pre>\n<p>The first command sets an exclusive lock to directory /tmp and the second command will fail.</p>\n<pre><code>(shell1) $ flock -s /tmp -c cat\n(shell2) $ flock -s -w .007 /tmp -c echo; /bin/echo $?</code></pre>\n<p>The first command sets a shared lock to directory /tmp and the second command will not fail. Notice that attempting to get exclusive lock with the second command would fail.</p>\n<pre><code>$ flock -x local-lock-file echo &#39;a b c&#39;</code></pre>\n<p>This command grabs the exclusive lock <code>local-lock-file</code> before running <code>echo</code> with ‘a b c’.</p>\n<h3 id=\"mispipe\">mispipe</h3>\n<p>This command pipes two commands together like the shell does, but unlike piping in the shell, which returns the exit status of the last command; when using mispipe, the exit status of the first command is returned.</p>\n<p><strong>Note</strong>: Modern shells, like <code>bash</code> or <code>zsh</code>, offer a pipefail option, although that option does not behave the same like mispipe because it makes a failure of <strong>any</strong> command in the pipeline to be returned, not just the exit status of the first. This can be a better alternative to mispipe which only returns the exit status of the first command passed to it.</p>\n<h3 id=\"parallel\">parallel</h3>\n<p>This tool runs the specified command, passing it a single one of the specified arguments. This is repeated for each argument. Jobs may be run in parallel. The default is to run one job per CPU.</p>\n<pre><code>$ parallel sh -c &quot;echo hi; sleep 2; echo bye&quot; -- 1 2 3</code></pre>\n<p>The above command runs three subshells that each print a message, delay, and print another message. If your system has multiple CPUs, parallel will run some of the jobs in parallel, which should be clear from the order the messages are output.</p>\n<pre><code>$ parallel -j 3 ufraw -o processed -- *.NEF</code></pre>\n<p>The above command runs three ufraw processes at the same time until all of the NEF files have been processed.</p>\n<pre><code>$ parallel -j 3 -- ls df &quot;echo hi&quot;</code></pre>\n<p>The above command runs three independent commands (<code>ls</code>, <code>df</code> and the <code>echo</code>) in parallel.</p>\n<p><strong>Note</strong>: check the <code>xargs</code> command to also run multiple processes at a time, specifically with the <code>-n</code> and <code>-P</code> flags.</p>\n<h3 id=\"pee\">pee</h3>\n<p>This command is similar to how <code>tee</code> uses the standard input but for pipes, hence the name. It allows redirecting output to multiple commands at once. Each command is run and fed a copy of the standard input. The output of all commands is sent to <code>stdout</code>. While this is similar to <code>tee</code>, a copy of the input is not sent to <code>stdout</code>, like <code>tee</code> does. If that is desired, use <code>pee cat ...</code> instead.</p>\n<pre><code>$ cat file\n5\n4\n3\n2\n1\n$ cat file | pee &#39;sort -u &gt; sorted&#39; &#39;sort -R &gt; unsorted&#39;\n$ cat sorted\n1\n2\n3\n4\n5\n$ cat unsorted\n2\n1\n4\n3\n5</code></pre>\n<p>In this example, <code>pee</code> receives two commands: one for sorting the contents of the file, and another for randomly sorting the same file contents, outputting to two different files named sorted and unsorted, respectively.</p>\n<h3 id=\"sponge\">sponge</h3>\n<p>This tool reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before writing the output file. This means that it only writes to the file once the input has been fully read. This allows constructing pipelines that read from and write to the same file, without needing to redirect the output to temporary files.</p>\n<p>Instead of using these commands to sort a file contents:</p>\n<pre><code>$ sort filename | uniq &gt; temp\n$ mv temp filename</code></pre>\n<p>Just use:</p>\n<pre><code>$ sort filename | uniq | sponge filename</code></pre>\n<p>No need to redirecting anymore.</p>\n<h3 id=\"ts\">ts</h3>\n<p>This tool adds a timestamp to the beginning of each line of input. This is an awesome tool that I like to use specially on a chain of commands where I can measure how long a given command is taking to complete before passing the control to the next command. It is also a great way of logging the time stamps of a programs’ output.</p>\n<pre><code>$ cat script.sh\necho &quot;The first line&quot;\nsleep 5s\necho &quot;The last line&quot;\n$ chmod a+x script.sh\n$ ./script.sh\nThe first line\nThe last line\n$ ./script.sh | ts\nJul 27 15:50:42 The first line\nJul 27 15:50:47 The last line</code></pre>\n<p>One possible use of the <code>ts</code> command is to assert whenever a given machine is up and running. Restart a machine and then use <code>ping</code> with the <code>ts</code> to know exactly when that machine came up to life!</p>\n<pre><code>$ ping &lt;IP address or hostname&gt; | ts\nJul 27 15:53:15 PING EE.FF.GG.HH (EE.FF.GG.HH) 56(84) bytes of data.\nJul 27 15:53:15 From AA.BB.CC.DD icmp_seq=3 Destination Net Unreachable\nJul 27 15:53:18 From AA.BB.CC.DD icmp_seq=7 Destination Net Unreachable\n(...)\nJul 27 15:54:08 64 bytes from EE.FF.GG.HH: icmp_seq=57 ttl=127 time=0.673 ms</code></pre>\n<p>The optional format parameter controls how the timestamp is formatted, as used by the C function <code>strftime</code>. The default format is <code>%b %d %H:%M:%S</code>. In addition to the regular <code>strftime</code> conversion specifications, <code>%.S</code> and <code>%.s</code> are like <code>%S</code> and <code>%s</code>, but provide sub-second resolution (i.e., “30.00001” and “1301682593.00001”).</p>\n<h3 id=\"vidir\">vidir</h3>\n<p>This tool allows editing of the contents of a directory in a text editor (check the <code>$EDITOR</code> environment variable, because it will be used for the text editor of your choice). If no directory is specified, the current directory is edited. When editing a directory, each item in the directory will appear on its own numbered line. These numbers are how vidir keeps track of what items are changed. Delete lines to remove files from the directory, or edit filenames to rename files. You can also switch pairs of numbers to swap filenames. Note that if “-“ is specified as the directory to edit, it reads a list of filenames from stdin and displays those for editing. Alternatively, a list of files can be specified on the command line.</p>\n<h3 id=\"vipe\">vipe</h3>\n<p>This tool allows you to run your editor in the middle of a unix pipeline and edit the data that is being piped between programs (once again, check the <code>$EDITOR</code> environment variable for your text editor of choice).</p>\n<pre><code>$ fortune | vipe | cowsay\n _________________________________________\n/ &quot;I am your density.&quot;                    \\\n|                                         |\n| -- George McFly in &quot;Back to the Future&quot; |\n| - This fortune cookie was just edited   |\n\\ by Miguel Rentes (in the future!)       /\n -----------------------------------------\n        \\   ^__^\n         \\  (oo)\\_______\n            (__)\\       )\\/\\\n                ||----w |\n                ||     ||</code></pre>\n<p>On the above command I edited the fortune cookie before passing it to the cowsay command. This is a command that offers you great flexibility and power.</p>\n<h3 id=\"zrun\">zrun</h3>\n<p>This tool automatically decompresses arguments to command. Prefixing a shell command with <code>zrun</code> causes any compressed files that are arguments of the command to be transparently uncompressed to temp files (not pipes) and the uncompressed files fed to the command. This is a quick way to run a command that does not itself support compressed files, without manually uncompressing the files. For example, if you want to <code>diff</code> the contents of two archives without having to uncompress them first, use the following command:</p>\n<pre><code>$ zrun diff archive1.gz archive2.gz</code></pre>\n<p>This command will uncompress automatically the two .gz files and pass the contents to the diff command. Very cool.</p>\n<p>The following compression types are supported: <code>gz bz2 Z xz lzma lzo</code>. If zrun is linked to some name beginning with z, like zprog, and the link is executed, this is equivalent to executing <code>zrun prog</code>.</p>\n<p>That’s it! Please don’t forget to read the manpages for greater detail on the above commands. I hope you liked to learn about this fantastic tool package and that it motivated you to use it on your daily UNIX programming tasks! Until next time, have an awesome coding fun!</p>","headings":[{"level":1,"text":"moreutils: the utilities package every UNIX/Linux/Mac OS developer should know","id":"moreutils-the-utilities-package-every-unix-linux-mac-os-develope"},{"level":2,"text":"Installing","id":"installing"},{"level":3,"text":"combine","id":"combine"},{"level":3,"text":"errno","id":"errno"},{"level":3,"text":"ifdata","id":"ifdata"},{"level":3,"text":"ifne","id":"ifne"},{"level":3,"text":"isutf8","id":"isutf8"},{"level":3,"text":"lckdo","id":"lckdo"},{"level":3,"text":"mispipe","id":"mispipe"},{"level":3,"text":"parallel","id":"parallel"},{"level":3,"text":"pee","id":"pee"},{"level":3,"text":"sponge","id":"sponge"},{"level":3,"text":"ts","id":"ts"},{"level":3,"text":"vidir","id":"vidir"},{"level":3,"text":"vipe","id":"vipe"},{"level":3,"text":"zrun","id":"zrun"}]}}