svenwelzel.com - sven.capetown - sven.co.za - sven-welzel.de

i think. i conceptualise. i code. happy thoughts!
  • Home
  • About Sven Welzel
  • About synch.cc
  • Cape Town Load Shedding 2021
Home / code / bash / Create a Tar file from Directory and all Subdirectories

Posts navigation

Create a Tar file from Directory and all Subdirectories

It’s a simple one-liner that one tends to forget (that is, one that I forget as I untar more than I manually tar). So creating a tar file from directory and all subdirectories is as simple as

tar -cvf file.tar directory

To compress it on the fly, pipe it to your favourite compression application (compress, 7za, gzip) – I prefer gzip for speed, 7-zip for compression ratio on most data. So that would translate the line above into:

tar -cvf - directory | gzip -9c > file.tar.gz

or, for the corresponding 7zip version:

tar -cvf - directory | 7za a -si directory.tar.7z

From there, scp it to your location of choice! Remember that 7-zip does not maintain permissions and ownership, so tar it first before compression.
How to scp it?

scp -PPORT directory.tar.(7z|gz) USER@host:/path/to/transfer/location

If appropriate, use the keyless SSH login for this so that you need to enter the password manually in the process…

Oh, and should you want to add a $NOW or $DATE variable:

NOW="$(date +"%d-%m-%Y")"

allows you to use $NOW as a variable in your filename such as THIS_IS_MY_FILE_$NOW.tar

Just so that I have it documented – but you knew that already! 🙂

Tags: 7za, 7zip, backup, bash, gzip, keyless ssh, login, scp, ssh, tar
Sven Welzel bash, code, linux, sysadmin Share
Sven Welzel

About Sven Welzel

View all posts by Sven Welzel →

General

  • Kilimanjaro2010 – all the pictures
  • Loadshedding Cape Town Lookup Tool 2021
  • My Snapshots
  • synch.cc

System Tools

  • EMail blacklist quick-check
© 2007- 2015 Copyright Sven Welzel
www.svenwelzel.com - sven.capetown - blog.sven.co.za
hosted by
synch.cc