3.5. Finding and Handling Data
Files accumulate faster than memory, so Debian ships two complementary
answers to "where is it": find, which walks the live filesystem and
matches any metadata, and plocate, which consults a prebuilt index of
names. The section ends with archives - the standard ways to move a tree
of files as one object.
3.5.1. Finding Files by Name, Type, and Metadata
find takes a starting path and a list of tests, walks everything under
that path, and prints the names where all the tests hold. Build a small
tree that exercises each kind of test. The setup creates three logs, a
2-megabyte binary, a text note, an empty directory, and a marker file, and
backdates one log to three days ago so the time tests have something to
find. All of it is silent:
$ mkdir -p find/logs find/data find/empty
$ printf "old\n" > find/logs/old.log
$ printf "new\n" > find/logs/new.log
$ truncate -s 2M find/data/large.bin
$ printf "note\n" > find/data/note.txt
$ touch -d "3 days ago" find/logs/old.log
$ touch find/marker
$ printf "fresh\n" > find/logs/fresh.log
truncate -s 2M creates a 2-megabyte file of zeros without writing two
megabytes. The marker was touched just before fresh.log was written, so
the two have slightly different modification times. The simplest test
matches names - quote the pattern, or the shell expands it first
(Section 3.2.1):
$ find /home/john/debian-handbook/shell-basics/find -name "*.log" -type f | sort
/home/john/debian-handbook/shell-basics/find/logs/fresh.log
/home/john/debian-handbook/shell-basics/find/logs/new.log
/home/john/debian-handbook/shell-basics/find/logs/old.log
find test or option |
Effect |
|---|---|
-name pattern |
Basename matches the glob pattern (-iname ignores case). |
-type f/d/l |
Regular file / directory / symbolic link. |
-size +2M, -size -10k |
Larger than / smaller than the given size (c bytes, k, M, G). |
-mtime +1 |
Modified more than one day ago (-mtime -1: within the day). |
-newer file |
Modified after file was. |
-user name, -perm mode |
Owned by name / has the given permissions. |
-empty |
Empty file or empty directory. |
-maxdepth N |
Descend at most N levels below the starting point. |
Type alone answers "what directories exist here?":
$ find /home/john/debian-handbook/shell-basics/find -type d | sort
/home/john/debian-handbook/shell-basics/find
/home/john/debian-handbook/shell-basics/find/data
/home/john/debian-handbook/shell-basics/find/empty
/home/john/debian-handbook/shell-basics/find/logs
Size and time tests are where find starts replacing an afternoon of
clicking. Find every file over one megabyte, printing size and path:
$ find /home/john/debian-handbook/shell-basics/find -type f -size +1M -printf "%s %p\n"
2097152 /home/john/debian-handbook/shell-basics/find/data/large.bin
Files not touched for more than a day - the backdated log:
$ find /home/john/debian-handbook/shell-basics/find -type f -mtime +1
/home/john/debian-handbook/shell-basics/find/logs/old.log
Files newer than the marker:
$ find /home/john/debian-handbook/shell-basics/find -type f -newer /home/john/debian-handbook/shell-basics/find/marker
/home/john/debian-handbook/shell-basics/find/logs/fresh.log
Ownership and permission tests select by who can use a file. Every file john owns that he can read:
$ find /home/john/debian-handbook/shell-basics/find -type f -user john -perm -u+r | sort
/home/john/debian-handbook/shell-basics/find/data/large.bin
/home/john/debian-handbook/shell-basics/find/data/note.txt
/home/john/debian-handbook/shell-basics/find/logs/fresh.log
/home/john/debian-handbook/shell-basics/find/logs/new.log
/home/john/debian-handbook/shell-basics/find/logs/old.log
/home/john/debian-handbook/shell-basics/find/marker
-perm -u+r means "at least read permission for the owner", the usual
intent; -perm u+r without the dash would mean exactly that mode. The
empty directory is a test of its own:
$ find /home/john/debian-handbook/shell-basics/find -type d -empty
/home/john/debian-handbook/shell-basics/find/empty
-printf formats the results directly - %y type, %s size in bytes,
%p path - which turns find into a reporting tool rather than just a
lister:
$ find /home/john/debian-handbook/shell-basics/find -maxdepth 2 -printf "%y %s %p\n" | sort
d 4096 /home/john/debian-handbook/shell-basics/find
d 4096 /home/john/debian-handbook/shell-basics/find/data
d 4096 /home/john/debian-handbook/shell-basics/find/empty
d 4096 /home/john/debian-handbook/shell-basics/find/logs
f 0 /home/john/debian-handbook/shell-basics/find/marker
f 2097152 /home/john/debian-handbook/shell-basics/find/data/large.bin
f 4 /home/john/debian-handbook/shell-basics/find/logs/new.log
f 4 /home/john/debian-handbook/shell-basics/find/logs/old.log
f 5 /home/john/debian-handbook/shell-basics/find/data/note.txt
f 6 /home/john/debian-handbook/shell-basics/find/logs/fresh.log
Two metadata tests reach back into Section 3.3.4. A file can be found by inode number - useful when a name is awkward but the identity is known:
$ cd find && inode=$(stat -c %i logs/new.log); find . -inum "$inode"
./logs/new.log
And the hard-link test finds files that have more than one name, which is
how you discover that two paths share storage. Still in find, make a hard
link to the note, then ask for every multiply-linked file in the tree:
$ ln data/note.txt note-hard.txt && find . -type f -links +1 | sort
./data/note.txt
./note-hard.txt
Both names are listed, because the inode is multiply referenced - there is no primary among them. Return to the workspace root:
$ cd ~/debian-handbook/shell-basics
For pure name lookups over the whole system, plocate consults a database
instead of walking trees, which is dramatically faster and slightly out of
date. Build the index first (it is normally rebuilt nightly by a timer):
$ sudo updatedb
The command prints nothing; it indexes the filesystem into the locate database. Query it:
$ plocate -l 3 bashrc
/etc/bash.bashrc
/etc/skel/.bashrc
/home/john/.bashrc
-l 3 caps the listing at three lines. locate is a symlink to
plocate on Debian, so older finger-habits work.
find |
plocate |
|
|---|---|---|
| Source of truth | The live filesystem | A database from the last updatedb |
| Can match | Name, type, size, time, owner, permissions, inode | Names only |
| Cost | Walks every directory under the start point | An index lookup |
| Freshness | Always current | As of the last index rebuild |
3.5.2. Combining Search Results with Commands Safely
Back at the workspace root: a list of paths is rarely the goal - the goal
is to count, copy, or delete what was found. The safe way to hand find's
output to another command must survive the one thing that breaks naive
pipelines: file names containing spaces, quotes, or newlines. Create a
fixture with exactly that:
$ mkdir -p "safe/space dir"
$ printf "one\n" > "safe/space dir/a file.txt"
$ printf "two\n" > "safe/space dir/b file.txt"
$ printf "three\n" > safe/plain.txt
The portable answer is NUL separation. -print0 terminates each result
with a zero byte instead of a newline, and xargs -0 splits on zero bytes,
so no valid file name can be misread:
$ find /home/john/debian-handbook/shell-basics/safe -type f -name "*.txt" -print0 | sort -z | xargs -0 -I{} printf "<%s>\n" "{}"
</home/john/debian-handbook/shell-basics/safe/plain.txt>
</home/john/debian-handbook/shell-basics/safe/space dir/a file.txt>
</home/john/debian-handbook/shell-basics/safe/space dir/b file.txt>
Each bracketed line is one file name, intact. sort -z sorts NUL-separated
records, and xargs -0 -I{} inserts each whole name where {} appears.
The older and equally safe route avoids the pipe entirely - -exec runs a
command on the results inside find itself:
$ find /home/john/debian-handbook/shell-basics/safe -type f -name "*.txt" -exec wc -l {} +
1 /home/john/debian-handbook/shell-basics/safe/space dir/a file.txt
1 /home/john/debian-handbook/shell-basics/safe/space dir/b file.txt
1 /home/john/debian-handbook/shell-basics/safe/plain.txt
3 total
The trailing + batches as many names as fit into one invocation of wc
- hence the total line; -exec command {} \; instead runs the command
once per file, which is slower but sometimes necessary.
To see precisely what the naive form gets wrong, run it deliberately. A
newline-separated pipe into plain xargs splits every name on
whitespace:
$ find /home/john/debian-handbook/shell-basics/safe -type f -name "*.txt" -print | xargs -n1 printf "<%s>\n"
</home/john/debian-handbook/shell-basics/safe/space>
<dir/a>
<file.txt>
</home/john/debian-handbook/shell-basics/safe/space>
<dir/b>
<file.txt>
</home/john/debian-handbook/shell-basics/safe/plain.txt>
Six "files" from three: the space in the directory name split each path
into fragments. A deletion built this way would fail on the fragments and,
depending on the command, could act on the wrong names. That is why the
NUL-separated or -exec forms are the ones to write by habit.
Deletion deserves the warning twice. rm driven by a search is
irreversible: check the list before the delete, and keep the operation
inside a directory you own. Create two files, one of which should go:
$ mkdir -p "prune/space dir"
$ printf "x\n" > "prune/space dir/a file.tmp"
$ printf "y\n" > prune/keep.txt
Build the habit of running the search twice: first with -print in
place of the deletion, to read exactly the names it selects, and only then
- after checking them - with the deletion. The -- terminator marks the
end of rm's options so a file literally named like an option cannot
become one:
$ find /home/john/debian-handbook/shell-basics/prune -type f -name "*.tmp" -print0 | xargs -0 rm --
Nothing prints. What remains is the untouched file:
$ find /home/john/debian-handbook/shell-basics/prune -type f -printf "%p\n"
/home/john/debian-handbook/shell-basics/prune/keep.txt
3.5.3. Creating and Inspecting Archives and Compressed Files
An archive bundles many files into one, preserving names, permissions, and
the directory structure; compression then shrinks the bundle. On Debian the
two are usually combined in one tar invocation. Set up a directory tree
worth archiving:
$ mkdir -p archive/src/sub
$ printf "alpha\n" > archive/src/a.txt
$ printf "beta\n" > archive/src/sub/b.txt
Create a gzip-compressed tar archive. -C src . makes tar archive the
contents of src (as ./a.txt, ./sub/...) rather than the directory
name itself, which keeps the extraction free of an extra nesting level:
$ cd archive && tar -czf sample.tar.gz -C src .
tar option |
Effect |
|---|---|
-c / -t / -x |
Create / list / extract an archive. |
-f file |
Read or write file (otherwise a tape device). |
-z / -J / --zstd |
Filter through gzip / xz / zstd. |
-C dir |
Change to dir before reading or writing files. |
-v |
List names as they are processed. |
The command is silent; the archive is a file like any other. List its contents without extracting:
$ tar -tzf /home/john/debian-handbook/shell-basics/archive/sample.tar.gz
./
./a.txt
./sub/
./sub/b.txt
Extract it into a fresh directory and compare trees:
$ mkdir -p /home/john/debian-handbook/shell-basics/archive/out && tar -xzf /home/john/debian-handbook/shell-basics/archive/sample.tar.gz -C /home/john/debian-handbook/shell-basics/archive/out
diff -r compares two directory trees recursively and prints nothing when
they match:
$ diff -r /home/john/debian-handbook/shell-basics/archive/src /home/john/debian-handbook/shell-basics/archive/out
The silent diff is success. gzip -l reports the compression of a
gzip file:
$ gzip -l /home/john/debian-handbook/shell-basics/archive/sample.tar.gz
compressed uncompressed ratio uncompressed_name
191 10240 98.3% /home/john/debian-handbook/shell-basics/archive/sample.tar
The uncompressed size is the tar member (10240 bytes - tar pads to
512-byte blocks), not the original files; the compressed size varies by a
byte or two with the timestamps tar records in the member header. The
file command identifies a compressed stream, which is how you check what
you actually have before choosing the extraction flags:
$ file /home/john/debian-handbook/shell-basics/archive/sample.tar.gz
/home/john/debian-handbook/shell-basics/archive/sample.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 10240
The same archive can be made with any of the filters. xz, with -J:
$ tar -cJf sample.tar.xz -C src .
$ file /home/john/debian-handbook/shell-basics/archive/sample.tar.xz
/home/john/debian-handbook/shell-basics/archive/sample.tar.xz: XZ compressed data, checksum CRC64
And zstd, with --zstd:
$ tar --zstd -cf sample.tar.zst -C src .
$ file /home/john/debian-handbook/shell-basics/archive/sample.tar.zst
/home/john/debian-handbook/shell-basics/archive/sample.tar.zst: Zstandard compressed data (v0.8+), Dictionary ID: None
The zip format is a container rather than a compression scheme: it stores
each file separately compressed, which is why a zip can be listed and
its members extracted individually without reading the whole archive. That
random access, plus its universal support on non-Unix systems, is its
reason to exist:
$ zip -qr sample.zip src
$ unzip -l /home/john/debian-handbook/shell-basics/archive/sample.zip
Archive: /home/john/debian-handbook/shell-basics/archive/sample.zip
Length Date Time Name
--------- ---------- ----- ----
0 2026-11-18 08:20 src/
6 2026-11-18 08:20 src/a.txt
0 2026-11-18 08:20 src/sub/
5 2026-11-18 08:20 src/sub/b.txt
--------- -------
11 4 files
Each format also verifies its own integrity - an archive that transfers across a network should be tested before it is trusted, and all four formats now exist to test together:
$ gzip -t /home/john/debian-handbook/shell-basics/archive/sample.tar.gz; echo "gzip=$?"; xz -t /home/john/debian-handbook/shell-basics/archive/sample.tar.xz; echo "xz=$?"; zstd -q -t /home/john/debian-handbook/shell-basics/archive/sample.tar.zst; echo "zstd=$?"; unzip -tq /home/john/debian-handbook/shell-basics/archive/sample.zip; echo "zip=$?"
gzip=0
xz=0
zstd=0
No errors detected in compressed data of /home/john/debian-handbook/shell-basics/archive/sample.zip.
zip=0
Every test passed: a status of 0 and no complaint means the compressed data is intact.
Extract the zip with -d naming the destination, then compare:
$ mkdir -p /home/john/debian-handbook/shell-basics/archive/unzipout && unzip -q /home/john/debian-handbook/shell-basics/archive/sample.zip -d /home/john/debian-handbook/shell-basics/archive/unzipout
$ diff -r /home/john/debian-handbook/shell-basics/archive/src /home/john/debian-handbook/shell-basics/archive/unzipout/src
Both silent: extracted and original match. A checksum records an archive's identity in one line - the value to compare after a transfer:
$ sha256sum /home/john/debian-handbook/shell-basics/archive/sample.tar.gz
14d90410632bb203b6f5bfc94243230e83682e247b99e9a26ab693b7195aaeec /home/john/debian-handbook/shell-basics/archive/sample.tar.gz
sha256sum prints the SHA-256 digest of the file; the same command on the
receiving end must print the same digest, or the copy is not the file you
sent. Chapter 2 used the same principle for
installation images.
Which compression to choose? The formats differ in ratio, speed, and where they are expected to be understood. For a concrete anchor, the same synthetic text file - 8000 repeated lines, 304,000 bytes, with its modification time fixed so the results are reproducible - was archived and compressed once each on the example system; the numbers below are that single observation, not a benchmark:
| Format | Command | Observed size | Character |
|---|---|---|---|
| uncompressed tar | tar -cf |
307,200 bytes | no compression; tar pads to block boundaries |
| gzip | tar -czf |
1,054 bytes | fast, moderate ratio, understood everywhere |
| xz | tar -cJf |
296 bytes | best ratio, slower to compress and decompress |
| zstd | tar --zstd -cf |
166 bytes | fast in both directions, good ratio |
| zip | zip -qr |
1,108 bytes | per-file compression; random access; universal on other systems |
Highly repetitive text compresses extremely well in every format, so treat
the relative ordering - zstd and xz well ahead of gzip here - as the
pattern, not the proportions. Debian itself uses these formats at scale:
the payload of every package in the archive is a data.tar member, xz
compressed in the packages on this system (the package format also permits
zstd). For archives you create, gzip remains the safest interoperability
choice, and zstd is the practical default when both ends run current
tools; Chapter 15 revisits the choice for backups,
where integrity checking and incremental saves matter more than raw ratio.