brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tar --one-top-level keeps a messy archive from spilling into the current folder

Archive
~/tips/tip-tar-one-top-level

tar -xzf archive.tgz --one-top-level

Extracts into a new folder named after the archive, so loose files stay together.

Some tarballs have no top folder. You unpack one in your Downloads folder and fifty files land next to everything else. I’d rather not clean that up by hand, and GNU tar has a flag for it.

tar -xzf archive.tgz --one-top-level

Without a value, tar creates a folder named after the archive, minus the compression suffix, and extracts into that. With a value you pick the name yourself:

tar -xzf archive.tgz --one-top-level=mydir

Here is what it did with an archive whose two files had no folder around them. The archive was called bomb.tgz:

$ tar -xzf bomb.tgz --one-top-level
$ ls -R .
.:
bomb

./bomb:
one  two

With --one-top-level=mydir the same files ended up in mydir instead. It combines with -C, which picks where the new folder is made: tar -xzf other.tgz -C ~/work --one-top-level creates ~/work/other. If the archive is unknown to you, list it first with tar -tf and see whether it has a top folder at all.

Caveat: if the archive already has a folder inside it, you get a folder around that folder. On GNU tar 1.35 a file called other.tgz that contained proj/ unpacked to other/proj/. To drop a folder you do not need, use --strip-components instead. The flag is GNU tar. The tar on macOS is bsdtar with a different option list, so I’d check man tar there first.

Further reading: GNU tar manual

More Archive tips find logs -name '*.log' -print0 | tar --null -T – -czf logs.tgz tar -cf ../recent.tar --newer-mtime='1 day ago' . tar -df backup.tar tar --sort=name --mtime=2026-01-01T00:00:00Z \ tar --exclude-vcs -czf project.tar.gz project/ All Archive tips (7) All topics

Link