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 --strip-components=1 drops the top folder when you unpack

Tar
~/tips/tip-tar-strip-components-folder

tar -xzf app-2.4.1.tar.gz -C /mnt/user/appdata/app --strip-components=1

Unpacks an archive straight into an existing folder without the extra version folder.

Release tarballs nearly always wrap everything in one folder named after the version, like app-2.4.1/. On a laptop I don’t mind. On a server, where the files should go straight into a folder that already exists, that extra level is just in the way.

mkdir -p /mnt/user/appdata/app
tar -xzf app-2.4.1.tar.gz -C /mnt/user/appdata/app --strip-components=1

--strip-components=1 removes the first part of every path while tar extracts, so app-2.4.1/config/settings.yml lands as config/settings.yml. -C makes tar change to the target folder before it starts, and that folder has to exist, hence the mkdir.

The number counts folder levels. An archive that wraps its files in two folders needs --strip-components=2, and a wrong guess scatters files into the wrong place. So I would always look first:

tar -tzf app-2.4.1.tar.gz | head

That shows the paths as they are stored, and you can count the levels before you commit to anything. One more thing about -C: the man page calls it position-sensitive, which means it only applies to the file names that come after it on the command line. For a plain extract like this one that never matters, but it can surprise you when you create an archive from several folders.

Further reading: tar(1) manual page

More Tar tips printf 'Signature: 8a477f597d28d172789f06886806bc55' > /mnt/user/appdata/app/cache/CACHEDIR.TAG All Tar tips (2) All topics

Link