Last active
August 5, 2025 03:45
-
-
Save henri/bbfc52067e5535cc1e27a803873e1e16 to your computer and use it in GitHub Desktop.
tar cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a compressed archive from a number of sources | |
tar -czf compressed-archive.tar.gz /srv/source1 /srv/source2 # ... | |
# list all .txt files in archive (better if you are hunting into large archives) | |
tar -tf archive.tar --wildcards '*.txt' | |
tar -tzf archive.tar.gz --wildcards '*.txt' | |
# it is possible to use third party tools to mount or explore a compressed tar archive | |
# eg : | |
# - https://github.com/mxmlnkn/ratarmount | |
# - https://midnight-commander.org/ (eg. mc) # better for smaller archives | |
# - sudo apt-get install archivemount # On Debian/Ubuntu systems (can be slow for larger archives) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# About : This script is designed to help you with bundling up various files and directories | |
# into a tar archive. You create a file with the items you want to pack, | |
# Then you feed this list into this script via stdin and it | |
# will package them up. This script currently has no support | |
# for directorties or files with spaces in the path. It would | |
# be trivial to add maybe in another version if needed. | |
# | |
# To use this script save it and make it executable : | |
# example : chmod 755 packing.bash | |
# | |
# Usage : cat <packing_list.txt> | ./packfiles.bash <packged.tar.gz> | |
# | |
# (C)Copyright 2025 | |
# Henri Shustak | |
# | |
# Released under the GNU GPL version 3 or later | |
# https://www.gnu.org/licenses/gpl-3.0.en.html | |
# | |
# version 1.0 - initial release | |
# version 1.1 - added support for comments in the input file for lines starting with has | |
# | |
set -e | |
# TARBALL="packed_bundle.tar.gz" | |
TARBALL="$1" | |
if [ -z "$TARBALL" ] ; then | |
echo "" | |
echo "You must specify a single argument which will be your output" | |
echo "compressed tar archive. This file will hold the files you" | |
echo "feed into this script via standard input." | |
echo "" | |
echo "Usage Example : cat packing_list.txt | $0 packaged.tar.gz" | |
echo "" | |
echo "Provide the list of files and/or directory to include via" | |
echo "standard input to this script (one file / directory pr line)." | |
echo "" | |
exit 1 | |
fi | |
if ! [[ $TARBALL == *.tar.gz ]] ; then | |
echo "" | |
echo "ERROR! : Incorrect output file name." | |
echo "" | |
echo " The name provided for the tar archive must" | |
echo " end with \".tar.gz\"" | |
echo "" | |
echo " Example file name to provide : " | |
echo " my_packing.tar.gz" | |
echo "" | |
fi | |
if [[ -e $TARBALL ]] ; then | |
echo "" | |
echo "ERROR : A packed bundle of files alrady exists : " | |
echo " $TARBALL" | |
echo "" | |
echo " If you would like to make a new one" | |
echo " then move this one out of the way." | |
echo " Alteratnivly, pick a different" | |
echo " name for your new archive." | |
echo "" | |
exit -1 | |
fi | |
# Read file names from stdin into an array | |
FILES=() | |
while IFS= read -r line ; do | |
# skip empty or whitespace-only lines | |
[[ -z "${line// }" ]] && continue | |
# skip lines which start with hash (comment) | |
[[ $line =~ ^# ]] && continue | |
FILES+=("$line") | |
done | |
if [ "${#FILES[@]}" -eq 0 ] ; then | |
echo "No files specified on stdin." | |
exit 2 | |
fi | |
# Create tarball | |
tar -czf "$TARBALL" "${FILES[@]}" | |
tar_status=$? | |
if [[ $tar_status != 0 ]] ; then | |
echo "" | |
echo "ERROR! : During archive creation" | |
echo "" | |
exit $tar_status | |
fi | |
# provide some helpful output | |
echo "" | |
echo "Move data to remote host(s) /tmp/ directory with the command : " | |
echo "scp $TARBALL user@host:/tmp/" | |
echo "" | |
echo "Unpack your packing list into specific directory :" | |
echo "mkdir /path/to/unpacked/dir" | |
echo "tar -xvzf "$TARBALL" --directory /path/to/upacked/dir" | |
echo "" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment