我想查看档案的内容(文件和文件夹列表),例如 tar.gz
文件无需提取它。
有没有什么方法可以做到这一点?
答案1
在终端中运行以下命令即可查看 tar.gz 文件的内容(无需解压):
tar -tf filename.tar.gz
-t, --list
列出档案的内容。参数是可选的。给出时,它们指定要列出的成员的名称。
-f, --file=ARCHIVE 使用存档文件或设备 ARCHIVE...
答案2
你也可以使用 vim
vim filename.tar.gz
答案3
less
还可以打开gz
压缩和未压缩的tar
档案。它还会给你一个可爱的ls -l
输出样式:
$ less ~/src/compiz_0.9.7.8-0ubuntu1.6.debian.tar.gz
drwxrwxr-x 0/0 0 2012-09-21 11:41 debian/
drwxrwxr-x 0/0 0 2012-08-09 13:32 debian/source/
-rw-rw-r-- 0/0 12 2012-08-09 13:32 debian/source/format
-rw-rw-r-- 0/0 25 2012-08-09 13:32 debian/libdecoration0-dev.docs
-rw-rw-r-- 0/0 25 2012-08-09 13:32 debian/compiz-dev.docs
-rw-rw-r-- 0/0 347 2012-08-09 13:32 debian/compiz-core.install
-rw-rw-r-- 0/0 125 2012-08-09 13:32 debian/libdecoration0-dev.install
...
因为它是less
,所以您可以滚动浏览它、搜索它等等。但是,它在其他压缩算法中却惨遭失败(根据我的经验)。
答案4
嗯,这取决于文件。大多数(解)压缩程序都有一个列出档案内容的标志。
tar
/tar.gz
/tgz
/tar.xz
/tar.bz2
/tbz
文件$ tar tf foo.tgz dir1/ dir1/subdir1/ dir1/subdir1/file dir1/subdir2/ dir1/subdir2/file dir2/
zip
文件$ zip -sf foo.zip Archive contains: dir1/ dir2/ dir1/subdir1/ dir1/subdir1/file dir1/subdir2/ dir1/subdir2/file Total 6 entries (0 bytes)
7zip
文件$ 7z l foo.7z 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.utf8,Utf16=on,HugeFiles=on,4 CPUs) Listing archive: foo.7z -- Path = foo.7z Type = 7z Solid = - Blocks = 0 Physical Size = 168 Headers Size = 168 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2015-03-30 19:00:07 ....A 0 0 dir1/subdir1/file 2015-03-30 19:00:07 ....A 0 0 dir1/subdir2/file 2015-03-30 19:07:32 D.... 0 0 dir2 2015-03-30 19:00:07 D.... 0 0 dir1/subdir2 2015-03-30 19:00:07 D.... 0 0 dir1/subdir1 2015-03-30 19:00:06 D.... 0 0 dir1 ------------------- ----- ------------ ------------ ------------------------ 0 0 2 files, 4 folders
rar
文件$ rar v foo.rar RAR 4.20 Copyright (c) 1993-2012 Alexander Roshal 9 Jun 2012 Trial version Type RAR -? for help Archive foo.rar Pathname/Comment Size Packed Ratio Date Time Attr CRC Meth Ver ------------------------------------------------------------------------------- dir1/subdir1/file 0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9 dir1/subdir2/file 0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9 dir1/subdir1 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir1/subdir2 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir1 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir2 0 0 0% 30-03-15 19:07 drwxr-xr-x 00000000 m0 2.0 ------------------------------------------------------------------------------- 6 0 16 0%
这就是大多数比较流行的存档格式。考虑到所有这些,您可以编写一个小脚本,根据您提供给它的文件扩展名使用适当的命令:
#!/usr/bin/env bash
for file in "$@"
do
printf "\n-----\nArchive '%s'\n-----\n" "$file"
## Get the file's extension
ext=${file##*.}
## Special case for compressed tar files. They sometimes
## have extensions like tar.bz2 or tar.gz etc.
[[ "$(basename "$file" ."$ext")" =~ \.tar$ ]] && ext="tgz"
case $ext in
7z)
type 7z >/dev/null 2>&1 && 7z l "$file" ||
echo "ERROR: no 7z program installed"
;;
tar|tbz|tgz)
type tar >/dev/null 2>&1 && tar tf "$file"||
echo "ERROR: no tar program installed"
;;
rar)
type rar >/dev/null 2>&1 && rar v "$file"||
echo "ERROR: no rar program installed"
;;
zip)
type zip >/dev/null 2>&1 && zip -sf "$file"||
echo "ERROR: no zip program installed"
;;
*)
echo "Unknown extension: '$ext', skipping..."
;;
esac
done
将该脚本保存在您的文件中PATH
并使其可执行。然后您可以列出任何档案的内容:
$ list_archive.sh foo.rar foo.tar.bz foo.tar.gz foo.tbz foo.zip
-----
Archive 'foo.rar'
-----
RAR 4.20 Copyright (c) 1993-2012 Alexander Roshal 9 Jun 2012
Trial version Type RAR -? for help
Archive foo.rar
Pathname/Comment
Size Packed Ratio Date Time Attr CRC Meth Ver
-------------------------------------------------------------------------------
dir1/subdir1/file
0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9
dir1/file
0 8 0% 30-03-15 19:29 -rw-r--r-- 00000000 m3b 2.9
dir1/subdir1
0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0
dir1
0 0 0% 30-03-15 19:29 drwxr-xr-x 00000000 m0 2.0
dir2
0 0 0% 30-03-15 19:07 drwxr-xr-x 00000000 m0 2.0
-------------------------------------------------------------------------------
5 0 16 0%
-----
Archive 'foo.tar.bz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/
-----
Archive 'foo.tar.gz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/
-----
Archive 'foo.tbz'
-----
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/
-----
Archive 'foo.zip'
-----
Archive contains:
dir1/
dir1/subdir1/
dir1/subdir1/file
dir1/file
dir2/
Total 5 entries (0 bytes)
既然有人提到了那个较小的编辑器,那么自然,emacs 也可以做到这一点: