
答え1
引数に--xform
は任意の数のsed
置換式を指定できますが、これは非常に強力です。この場合、最後まですべてに一致し/
、何も置換しないパターンを使用します。
tar cvf allfiles.tar --xform='s|.*/||' $(<mylist.txt)
--show-transformed-names
新しい名前を表示するには追加してください。
この置換は、コマンドラインで指定されたものだけでなく、すべてのファイル名に適用されます。たとえば、ファイルがあり/a/b/c
、リストで のみを指定した場合/a
、最終的なファイル名は のみでありc
、 ではありませんb/c
。常により明示的に、置換の正確なリストを提供することができます。たとえば、次の場合です。
--xform='s|^tmp/path2/||;s|^tmp/||;s|^path3/||'
注意: 初期値/
は tar によって削除されるため ( を使用しない限り-P
)、上記の式には初期値が欠落しています。また、ディレクトリのリストは、最長一致が最初に行われるように並べ替える必要があります。そうでない場合は、tmp/path2/
すでにtmp/
削除されているため一致しません。ただし、このリストの作成を自動化できます。例:
--xform="$(sed <mylist.txt 's|[^/]*$||; s|^/||; s:.*:s|^&||;:' | sort | tr -d '\n')"
答え2
GNU を使用すると、いつでも好きなときに何度でもすぐにtar
使用できます。-C
$ tree
.
├── 1
│ └── aaa
├── 2
│ └── bbb
└── 3
└── ccc
# Caveat: the directory change is always relative to directory tar is using *at
# that time*, so the following doesn't work:
$ tar -cf foo.tar -C 1 aaa -C 2 bbb -C 3 ccc
tar: 2: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
$ tar -cf foo.tar -C 1 aaa -C ../2 bbb -C ../3 ccc
$ tar tf foo.tar
aaa
bbb
ccc
# You can avoid the special case for the first directory by always specifying
# an absolute path:
$ tar -cf foo.tar -C $(pwd)/1 aaa -C $(pwd)/2 bbb -C $(pwd)/3 ccc
# Now let's create that automatically from your file:
$ cat mylist.txt
/tmp/1/aaa
/tmp/2/bbb
/tmp/3/ccc
$ while read -r line; do printf '-C %s %s ' $(dirname "$line") $(basename "$line") ; done < mylist.txt
-C /tmp/1 aaa -C /tmp/2 bbb -C /tmp/3 ccc
# That looks about right. Let's use it in our tar command:
$ tar -cvf foo.tar $(while read -r line; do printf '-C %s %s ' $(dirname "$line") $(basename "$line") ; done < mylist.txt)
aaa
bbb
ccc