Linux文件截断命令(truncate head tail dd)

一、truncate

truncate --help
Usage: truncate OPTION... FILE...
Shrink or extend the size of each FILE to the specified size

A FILE argument that does not exist is created.

If a FILE is larger than the specified size, the extra data is lost.
If a FILE is shorter, it is extended and the extended part (hole)
reads as zero bytes.

Mandatory arguments to long options are mandatory for short options too.
  -c, --no-create        do not create any files
  -o, --io-blocks        treat SIZE as number of IO blocks instead of bytes
  -r, --reference=RFILE  base size on RFILE
  -s, --size=SIZE        set or adjust the file size by SIZE bytes
      --help     display this help and exit
      --version  output version information and exit

SIZE is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

SIZE may also be prefixed by one of the following modifying characters:
'+' extend by, '-' reduce by, '<' at most, '>' at least,
'/' round down to multiple of, '%' round up to multiple of.
概述
  1. 缩小或扩展文件到指定的大小;
  2. (默认)文件不存在会被创建;
  3. 如果文件大于指定的大小,额外的(末尾的)数据将被丢弃;
  4. 如果文件小于指定的大小,则对其进行扩展,并且扩展部分读取为零字节;
实例(可用于删除文件末尾指定大小的内容)
  1. -s 参数:设置或调整文件到指定的大小。

    -s, --size=SIZE        set or adjust the file size by SIZE bytes
    
    $ echo "123456789" > test_truncate
    $ hexdump -C test_truncate
    00000000  31 32 33 34 35 36 37 38  39 0a                    |123456789.|
    # 文件大于指定的大小,额外的(末尾的)数据将被丢弃
    $ truncate -s 5 test_truncate 
    $ hexdump -C test_truncate 
    00000000  31 32 33 34 35                                    |12345|
    # 如果文件小于指定的大小,则对其进行扩展,并且扩展部分读取为零字节
    truncate -s 10 test_truncate
    $ hexdump -C test_truncate 
    00000000  31 32 33 34 35 00 00 00  00 00                    |12345.....|
    
  2. -r 参数:将文件设置为于参考文件相同的大小。

    -r, --reference=RFILE  base size on RFILE
    
    $ ls -l test_truncate*
    -rw-rw-r-- 1 guest guest 10 Nov 30 01:52 test_truncate
    -rw-rw-r-- 1 guest guest 21 Nov 30 02:00 test_truncate_ref
    $ hexdump -C test_truncate                       
    00000000  31 32 33 34 35 00 00 00  00 00                    |12345.....|
    0000000a
    # 将test_truncate文件的大小设置为与test_truncate_ref文件相同
    $ truncate -r test_truncate_ref test_truncate
    $ ls -l test_truncate*                       
    -rw-rw-r-- 1 guest guest 21 Nov 30 02:01 test_truncate
    -rw-rw-r-- 1 guest guest 21 Nov 30 02:00 test_truncate_ref
    $ hexdump -C test_truncate                   
    00000000  31 32 33 34 35 00 00 00  00 00 00 00 00 00 00 00  |12345...........|
    00000010  00 00 00 00 00                                    |.....|
    
  3. -c 参数:文件不存在不创建。

    -c, --no-create        do not create any files
    
    # 带-c参数,不会创建不存在的文件
    ls -l test_truncate
    ls: cannot access test_truncate: No such file or directory
    $ truncate -s 100 -c test_truncate
    $ ls -l test_truncate             
    ls: cannot access test_truncate: No such file or directory
    
    # 不带-c参数,文件不存在则创建,内容都是'0'
    $ truncate -s 100 test_truncate   
    $ ls -l test_truncate          
    -rw-rw-r-- 1 guest guest 100 Nov 30 01:07 test_truncate
    $ hexdump -C test_truncate 
    00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000060  00 00 00 00                                       |....|
    00000064
    

二、head

head --help
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=[-]K         print the first K bytes of each file;
                             with the leading '-', print all but the last
                             K bytes of each file
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading '-', print all but the last
                             K lines of each file
  -q, --quiet, --silent    never print headers giving file names
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
概述

将每个文件的前10行打印到标准输出,如果有多个文件则先打印文件名

$ head test_head test_truncate
==> test_head <==
1234567890

==> test_truncate <==
123451234567890
实例(可用于删除文件末尾指定大小的内容)
  1. -c 参数:打印文件的前K个字节;如果是-K,则去除末尾K字节,打印前面所有字节。

    -c, --bytes=[-]K         print the first K bytes of each file;
                                 with the leading '-', print all but the last
                                 K bytes of each file
    
    # 末尾有个回车符
    $ cat test_head 
    1234567890
    $ head -c 3 test_head 
    123$ 
    $ head -c -3 test_head
    12345678$ 
    
  2. -n 参数:打印文件的前K行;如果是-K,则去除末尾K行,打印前面所有行。

    -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading '-', print all but the last
                             K lines of each file
    
    $ cat test_head 
    1234567890
    234567890
    34567890
    4567890
    567890
    $ head -n 2 test_head 
    1234567890
    234567890
    $ head -n -2 test_head 
    1234567890
    234567890
    34567890
    

三、tail

tail --help
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=K            output the last K bytes; or use -c +K to output
                             bytes starting with the Kth 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=K            output the last K lines, instead of the last 10;
                             or use -n +K to output starting with the Kth
      --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
      --help     display this help and exit
      --version  output version information and exit

If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.
概述:
  1. -n:显示文件的最后 n 行,默认为 10 行。
  2. -f:实时追踪文件的变化并输出新增的内容。
  3. -q:不显示文件名。
  4. -s:设置输出的间隔时间(秒)。
  5. -c:以字节为单位显示指定范围的内容。
实例(可用于删除文件开头指定大小的内容)
  1. -c 参数:打印文件的后K个字节;如果是+K,则从第K个字节开始打印所有后面的内容。

    -c, --bytes=K            output the last K bytes; or use -c +K to output
                                 bytes starting with the Kth of each file
    

    重点

    • 经验证+0和+1都表示从第一个字节开始,和cat功能一样。
    • 可用于删除文件前面的内容,输出到新文件:
      如需删除文件前面的K字节,则参数为+(K+1)
    $ cat test_tail
    1234567890
    $ tail -c 3 test_tail
    90
    $ tail -c +3 test_tail
    34567890
    $ tail -c +0 test_tail
    1234567890
    $ tail -c +1 test_tail
    1234567890
    
  2. -f 参数:实时追踪文件的变化并输出新增的内容。可指定显示几行文件中的内容。

    -f, --follow[={name|descriptor}]
                               output appended data as the file grows;
                                 an absent option argument means 'descriptor'
    
    $ cat test_tail
    1234567890
    234567890
    34567890
    4567890
    567890
    $ tail -2f test_tail 
    4567890
    567890
    
    
  3. -n 参数:显示文件的最后 n 行,默认为 10 行。

    -n, --lines=K            output the last K lines, instead of the last 10;
                                 or use -n +K to output starting with the Kth
    
    $ cat test_tail      
    1234567890
    234567890
    34567890
    4567890
    567890
    $ tail -n 3 test_tail
    34567890
    4567890
    567890
    

四、dd

dd --help
Usage: dd [OPERAND]...
  or:  dd OPTION
Copy a file, converting and formatting according to the operands.

  bs=BYTES        read and write up to BYTES bytes at a time
  cbs=BYTES       convert BYTES bytes at a time
  conv=CONVS      convert the file as per the comma separated symbol list
  count=N         copy only N input blocks
  ibs=BYTES       read up to BYTES bytes at a time (default: 512)
  if=FILE         read from FILE instead of stdin
  iflag=FLAGS     read as per the comma separated symbol list
  obs=BYTES       write BYTES bytes at a time (default: 512)
  of=FILE         write to FILE instead of stdout
  oflag=FLAGS     write as per the comma separated symbol list
  seek=N          skip N obs-sized blocks at start of output
  skip=N          skip N ibs-sized blocks at start of input
  status=LEVEL    The LEVEL of information to print to stderr;
                  'none' suppresses everything but error messages,
                  'noxfer' suppresses the final transfer statistics,
                  'progress' shows periodic transfer statistics

N and BYTES may be followed by the following multiplicative suffixes:
c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M
GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.
概述
  • if=file:输入文件名,缺省为标准输入
  • of=file:输出文件名,缺省为标准输出
  • ibs=bytes:一次读入 bytes 个字节(即一个块大小为 bytes 个字节)
  • obs=bytes:一次写 bytes 个字节(即一个块大小为 bytes 个字节)
  • bs:设置每次读取和写入的块大小(单位为字节或者是可以添加的后缀,如b、k、m等),默认为512字节。
  • count:拷贝的块数,块大小等于 ibs 指定的字节数。若未指定count,则直到输入数据耗尽(如文件遇到EOF),或输出空间满为止。
  • skip=blocks:从输入文件开头跳过 blocks 个块后再开始复制
  • seek=blocks:从输出文件开头跳过 blocks 个块后再开始复制
  • iflag:设置输入选项,常用的选项有direct(绕过缓存直接读取)和sync(同步数据到磁盘)。
  • oflag:设置输出选项,常用的选项有direct(绕过缓存直接写入)和sync(同步数据到磁盘)。
实例
  1. 备份还原

    # 备份
    dd if=/dev/sda of=/tmp/backup.img
    # 还原
    dd if=/tmp/backup.img of=/dev/sda
    
  2. 擦除磁盘数据

    dd if=/dev/zero of=/dev/sda bs=4096
    
  3. 创建虚拟磁盘

    # 写入0
    dd if=/dev/zero of=/tmp/image.img bs=1G count=1
    # 写入随机数
    dd if=/dev/urandom of=/tmp/random.img bs=1M count=1
    
  4. 将ISO镜像写入U盘

    dd if=/path/to/image.iso of=/dev/sdb bs=4M conv=fdatasync
    

    dd测试sync、fsync和fdatasync参数详解

  5. 磁盘复制

    dd if=/dev/sda of=/dev/sdb bs=10M conv=fdatasync
    

dd命令实际应用

五、应用

1. 清空文件内容
echo > 文件名
echo "" > 文件名
cat /dev/null > 文件名
truncate -s 0 文件名
dd if=/dev/null of=文件名

注意:echo并没有将文件大小完全清0,会生成一个换行符(ASCII码0A)

$ echo > test_data             
$ hexdump -C test_data     
00000000  0a                                                |.|
2. 截断文件前后内容
  • tail截断前面5个字节,truncate截断后面5个字节

    $ hexdump -C test_data
    00000000  31 32 33 34 35 36 37 38  39 30 61 62 63 64 65 66  |1234567890abcdef|
    00000010  67 0a                                             |g.|
    $ tail -c +6 test_data > test_data_truncate
    $ hexdump -C test_data_truncate            
    00000000  36 37 38 39 30 61 62 63  64 65 66 67 0a           |67890abcdefg.|
    $ truncate -s 8 test_data_truncate 
    $ hexdump -C test_data_truncate    
    00000000  36 37 38 39 30 61 62 63                           |67890abc|
    
  • tail截断前面5个字节,head截断后面5个字节

    $ hexdump -C test_data                         
    00000000  31 32 33 34 35 36 37 38  39 30 61 62 63 64 65 66  |1234567890abcdef|
    00000010  67 0a                                             |g.|
    00000012
    $ tail -c +6 test_data > test_data_head
    $ hexdump -C test_data_head 
    00000000  36 37 38 39 30 61 62 63  64 65 66 67 0a           |67890abcdefg.|
    0000000d
    $ head -c -5 test_data_head > test_data_head2
    $ hexdump -C test_data_head2 
    00000000  36 37 38 39 30 61 62 63                           |67890abc|
    
  • dd截断前后各5个字节

    $ hexdump -C test_data
    00000000  31 32 33 34 35 36 37 38  39 30 61 62 63 64 65 66  |1234567890abcdef|
    00000010  67 0a                                             |g.|
    $ ls -l test_data
    -rw-rw-r-- 1 guest guest 18 Nov 30 21:24 test_data
    $ dd if=./test_data of=./test_data_dd skip=5 bs=1 count=8
    8+0 records in
    8+0 records out
    8 bytes (8 B) copied, 0.000329923 s, 24.2 kB/s
    $ hexdump -C test_data_dd 
    00000000  36 37 38 39 30 61 62 63                           |67890abc|
    
  • 18
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值