GNU Coreutils

Small reference / cheatsheet for some of the software grouped under GNU Coreutils.


Here is the official documentation of GNU Coreutils, you might be better off reading it directly. I wrote this to have a reference that's more to my liking, and to force myself to review these tools, but I might have made errors or omitted various things. If in doubt, always check the official documentation. This is (and will be for a long time) a work-in-progress.


cat

cat (concatenate and write files) copies each file, or standard input if no files are given, to standard output.

cat [option]... [file1]...
0

Notes

  • cat normally reads and writes in binary mode, but will read and write in text mode if any of the -bensAE options is used, or if cat is reading / writing from / to the standard input / output of a terminal.

Options

Option Description Note
-v show nonprinting characters
-E show $ at the end of each line
-T show TABs as ^I
-A show all the invisible characters equivalent to -vET
-e shortcut for -vE
-t shortcut for -vT
-b number all nonempty output lines
-n number all output lines ignored if -b is used
-s omit repeated blank lines, output just one

Examples

# concatenate to ./a and ./b into ./c
cat a b > c
0
1

Extra - The "useless use of cat"

Generally, the use of cat as a way to read a file and pipe its contents to a different command is frowned upon. There are many memes of cat abuse, and even cat's Wikipedia page explains why it's such a hot subject.

# don't do this
cat file | command arg1 arg2

# do this
command arg1 arg2 < file
0
1
2
3
4

Because many programs operate on stdin by default, when not given files as arguments, there's a lot of use of the following (also incorrect) pattern:

# don't do this
cat file | grep "test"

# do this
grep "test" file
0
1
2
3
4

tac

tac (concatenate and write files in reverse) copes each file, or standard input if no files are given, to standard output, reversing the order of the records (records being lines by default).

tac [option]... [file]...
0

Notes

  • records are separated by a specific string (newline by default), the separator being positioned after the record by default

Options

Option Description
-s separator a separator to use instead of newline
-r treat the separator as a regular expresion
-b declare that the separator occurs before the record

Examples

# reverse characters in a string
echo "abc" | tac -r -s "."
0
1

base64 / base32

base64 and base32 transform data read from a file or standard input into / from base64 or base32.

base64 [option]... [file]
base32 [option]... [file]
0
1

Notes

  • when decoding newlines are always accepted

Options

Option Description Note
-w cols during encoding, wrap line after cols characters default: 76
-d decode instead of encoding (which is default)
-i ignore unrecognized bytes to allow distorted data to be decoded

Examples

# encode and decode
echo "test" | base64 | base64 -d
0
1

basenc

basenc transforms data read from a file or standard input into / from common encoding forms: base64, base32, base2, Z85 and variations.

basenc encoding [option]... [file]
0

Encoding options

All these apply to the encoding option. In addition basenc inherits the -d, -w, -i options from base64 and base32.

Encoding Description
--base64 equivalent to base64
--base64url file-and-url-safe base64
--base32 equivalent to base32
--base32hex Extended Hex Alphabet base32
--base16 base16 (hex)
--base2lsbf binary string with the least significant bit of every byte first
--base2msbf binary string with the most significant bit of every byte first
--z85 Z85 - ZeroMQ Z85 spec

Examples

# string to hex
echo "test" | basenc --base16
0
1

head prints the first n lines, or all but the last n lines of each file or standard input.

head [option]... [file]...
0

Notes

  • if more than one file is given, head will print a one-line header before the output of each file (==> filename <==< code="">)

Options

Options Description Note
-n [-]num print the first num lines, if num is a negative number, then print all but the last num lines of each file 10 by default
-c [-]num similar to -n but use bytes instead of lines you can use the multiplicative suffixes b, KB, K, MB, M, G, GB
-q don't print the filename headers
-v always print the filename headers
-z use ASCII NUL instead of newline on input and output useful with find -print0, xargs -0

tail

tail prints the last n lines, or from the start of the file after num lines, for each file.

tail [option]... [file]...
0

Notes

  • if more than o0ne file is given, tail will print a one-line header before the output of each file (==> filename <==< code="">)

Options

Option Description Note
-n [+]num print the last num lines, if num is prefixed with a +, print from line num to the end of the file
-c [+]num similar to -n but use bytes instead of lines you can use the multiplicative suffixes b, KB, K, MB, M, G, GB
-f loop forever, trying to read more characters from the end of the files
-F like -f, but attempt to reopen a file when it's removed
--pid=pid terminate tail when the process with the specified pid dies
-q never print filename headers
--retry keep trying to open the specified file
-s number set the number of seconds to wait between iterations default 1.0
-v always print filename headers
-z use ASCII NUL instead of newline on input and output

Examples

# watch lines being appended to a log file
tail -f logs/some.log
0
1

wc

wc (print newline, word and byte counts) counts the number of bytes, charactes, words and newlines in each given file or standard input.

wc [option]... [file]...
0

Notes

  • for files provided as arguments, wc will print the filename after the counts
  • the width of the count fields depends on the input, however if only one count is printed, it's guaranteed to be printed without leading spaces
  • by default wc prints three counts: newline, words and byte counts

Options

Option Description
-c print the byte count
-m print the character count
-w print the word count
-l print the line count
-L print the maximum line length
--files0-from=file process files listed in file, separated by a zero byte

Examples

# find the lenght of the longest line in any .c or .h file
find . -name '*.[ch]' -print0 | wc -L --files0-from=- | tail -n1
0
1

cksum

cksum outputs the CRC checksum, the number of bytes and the filename for each file, or for stdin

cksum [option]... [file]...
0

md5sum

md5sum computes the md5 hash for each file or stdin, and prints the hash and the filename (if provided through arguments).

md5sum [option]... [file]...
0

Options

Option Description Note
-c read the output of md5sum as input, and validate that the provided hashes match the files
--ignore-missing when verifying checksums ignore missing files
--quit when verifying checksums don't generate an OK message
--strict when verifying, if one of the input lines is invalid, exit with nonzero status
-z output a zero byte at the end of each line instead of a newline

Examples

# compute the md5 sum of all files
checksums=$(md5sum ./*)

# validate the checksums
md5sum -c <<< "$checksums"
0
1
2
3
4

b2sum

b2sum computes the BLAKE2 512-bit checksum. It has the same usage and options as md5sum.

Additional options

Option Description Note
-l num change the default digest length to num bits num must be a multiple of 8

sha*sum

Each of these utilities computes and verifies its variant of SHA digest. All have the same usage and options as md5sum.

Variants

  • sha1sum
  • sha224sum
  • sha256sum
  • sha384sum
  • sha512sum
sha1sum [option]... [file]...
sha224sum [option]... [file]...
# ...
0
1
2

Resources