-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtar-sorted
executable file
·51 lines (43 loc) · 2.37 KB
/
tar-sorted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
#############################################################################
# tar-sorted: Create tar files automatically sorted by file name. #
# #
# This is particularly helpful on random-order file systems, such as ext4. #
# #
# With the Bettergist Collector, we use this to be able to roughly estimate #
# how long compressing and extracting multiple-gigabyte files with millions #
# of files will take. #
# #
# It is a drop-in replacement for `tar`, and uses the same arguments. #
# #
# Optionally, you can install the function directly into your ~/.bashrc. #
# #
# Part of HopeSeekr's BashScripts Collection #
# https://github.com/hopeseekr/BashScripts/ #
# #
# Copyright © 2024 Theodore R. Smith <[email protected]> #
# GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690 #
# #
# License: Creative Commons Attribution v4.0 International #
# #
#############################################################################
# Create tar archives sorted alphanumerically.
function tar-sorted() {
local options="$@"
local archive="$2"
local zstd_flag=""
# Check for compression flag
if [[ "$options" == *"--zstd"* ]]; then
zstd_flag="--zstd"
echo "Options (before): $options"
options="${options/--zstd/}"
echo "Options (after): $options"
set -- $options
fi
# Only pass the remaining arguments (targets) to the find command
find "${@:3}" -print0 | sort -z | tar "$zstd_flag" "$1" "$2" --no-recursion --null -T -;
}
function tar-sorted-orig() {
find "${@:3}" -print0 | sort -z | tar "$1" "$2" --no-recursion --null -T -;
};
tar-sorted "$@"