Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

GNU coreutils explained: the basic Linux tools that make the terminal work

Linux mascot using a laptop with some multiplexer terminals around it.
Lucas Gouveia/How-To Geek

Every Linux system is unique, and other Unix-based systems like macOS and BSD introduce even more differences. But they all have a reliable, consistent set of tools at their heart, and these tools help define the Linux experience.

What are coreutils?

The GNU Core Utilities (coreutils) are standard, Unix-based shell commands that you can expect to be installed on almost any Linux system. They tend to be on the simpler side, offering core functionality, like ls (list directory contents), pwd (print current directory), and cp (copy files).

Output from the ls command with the --version option giving the version number as 9.9, followed by a copyright notice and license details.

Many of these commands originate from the earliest days of Unix, so they are robust and dependable. Commands like cat , mv, and wc are so essential to the Unix experience that they've endured, with few fundamental changes, for over 60 years.

Advertisement
Advertisement

Programs in coreutils tend to adopt a common approach to their behavior. For example, you can expect them to support long-named command-line options like --help and --verbose.

On a macOS system, you'll find equivalents of these programs, many of which originated from BSD. This means that, while broadly interoperable, differences in behavior do exist.

Common coreutils programs

At the simplest end of the scale lie programs like true and false, which are minimal in the extreme. This pair of tools exists solely to return a fixed exit status: 0 and non-zero, respectively.

A command-line running the true and false programs, then echoing their exit status, showing values of 0 and 1 respectively.

These commands are so straightforward that your shell may already define them as built-ins; zsh does, for example . Although coreutils includes them for compatibility, your shell will prioritize any built-in equivalents. You can use the which command to discover exactly how another command is running, and a program's full path to override any built-in.

Advertisement
Advertisement

Most of the coreutils commands perform low-level operations on objects such as files and users. These include sysadmin commands like chmod , chown , dd, and du. You'll find standard commands for working with files like cp, mkdir, mv, rm , and touch , alongside commands for working with data/text like cut, sort, uniq , head , tail, and fold.

User-specific commands are useful on multi-user systems; these include users, groups, who, and whoami. You can also discover low-level information about your system with programs such as hostid, hostname, and uptime.

However, there are still many common programs that are not part of coreutils. Tools like grep , awk , sed, man , and top are all slightly more advanced than coreutils, even though you can expect them to be installed on almost every Linux system. Nor will you find any programming languages or shells in coreutils.

Installing and using coreutils

On most Linux systems, you'll already have coreutils installed. You can double-check you are running the coreutils version of a command with the --version option:

Output from the ls command with the --version option giving the version number as 9.9, followed by a copyright notice and license details.

If you get an error about an "unrecognized option '--version'," then you're not running the coreutils version.

Advertisement
Advertisement

Thewhichcommand, especiallywhich -acan help you determine the exact program that runs when you type its name.

On macOS, you can use Homebrew to install the GNU coreutils:

This will install the GNU coreutils equivalents, naming each with a gprefix to avoid conflicts. So gcp will run the GNU coreutils version of cp:

A macOS system showing that the command cp is a non-GNU version, while gcp runs the coreutils equivalent.

With some configuration, you can prefer the GNU coreutils by default, even when using the version of each command without its g prefix; just add the following to your PATH :

What the core utilities can teach you

The core utilities are small tools, each addressing a fundamental task. They are actively maintained and have been written to utilize the POSIX standard, while adding functionality on top of it.

Advertisement
Advertisement

As a result, they are great studies to base your own software on, both in terms of their interfaces and their source code.

The true program is a case in point. At 80 lines of C code , it's not doing much, but it's clearly doing more than just returning an exit status. Additionally, the true program:

  • Uses a clever macro trick to double as the false program

  • Shows how to handle usage via a command-line argument (--help)

  • Shows how to handle version details via --version

If you're looking to write your own command-line software, a program such as true is a great template to base it on. It demonstrates GNU conventions and introduces various libraries, like the system.h header file, that provide additional functionality.

Alternatives to coreutils

The GNU coreutils programs are written in C, but equivalents exist in other languages. The uutils project is the best known of these; it aims to rewrite these utilities in Rust, maintaining full compatibility with the GNU toolset.

The uutils home page describes the project as

Some individual programs that belong to coreutils have modern alternatives. These include ncdu instead of du and eza to replace ls. These versions often include features like support for colors, icons, and interactive text interfaces (TUIs).

Advertisement
Advertisement

There are also projects that aim to supplement coreutils, adding programs that are every bit as useful, but were initially overlooked. The most prominent is Joey Hess's collection of tools called moreutils . This package includes ts (to add timestamps to input), sponge (which writes standard input to a file synchronously), and lckdo (executes a program with a lock held).

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: