-
Notifications
You must be signed in to change notification settings - Fork 2
/
tar_sorted
39 lines (32 loc) · 1.73 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
#! /bin/sh
#----------------------------------------------------------------------
# Description: Create tar archive with files sorted by extension
# Author: Artem S. Tashkinov
# Created at: 2010-06-15 18:51:19
# 2014-04-23 02:53:33 (--numeric-owner)
# 2010-07-18 22:27:07 unknown fixes
# 2019-09-10 16:21:14 fixed "if" - must have used "test"
# 2023-02-23 14:05:52 a fix for filenames containing '\' - should this be applied to directories names as well?
# 2023-07-02 14:21:04 simplify
# Computer: localhost.localdomain.
# System: Linux 2.6.33.4-ic on i686
#
# Copyright (c) 2010 Artem S. Tashkinov All rights reserved.
#----------------------------------------------------------------------
# set -x
[ ! -d "$1" ] && echo "Gimme an existing directory name" >&2 && exit 1
[ -z "$2" ] && echo "Give me a destination filename" >&2 && exit 2
[ -f "$2" ] && echo "[$2] already exists!" >&2 && exit 3
[ -n "`find "$1" -iname '*|*'`" ] && echo "birdie is stupid :( - there are files with '|' in their names! This won't work." >&2 && exit 4
find_files()
{
#find "$1"-type f | sed 's/^\.\///;s/\(..*\)\./\1\|/' | sort -t '|' -k2 | sed 's/|/\./'
# Thanks to Neil Moore aka \ametyst at irc://irc.freenode.net/bash
find "$1" ! -type d | sed -e 's,\(.*\)/,\1|,' -e 's,\(.*|..*\)\.,\1|,' | sort -i -t\| -k 3,3 -k 2,2 --buffer-size=1G | sed -e 's,|,/,' -e 's,|,.,' | sed 's/\\/\\\\/g'
test "${PIPESTATUS[0]}" != "0" && return 1
find "$1" -depth -type d || return 1 # Saving directories timestamps, you SHOULD run tar with --no-recursion option
}
filelist=`mktemp` || exit 5
find_files "$1" > "$filelist" || exit 6
tar --no-recursion --files-from="$filelist" -acf "$2" # --numeric-owner
/bin/rm "$filelist"