Linux系统命令总结--查看文件内容

Linux系统命令总结–查看文件内容

查看文件类型

  1. file命令

    file命令file --help

    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
    40
    41
    42
    43
    44
    Usage: file [OPTION...] [FILE...]
    Determine type of FILEs.

    --help display this help and exit
    -v, --version output version information and exit
    -m, --magic-file LIST use LIST as a colon-separated list of magic
    number files
    -z, --uncompress try to look inside compressed files
    -Z, --uncompress-noreport only print the contents of compressed files
    -b, --brief do not prepend filenames to output lines
    -c, --checking-printout print the parsed form of the magic file, use in
    conjunction with -m to debug a new magic file
    before installing it
    -e, --exclude TEST exclude TEST from the list of test to be
    performed for file. Valid tests are:
    apptype, ascii, cdf, compress, elf, encoding,
    soft, tar, text, tokens
    -f, --files-from FILE read the filenames to be examined from FILE
    -F, --separator STRING use string as separator instead of `:'
    -i, --mime output MIME type strings (--mime-type and
    --mime-encoding)
    --apple output the Apple CREATOR/TYPE
    --extension output a slash-separated list of extensions
    --mime-type output the MIME type
    --mime-encoding output the MIME encoding
    -k, --keep-going don't stop at the first match
    -l, --list list magic strength
    -L, --dereference follow symlinks (default if POSIXLY_CORRECT is set)
    -h, --no-dereference don't follow symlinks (default if POSIXLY_CORRECT is not set) (default)
    -n, --no-buffer do not buffer output
    -N, --no-pad do not pad output
    -0, --print0 terminate filenames with ASCII NUL
    -p, --preserve-date preserve access times on files
    -P, --parameter set file engine parameter limits
    indir 15 recursion limit for indirection
    name 30 use limit for name/use magic
    elf_notes 256 max ELF notes processed
    elf_phnum 128 max ELF prog sections processed
    elf_shnum 32768 max ELF sections processed
    -r, --raw don't translate unprintable chars to \ooo
    -s, --special-files treat special (block/char devices) files as
    ordinary ones
    -C, --compile compile file specified by -m
    -d, --debug print debugging messages

    file命令主要用来查看文件的类型。

    | 参数 | 作用 |
    | —- | ———————————————————— |
    | -b | 结果不输出文件名 |
    | -z | 尝试解压压缩文件 |
    | -f | 指定名称文件,其中内容有一个或者多个文件名称时,依序辨识这些文件 |
    | -L | 直接显示符号链接指向的文件 |
    | -v | 显示版本信息 |
    | | |
    | | |

查看文件内容

  1. cat命令

    cat --help 显示cat命令帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit

cat相关命令

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
#查看文件
$ cat test.sh
echo date;

#-n参数给所有行加上行号
$ cat -n test.sh
1 echo date;

#只给有文本的行加上行号,可以用-b参数
$ cat -b test.sh
1 echo date;

2 echo time;

#如果不想出现制表符,可以用-T参数,-T参数会用^I字符组合去替换文中的所有制表符
$ cat -T test.sh
echo date;

echo time;

#使用-s参数来去除多余的空行,原来文件之间有多个空行
$ cat -s test.sh
echo date;

echo time;

#使用-e参数来使得每一行的末尾都以$结尾
$ cat -e test.sh
echo date;$
$
$
$
$
$
$
$
echo time;$
  1. more命令

    cat的缺陷在于一旦运行就无法控制后面操作,为了解决这个问题,开发人员编写了more命令。more命令会显示文本文件的内容,但是会在显示每页数据之后停下来。

    more --help

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    Usage:
    more [options] <file>...

    A file perusal filter for CRT viewing.

    Options:
    -d display help instead of ringing bell
    -f count logical rather than screen lines
    -l suppress pause after form feed
    -c do not scroll, display text and clean line ends
    -p do not scroll, clean screen and display text
    -s squeeze multiple blank lines into one
    -u suppress underlining
    -<number> the number of lines per screenful
    +<number> display file beginning from line number
    +/<string> display file beginning from search string match

    more命令的相关参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # more命令的相关参数,显示相关帮助信息
    more -d test.sh
    ...
    222
    222
    --More--(16%)[Press space to continue, 'q' to quit.]

    # more命令-f参数
    ...
  2. less命令

    less 命令实际上是more命令的升级版(名称由俗语less is more得来),

查看部分文件

  1. tail命令

    tail命令tail --help

    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
    Usage: tail [OPTION]... [FILE]...
    Print the last 10 lines of each FILE to standard output.
    With more than one FILE, precede each with a header giving the file name.

    With no FILE, or when FILE is -, read standard input.

    Mandatory arguments to long options are mandatory for short options too.
    -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to
    output starting with byte NUM of each file
    -f, --follow[={name|descriptor}]
    output appended data as the file grows;
    an absent option argument means 'descriptor'
    -F same as --follow=name --retry
    -n, --lines=[+]NUM output the last NUM lines, instead of the last 10;
    or use -n +NUM to output starting with line NUM
    --max-unchanged-stats=N
    with --follow=name, reopen a FILE which has not
    changed size after N (default 5) iterations
    to see if it has been unlinked or renamed
    (this is the usual case of rotated log files);
    with inotify, this option is rarely useful
    --pid=PID with -f, terminate after process ID, PID dies
    -q, --quiet, --silent never output headers giving file names
    --retry keep trying to open a file if it is inaccessible
    -s, --sleep-interval=N with -f, sleep for approximately N seconds
    (default 1.0) between iterations;
    with inotify and --pid=P, check process P at
    least once every N seconds
    -v, --verbose always output headers giving file names
    -z, --zero-terminated line delimiter is NUL, not newline
    --help display this help and exit
    --version output version information and exit

    tail命令:显示文件最后几行的内容。默认显示文件末尾10行。

    比如我们创建了一个包含20行文本的文本文件。使用cat命令显示该文件的全部内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    $ cat log_file
    line1
    line2
    line3
    line4
    line5
    Hello World - line 6
    line7
    line8
    line9
    line10
    line11
    Hello again - line 12
    line13
    line14
    line15
    Sweet - line16
    line17
    line18
    line19
    Last line - line20
    $

    现在显示的是整个文件,使用tail命令浏览文件最后10行的效果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ tail log_file
    line11
    Hello again - line 12
    line13
    line14
    line15
    Sweet - line16
    line17
    line18
    line19
    Last line - line20
    $

    可以向tail命令加入-n参数来修改所显示的行数。下面的例子中,通过加入-n 2使tail命令只显示文件的最后两行。

    1
    2
    3
    4
    $ tail -n 2 log_file
    line19
    Last line - line20
    $

    还可以在通过tail命令 加号显示某行之后的行数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ tail -n +10 log_file
    line10
    line11
    Hello again - line 12
    line13
    line14
    line15
    Sweet - line16
    line17
    line18
    line19
    Last line - line20

    -f参数是tail命令的一个突出特性,允许你在其他进程中使用该文件时查看文件的内容。tail会保持活动状态,并且不断的显示添加文件中的内容。

  2. head命令

    首先通过head --help来查看head命令的使用方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Usage: head [OPTION]... [FILE]...
    Print the first 10 lines of each FILE to standard output.
    With more than one FILE, precede each with a header giving the file name.

    With no FILE, or when FILE is -, read standard input.

    Mandatory arguments to long options are mandatory for short options too.
    -c, --bytes=[-]NUM print the first NUM bytes of each file;
    with the leading '-', print all but the last
    NUM bytes of each file
    -n, --lines=[-]NUM print the first NUM lines instead of the first 10;
    with the leading '-', print all but the last
    NUM lines of each file
    -q, --quiet, --silent never print headers giving file names
    -v, --verbose always print headers giving file names
    -z, --zero-terminated line delimiter is NUL, not newline
    --help display this help and exit
    --version output version information and exit

    head命令查看文件的前10行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ head log_file
    line1
    line2
    line3
    line4
    line5
    Hello World - line 6
    line7
    line8
    line9
    line10
    $

    也可以使用参数-n 来指定显示的行数,比如head -n 5 log_file显示log_file的前5行

    1
    2
    3
    4
    5
    6
    $ head -n 5 log_file
    line1
    line2
    line3
    line4
    line5

    也可以通过添加减号,指定不显示最后几行,比如head -n -5 log_file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ head -n -5 log_file
    line1
    line2
    line3
    line4
    line5
    Hello World - line 6
    line7
    line8
    line9
    line10
    line11
    Hello again - line 12
    line13
    line14
    line15

    一般文件头不会发生变化,所以没有-f参数

  3. head和tail命令的组合使用

    比如你想显示某个文件的第10行,我们可以组合使用head和tail命令

    1
    2
    $ tail -n +10 log_file |head -n 1
    line10

    再比如从第11行显示,但是不包括最后三行

    1
    2
    3
    4
    5
    6
    7
    8
    $ head -n -3 log_file | tail -n +11
    line11
    Hello again - line 12
    line13
    line14
    line15
    Sweet - line16
    line17

    再比如显示前20行,但是从第11行开始

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ head -n 20 log_file | tail -n +11
    line11
    Hello again - line 12
    line13
    line14
    line15
    Sweet - line16
    line17
    line18
    line19
    Last line - line20

    可以看出,组合head和tail命令可以产生更加强大的功能。

参考文献

《Linux命令行和shell脚本编程大全》