Reading and Writing#

Beyond listing files, the operator’s everyday I/O is putting data in and getting data out. The same set of commands covers tiny configs, terabyte logs, and binary blobs because Linux treats all of them as byte streams against a path.

Reading#

Form

Effect

cat FILE

print contents

less FILE

pager

head -n N FILE / tail -n N FILE

first / last N lines

tail -f FILE

follow as the file grows

read VAR < FILE

read a single line into a shell variable

mapfile -t LINES < FILE

read all lines into a Bash array

while IFS= read -r line; do ...; done < FILE

safe line loop

Writing (redirections)#

Form

Effect

cmd > FILE

write stdout, truncating

cmd >> FILE

append stdout

cmd 2> FILE

write stderr, truncating

cmd 2>> FILE

append stderr

cmd > FILE 2>&1

stdout + stderr → file

cmd &> FILE

same (Bash short form)

cmd > /dev/null 2>&1

discard everything

cmd | tee FILE

write and keep going on stdout

cmd | tee -a FILE

append + tee

cmd < FILE

feed file as stdin

cmd <<< "string"

here-string as stdin

: > FILE

truncate FILE to zero bytes

Heredocs#

$ cat <<EOF > /etc/myapp/config.yml
$ server:
  $ port: 8080
  $ host: $HOSTNAME
$ EOF

$ cat <<'EOF' > script.sh
$ echo $LITERAL
$ EOF

$ tr a-z A-Z <<-EOF
    $ hello
    $ world
$ EOF

Writing without a process#

Form

Effect

echo "text" > FILE

simple write

printf '%s\n' "$x" > FILE

safer for arbitrary data

: > FILE

create or empty a file in one syscall

Process substitution#

Form

Effect

diff <(cmdA) <(cmdB)

treat command output as files

cmd > >(filter1) 2> >(filter2)

send streams through filters

Atomic writes#

A common safe-write pattern. Write to a temp file in the same directory, then rename.

$ cat > "$f.tmp" <<EOF
$ contents
$ EOF
$ mv "$f.tmp" "$f"

mv (rename) within the same filesystem is atomic; readers either see the old file or the new one, never a half-written one.

Editing in place#

Form

Effect

sed -i 's/old/new/g' FILE

in-place substitution (GNU)

sed -i '' 's/old/new/g' FILE

BSD / macOS (note empty arg)

perl -pi -e 's/old/new/g' FILE

portable alternative

ex -s -c '%s/old/new/g|wq' FILE

POSIX-correct in-place edit

Splitting and joining#

Form

Effect

split -l 1000 big.csv chunk_

split by line count

split -b 100M big.bin part_

split by byte count

cat part_* > big.bin

reassemble

Useful tools#

Form

Effect

dd if=SRC of=DST bs=4M status=progress

raw block copy

cp --reflink=auto

CoW copy on Btrfs / XFS / APFS

rsync -aP src/ dst/

preserve everything, with progress

shred -u FILE

overwrite then delete (limited use on CoW / journaled filesystems)

References#

  • man 1 cat, man 1 less, man 1 head, man 1 tail, man 1 read, man 1 mapfile (reading file contents).

  • man 1 tee, man 1 echo, man 1 printf, man 1 truncate (writing without a process).

  • man 1 sed, man 1 perl, man 1 ex (in-place editing).

  • man 1 split, man 1 dd, man 1 cp, man 1 rsync, man 1 shred (bulk move and copy).

  • man 1 fallocate, man 1 mv (atomic write pattern).

  • Files for the file-as-byte-stream model these tools build on.

  • Archives for compressed and bundled forms.

  • Encryption for sealing the byte stream before it lands on disk.

  • Standard I/O for the redirection mechanics behind >, >>, |, and heredocs.