Lesson 8 / 31

Finding Files & Archives (find, tar)

Locate files by name, size and age with find, and bundle or extract archives with tar and gzip.

find

find walks a directory tree and filters by name, type, size or age.

find . -name "*.log"            # by name
find /var -type f -size +100M    # large files
find . -mtime -1                 # modified in last day
find . -name "*.tmp" -delete     # act on matches

tar

tar bundles files into one archive; -z adds gzip compression. Remember: c create, x extract, t list, f file.

tar -czf backup.tar.gz projects/     # create
tar -tzf backup.tar.gz               # list
tar -xzf backup.tar.gz -C /tmp       # extract

Run find with -print first and only add -delete or -exec once the matches look right.

Quick check

Quick check: Which command extracts backup.tar.gz?

  • tar -xzf backup.tar.gz
  • tar -czf backup.tar.gz
  • tar -tzf backup.tar.gz
  • gzip -c backup.tar.gz
Answer

tar -xzf backup.tar.gz — `x` extracts, `z` handles gzip and `f` names the file.