Вы можете использовать zcat
.
zcat | head -n
Распаковывается только для потоковой передачи этих n
строк.
Дополнительная литература:http://www.thegeekstuff.com/2009/05/zcat-zless-zgrep-zdiff-zcmp-zmore-gzip-file-operations-on-the-compressed-files/
Если ваш учитель хочет, чтобы вы воспроизвели результат, который вы показали в вопросе, они, вероятно, хотят, чтобы вы сделали что-то вроде этого:
#!/bin/sh
for f
do
if [ -d "$f" ]
then
printf '%s (%s)\n' "$f" Directory
elif [ -f "$f" ]
then
printf '%s (%s)\n' "$f" 'Ordinary file'
else
printf '%s (%s)\n' "$f" Other
fi
done
который является чистым bash.
Если вы предпочитаете использовать внешнюю программу, вы можете:
#!/bin/sh
for f
do
case "$(ls -ld "$f")" in
d*)
printf '%s (%s)\n' "$f" Directory
;;
-*)
printf '%s (%s)\n' "$f" 'Ordinary file'
;;
*)
printf '%s (%s)\n' "$f" Other
esac
done
Вот минималистичный пример, если вы просто хотите отличить файлы от каталогов, но вас не интересует тип файла.
find. -mindepth 1 -type d -printf '%f (Directory)\n' && find. -type f -printf '%f (Ordinary file)\n'
Используя команду find
, сначала перечислите каталоги, а затем файлы во второй команде. Назначение -mindepth 1
в первой команде — пропустить .
, указывающее текущий каталог.
Так как find является рекурсивным , вы, вероятно, захотите добавить опцию -maxdepth 1
.
Если у вас возник такой вопрос, первое, что вы должны сделать, это прочитать file
справочную страницу(man 1 file
). Как сказано в его документации:
The filesystem tests are based on examining the return from a stat(2) system
call. The program checks to see if the file is empty, or if it's some sort of
special file. Any known file types appropriate to the system you are running
on (sockets, symbolic links, or named pipes (FIFOs) on those systems that
implement them) are intuited if they are defined in the system header file
<sys/stat.h>.
The magic tests are used to check for files with data in particular fixed for‐
mats. The canonical example of this is a binary executable (compiled program)
a.out file, whose format is defined in <elf.h>, <a.out.h> and possibly
<exec.h> in the standard include directory. These files have a “magic number”
stored in a particular place near the beginning of the file that tells the
UNIX operating system that the file is a binary executable, and which of sev‐
eral types thereof. The concept of a “magic” has been applied by extension to
data files. Any file with some invariant identifier at a small fixed offset
into the file can usually be described in this way. The information identify‐
ing these files is read from /etc/magic and the compiled magic file
/usr/share/misc/magic.mgc, or the files in the directory /usr/share/misc/magic
if the compiled file does not exist. In addition, if $HOME/.magic.mgc or
$HOME/.magic exists, it will be used in preference to the system magic files.
file
опирается на магическое число. Поэтому, если вы хотите реализовать эквивалент файла самостоятельно, вы должны сначала использовать stat
, чтобы проверить, является ли это обычным файлом и не является ли он пустым, а затем использовать магическое число. Подумайте об использовании libmagic
, который сделает это за вас.