These aren’t real files on disk. The kernel answers reads and writes on them itself.
The files
| File | Read gives | Write does |
|---|---|---|
/dev/null | Nothing (end of file at once) | Discards everything |
/dev/zero | Endless zero bytes | Discards |
/dev/urandom | Endless random bytes, never blocks | Mixes the data into the pool |
/dev/random | Random bytes; on kernels since 5.6 the same as urandom once initialised | Mixes into the pool |
/dev/full | Zero bytes | Always fails with “No space left on device” |
/dev/stdin, stdout, stderr | Links to file descriptors 0, 1, 2 | |
/dev/tty | The controlling terminal | Writes to it, even when stdout is redirected |
/dev/null
command > /dev/null # hide output
command 2> /dev/null # hide errors
command > /dev/null 2>&1 # hide both
command &> /dev/null # same, bash
grep -q pattern file # often better than > /dev/null: stops at the first match
: > app.log # truncate a file (same as cat /dev/null > app.log)
cp /dev/null app.log # also truncates
ssh [email protected] 'cmd' < /dev/null # stop ssh reading stdin in loopsTruncating with : > file keeps the inode, so a process writing to the file keeps working and the space is freed. Deleting an open file doesn’t free space.
/dev/zero
# a 1 GiB file of zeros
dd if=/dev/zero of=test.img bs=1M count=1024 status=progress
fallocate -l 1G test.img # instant, same size, no writes
# swap file
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
# sparse file: 10 GiB apparent, almost no disk used
truncate -s 10G sparse.img
du -h --apparent-size sparse.img; du -h sparse.imgfallocate swap files work on ext4 and on XFS with current kernels; dd is the safe choice when swapon rejects one. Btrfs needs its own command: btrfs filesystem mkswapfile.
/dev/urandom
head -c 32 /dev/urandom | base64 # 32 random bytes as text
tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 24; echo # alphanumeric password
od -An -N4 -tu4 /dev/urandom # a random 32-bit number
dd if=/dev/urandom of=random.bin bs=1M count=100 # incompressible test data
shuf -n1 --random-source=/dev/urandom words.txt/dev/urandom is the right choice for keys and passwords. The old advice to wait on /dev/random no longer applies on modern kernels.
Wipe a disk
lsblk # be sure which device it is
dd if=/dev/zero of=/dev/sdX bs=4M status=progress oflag=direct
dd if=/dev/urandom of=/dev/sdX bs=4M status=progress # slower, random
wipefs -a /dev/sdX # only remove filesystem signatures
blkdiscard /dev/nvme0n1 # SSD/NVMe: discard every blockOn SSDs, overwriting doesn’t reach every cell. Use the drive’s secure erase (nvme format -s1, hdparm --security-erase) or full-disk encryption from the start.
Test with them
# rough sequential write speed, bypassing the page cache
dd if=/dev/zero of=/srv/test.img bs=1M count=2048 oflag=direct status=progress
# rough read speed
dd if=/srv/test.img of=/dev/null bs=1M iflag=direct status=progress
# network throughput without disks in the way
dd if=/dev/zero bs=1M count=1024 | ssh [email protected] 'cat > /dev/null'
# how a program handles a full disk
echo test > /dev/full
# CPU load on one core
cat /dev/zero > /dev/null &Zeros compress perfectly, so compressed or deduplicating storage reports fake speeds; use /dev/urandom data or fio for real benchmarks.
/dev/tcp
# bash only: open a TCP connection without nc or telnet
timeout 3 bash -c '</dev/tcp/203.0.113.10/22' && echo open || echo closed
exec 3<>/dev/tcp/example.com/80
printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' >&3
cat <&3These paths don’t exist on disk; bash handles them itself.
Keep in mind
- A mistyped
of=inddoverwrites whatever it points to, with no prompt. Read the command twice. - If
/dev/nullturns into a regular file (a script ranrm /dev/nullas root), recreate it:rm /dev/null; mknod -m 666 /dev/null c 1 3.
Related
- Linux pipes cheat sheet: redirects and chaining commands
- Bash scripting cheat sheet: pipefail, traps and loops
- tee cheat sheet: saving output mid-pipe, sudo tee
Comments
Sign in with Google, GitHub or Facebook to comment.
Loading comments…
