Se você tem o GNU tar, ele tem uma --to-command
opção:
--to-command=COMMAND
Pipe extracted files to COMMAND. The argument is the pathname
of an external program, optionally with command line
arguments. The program will be invoked and the contents of
the file being extracted supplied to it on its standard
output. Additional data will be supplied via the following
environment variables:
TAR_FILETYPE
Type of the file. It is a single letter with the
following meaning:
f Regular file
d Directory
l Symbolic link
h Hard link
b Block device
c Character device
Currently only regular files are supported.
...
TAR_FILENAME
The name of the file.
Essas variáveis podem ser usadas para manipular com segurança nomes de arquivos com espaços, etc.
Por exemplo, usando a substituição de string shell para remover o nome do arquivo do caminho fornecido e, em seguida, usando sed para imprimir somente os caminhos para não diretórios, você pode classificar e aplicar uniq -c
para obter a contagem:
tar xf foo.tar --to-command 'echo "$TAR_FILETYPE" "${TAR_FILENAME%/*}"' |
sed -n '/^[^d]/s/^. //p' |
sort |
uniq -c
Se você tiver o GNU sed, sort e uniq, você pode usar as opções -z
e printf "%s %secho
"
em vez de %code% para manipular com segurança todos os nomes de arquivos.
Exemplo:
% tar xf dev/pacaur/byobu/byobu_5.124.orig.tar.gz --to-command 'printf "%s %s--to-command=COMMAND
Pipe extracted files to COMMAND. The argument is the pathname
of an external program, optionally with command line
arguments. The program will be invoked and the contents of
the file being extracted supplied to it on its standard
output. Additional data will be supplied via the following
environment variables:
TAR_FILETYPE
Type of the file. It is a single letter with the
following meaning:
f Regular file
d Directory
l Symbolic link
h Hard link
b Block device
c Character device
Currently only regular files are supported.
...
TAR_FILENAME
The name of the file.
" "$TAR_FILETYPE" "${TAR_FILENAME%/*}"' | sed -zn '/^[^d]/s/^. //p' | sort -z | uniq -zc | tr 'tar xf foo.tar --to-command 'echo "$TAR_FILETYPE" "${TAR_FILENAME%/*}"' |
sed -n '/^[^d]/s/^. //p' |
sort |
uniq -c
' '\n'
15 byobu-5.124
2 byobu-5.124/Applications/Byobu.app/Contents
1 byobu-5.124/Applications/Byobu.app/Contents/MacOS
8 byobu-5.124/Applications/Byobu.app/Contents/Resources
4 byobu-5.124/etc/byobu
3 byobu-5.124/etc/profile.d
1 byobu-5.124/experimental
23 byobu-5.124/po
1 byobu-5.124/snap
38 byobu-5.124/usr/bin
43 byobu-5.124/usr/lib/byobu
18 byobu-5.124/usr/lib/byobu/include
1 byobu-5.124/usr/share/appdata
4 byobu-5.124/usr/share/byobu/desktop
12 byobu-5.124/usr/share/byobu/keybindings
4 byobu-5.124/usr/share/byobu/pixmaps
1 byobu-5.124/usr/share/byobu/pixmaps/highcontrast
11 byobu-5.124/usr/share/byobu/profiles
4 byobu-5.124/usr/share/byobu/status
3 byobu-5.124/usr/share/byobu/tests
3 byobu-5.124/usr/share/byobu/windows
3 byobu-5.124/usr/share/dbus-1/services
4 byobu-5.124/usr/share/doc/byobu
37 byobu-5.124/usr/share/man/man1
1 byobu-5.124/usr/share/sounds/byobu