Skip to content

Commit 4891773

Browse files
committed
Split cat, tac, and others, index od and hd
1 parent d96200d commit 4891773

14 files changed

+302
-280
lines changed

README.md

+47-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Linux information and cheatsheets.
44

55
Includes Linux concepts and utilities that work on Linux, not necessarily in the LSB.
66

7-
##Index
7+
## Index
88

99
Important files:
1010

@@ -40,31 +40,15 @@ Base topics:
4040
- [Special files](special-files.md): `proc`, `dev` and `sys` filesystems.
4141
- [Terminal](terminal.md): terminal emulators, ANSI escapes, control characters.
4242

43-
Media video, games, etc.) file types, viewers, editors, capture, synthesizers:
43+
Basic stream and file manipulation:
4444

45-
- [Audio](audio/): audio, music, sound.
46-
- [Book](book.md): PDF, DJVU.
47-
- [Compression](compression.md): Zip, tar, gzip, 7z.
48-
- [Dictionary](dictionary.md): dictionary formats.
49-
- [Game](game.md): games, emulation.
50-
- [File sharing](files-sharing.md): Soulseek, Dropbox.
51-
- [Image](image/): images, photos.
52-
- [Markup](markup/): Markdown, RST. Focus on command line interface and extensions.
53-
- [Video](video.md): videos, films, subtitles.
45+
- [cat](cat.md)
46+
- [head](head.md)
47+
- [tac](tac.md)
48+
- [tail](tail.md)
49+
- [truncate](truncate.md)
5450

55-
Generic data formats:
56-
57-
- [JSON](json.md)
58-
- [Unicode](unicode.md)
59-
- [XML](xml/)
60-
61-
Markup compilers:
62-
63-
- [Kramdown](kramdown/)
64-
- [Pandoc](pandoc/)
65-
- [RST](rst/)
66-
67-
File and directory related utilities:
51+
File and directory:
6852

6953
- [du](du.md)
7054
- [fdupes](fdupes.md)
@@ -73,12 +57,23 @@ File and directory related utilities:
7357
- [ls](ls.md)
7458
- [tree](tree.md)
7559

76-
Programming tools:
60+
Diff:
61+
62+
- [comm](comm.md)
63+
- [cmp](cmp.md)
64+
65+
Binary data viewers:
66+
67+
- [hd](hd.md)
68+
- [hexdump](hexdump.md)
69+
- [od](od.md)
70+
71+
Programming:
7772

7873
- [ack](ack.sh)
7974
- [Compile](compile/): compilation process, dynamic libraries.
8075

81-
Process-related tools:
76+
Processes:
8277

8378
- [chroot](chroot.sh)
8479
- [env](env.sh)
@@ -96,7 +91,31 @@ Process-related tools:
9691
- [ulimit](ulimit.md)
9792
- [wait](wait.sh)
9893

99-
Other tools:
94+
Media video, games, etc.) file types, viewers, editors, capture, synthesizers:
95+
96+
- [Audio](audio/): audio, music, sound.
97+
- [Book](book.md): PDF, DJVU.
98+
- [Compression](compression.md): Zip, tar, gzip, 7z.
99+
- [Dictionary](dictionary.md): dictionary formats.
100+
- [Game](game.md): games, emulation.
101+
- [File sharing](files-sharing.md): Soulseek, Dropbox.
102+
- [Image](image/): images, photos.
103+
- [Markup](markup/): Markdown, RST. Focus on command line interface and extensions.
104+
- [Video](video.md): videos, films, subtitles.
105+
106+
Generic data formats:
107+
108+
- [JSON](json.md)
109+
- [Unicode](unicode.md)
110+
- [XML](xml/)
111+
112+
Markup compilers:
113+
114+
- [Kramdown](kramdown/)
115+
- [Pandoc](pandoc/)
116+
- [RST](rst/)
117+
118+
Misc:
100119

101120
- [eval](eval.sh)
102121

@@ -107,7 +126,7 @@ Related subjects in other repositories:
107126
- [C++ Cheat](https://github.com/cirosantilli/cpp-cheat): contains some compilation / language heavy subjects like the POSIX C API or generation of dynamic libraries.
108127
- [Networking Cheat](https://github.com/cirosantilli/networking-tutorial): networking tools and protocols: HTTP, SSH, curl Apache.
109128

110-
##How to search for keywords
129+
## How to search for keywords
111130

112131
If you know what you are searching for, use the following methods to search by keyword.
113132

cat.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##cat
2+
3+
POSIX 7: <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cat.html>
4+
5+
Concatenate files to stdout.
6+
7+
```sh
8+
echo asdf > a
9+
echo qwer > b
10+
11+
[ "$(cat a)" = 'asdf' ] || exit 1
12+
[ "$(cat b)" = 'qwer' ] || exit 1
13+
14+
[ "$(cat a b)" = "$(printf 'asdf\nqwer'") ] || exit 1
15+
```
16+
17+
Stdin:
18+
19+
```sh
20+
[ "$(echo a | cat)" = 'a' ] || exit 1
21+
```

cmp.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# cmp
2+
3+
GNU Diffutils package.
4+
5+
Compares F and G byte by byte, until first difference is found.
6+
7+
cmp "$F" "$G"
8+
9+
If equal, print nothing.
10+
11+
Else, print location of first difference.
12+
13+
## s
14+
15+
Silent
16+
17+
Return status `0` if equal, `1` otherwise.
18+
19+
Prints nothing.
20+
21+
cmp -s "$F" "$G"
22+
if [ $? -eq 1 ]; then
23+
echo neq
24+
else
25+
echo eq
26+
fi

comm.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# comm
2+
3+
Ordered list diff.
4+
5+
POSIX 7. <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/comm.html>
6+
7+
Coreutils package.
8+
9+
cd "`mktemp -d`"
10+
echo -e "a\nc" > a
11+
echo -e "b\nc" > b
12+
comm a b
13+
14+
Output:
15+
16+
a
17+
\tb
18+
\t\tc
19+
20+
This produces 3 columns by indentation:
21+
22+
- lines only in `a`
23+
- lines only in `b`
24+
- lines in both

hd.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# hd
2+
3+
`od` alternative with better defaults, but not POSIX 7.
4+
5+
Uses saner hexadecimal defaults and shows ASCII side by side.
6+
7+
Saner than hexdump.
8+
9+
Very close to hexdump, but also shows ASCII visualization on the side of hexa visualization.
10+
11+
Example:
12+
13+
echo -en {a..z} "\n \x01" | hd
14+
15+
Output:
16+
17+
00000000 61 20 62 20 63 20 64 20 65 20 66 20 67 20 68 20 |a b c d e f g h |
18+
00000010 69 20 6a 20 6b 20 6c 20 6d 20 6e 20 6f 20 70 20 |i j k l m n o p |
19+
00000020 71 20 72 20 73 20 74 20 75 20 76 20 77 20 78 20 |q r s t u v w x |
20+
00000030 79 20 7a 20 0a 20 01 |y z . .|
21+
00000037
22+
23+
Non ASCII and whitespace chars or control chars such as newline or `\x01` are represented as dots on the ASCII side notation.
24+
25+
Offsets are in hexadecimal: 00, 10, 20.

head.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# head
2+
3+
POSIX 7: <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/head.html>
4+
5+
Keep only 10 first lines:
6+
7+
seq 20 | head
8+
9+
Keep only 2 first lines:
10+
11+
[ "$(printf '1\n2\n3\n4\n' | head -n2)" = "$(printf '1\n2\n')" ] || exit 1
12+
13+
2 first bytes:
14+
15+
[ "$(echo -en 'abc' | head -c 2)" = 'ab' ] || exit 1
16+
17+
## GNU extensions
18+
19+
Remove last two bytes:
20+
21+
[ "$(echo -en 'abc' | head -c -2)" = 'a' ] || exit 1

hexdump.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# hexdump
2+
3+
`od` alternative with better defaults, but not POSIX 7.
4+
5+
Uses saner hexadecimal defaults.
6+
7+
`hd` has even saner defaults.
8+
9+
View bytes in hexadecimal.
10+
11+
echo -n abc | hexdump -C
12+
13+
Options:
14+
15+
- `-C` : see bytes in hexadecimal
16+
- `-n32` : only 32 bytes
17+
- `-s32` : start at byte 32
18+
- `-v`: show duplicate lines
19+
- `-e '1/1 " %02X"'`: format string

od.md

+8-54
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
#od
1+
# od
22

3-
`od` and similar tools used to view binary data.
3+
View binary data.
4+
5+
POSIX 7: <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/od.html>
6+
7+
Saner non-POSIX 7 alternatives: `hd`, `hexdump`.
48

59
Very useful for viewing binary data which contains values which cannot be interpreted as some character set (ASCII, UTF-8) that can be printed to terminal screen.
610

711
You have some fun exploring things such as:
812

9-
- executables such as elf files
13+
- executables such as elf files
1014

11-
- partition tables:
15+
- partition tables:
1216

1317
sudo hd -n 512 /dev/sda
1418

15-
POSIX 7
16-
1719
Octal dump.
1820

1921
View byte values byte by byte in octal and other bases.
@@ -263,51 +265,3 @@ Output:
263265
##-N
264266

265267
Maximum number of bytes to read.
266-
267-
#hd
268-
269-
Very similar to od.
270-
271-
Uses saner hexadecimal defaults and shows ASCII side by side.
272-
273-
Not POSIX 7.
274-
275-
Saner than hexdump.
276-
277-
Very close to hexdump, but also shows ASCII visualization on the side of hexa visualization.
278-
279-
Example:
280-
281-
echo -en {a..z} "\n \x01" | hd
282-
283-
Output:
284-
285-
00000000 61 20 62 20 63 20 64 20 65 20 66 20 67 20 68 20 |a b c d e f g h |
286-
00000010 69 20 6a 20 6b 20 6c 20 6d 20 6e 20 6f 20 70 20 |i j k l m n o p |
287-
00000020 71 20 72 20 73 20 74 20 75 20 76 20 77 20 78 20 |q r s t u v w x |
288-
00000030 79 20 7a 20 0a 20 01 |y z . .|
289-
00000037
290-
291-
Non ASCII and whitespace chars or control chars such as newline or `\x01` are represented as dots on the ASCII side notation.
292-
293-
Offsets are in hexadecimal: 00, 10, 20.
294-
295-
#hexdump
296-
297-
Very similar to od, but not POSIX 7.
298-
299-
Uses saner hexadecimal defaults.
300-
301-
`hd` has even saner defaults.
302-
303-
View bytes in hexadecimal.
304-
305-
echo -n abc | hexdump -C
306-
307-
Options:
308-
309-
- `-C` : see bytes in hexadecimal
310-
- `-n32` : only 32 bytes
311-
- `-s32` : start at byte 32
312-
- `-v`: show duplicate lines
313-
- `-e '1/1 " %02X"'`: format string

sed.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -eu
33

44
# TODO make all asserts pass
55

6-
# POSIX 7
6+
# POSIX 7 <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html>
77

88
# Stream EDitor
99

tac.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# tac
2+
3+
`cat` reversed line-wise.
4+
5+
Coreutils.
6+
7+
[ "$(printf "a\nb\n" | tac)" = "$(printf "b\na")" ] || exit 1
8+
9+
Things get messy if the input does not end in newline:
10+
11+
[ "$(printf "a\nb" | tac)" = "$(printf "ba")" ] || exit 1

tail.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# tail
2+
3+
POSIX 7:
4+
5+
Opposite of head.
6+
7+
Show last 10 lines:
8+
9+
seq 20 | tail
10+
11+
Keep only 2 last lines:
12+
13+
[ "$(printf "1\n2\n3\n4\n" | tail -n2)" = "$(printf "3\n4\n")" ] || exit 1
14+
15+
## GNU Coreutils extensions
16+
17+
Keep only lines from the Nth onwards:
18+
19+
[ "$(printf "1\n2\n3\n4\n" | tail -n +2)" = "$(printf "2\n3\n4\n")" ] || exit 1
20+
[ "$(printf "1\n2\n3\n4\n" | tail -n +3)" = "$(printf "3\n4\n")" ] || exit 1

0 commit comments

Comments
 (0)