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

目录

      • 一、truncate
        • 功能概述
        • 实例(可用于删除文件末尾指定大小的内容)
      • 二、head
        • 功能概述
        • 实例(可用于删除文件末尾指定大小的内容)
      • 三、tail
        • 功能概述:
        • 实例(可用于删除文件开头指定大小的内容)
      • 四、dd
        • 概述
      • 五、应用
        • 1. 清空文件内容

一、truncate

truncate --help
Usage: truncate OPTION... FILE...
Shrink or extend the size of each FILE to the specified sizeA 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 exitSIZE 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 lastK bytes of each file-n, --lines=[-]K         print the first K lines instead of the first 10;with the leading '-', print all but the lastK 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 exitK 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 lastK 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 lastK 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 outputbytes 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=Nwith --follow=name, reopen a FILE which has notchanged size after N (default 5) iterationsto 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 atleast once every N seconds-v, --verbose            always output headers giving file names--help     display this help and exit--version  output version information and exitIf 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 outputbytes 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 timecbs=BYTES       convert BYTES bytes at a timeconv=CONVS      convert the file as per the comma separated symbol listcount=N         copy only N input blocksibs=BYTES       read up to BYTES bytes at a time (default: 512)if=FILE         read from FILE instead of stdiniflag=FLAGS     read as per the comma separated symbol listobs=BYTES       write BYTES bytes at a time (default: 512)of=FILE         write to FILE instead of stdoutoflag=FLAGS     write as per the comma separated symbol listseek=N          skip N obs-sized blocks at start of outputskip=N          skip N ibs-sized blocks at start of inputstatus=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 statisticsN 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 表示输入文件,of 表示输出文件,options 是一些可选参数。下面是一些常用的参数:

  • bs:设置每次读取和写入的块大小(单位为字节或者是可以添加的后缀,如b、k、m等),默认为512字节。
  • count:设置要复制的块数。
  • iflag:设置输入选项,常用的选项有direct(绕过缓存直接读取)和sync(同步数据到磁盘)。
  • oflag:设置输出选项,常用的选项有direct(绕过缓存直接写入)和sync(同步数据到磁盘)。

五、应用

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/185451.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Golang语言基础之切片

概述 数组的长度是固定的并且数组长度属于类型的一部分&#xff0c;所以数组有很多的局限性 func arraySum(x [3]int) int{sum : 0for _, v : range x{sum sum v}return sum } 这个求和函数只能接受 [3]int 类型&#xff0c;其他的都不支持。 切片 切片&#xff08;Slic…

virustotal的使用

www.virustotal.com是一个恶意代码扫描网站&#xff0c;提交时需要验证码。 该网站有近百个病毒引擎的支持。 该网站最有用的地方在于&#xff0c;这是一个交互式的恶意代码检测网站&#xff0c;这样的模式有一个隐形的福利&#xff0c;那就是为病毒木马爱好者提供了攻防一体…

市面上这么多SD-WAN服务商,究竟有何不同?

随着数字化浪潮的不断发展&#xff0c;企业网络已经成为了现代企业中不可缺少的一部分。而提供企业组网服务的SD-WAN服务商也呈现出快速增长的趋势。但是&#xff0c;市场上有这么多SD-WAN服务商&#xff0c;各个服务商技术实现方案非常相似&#xff0c;那么这些服务商之间到底…

人工智能驱动的医疗辅助:陪诊系统的技术原理与应用

随着人工智能技术的不断发展&#xff0c;医疗领域也迎来了新的可能性。本文将深入探讨陪诊系统的技术原理及其在医疗领域中的应用。我们将重点关注人工智能的核心概念&#xff0c;如自然语言处理、机器学习和语音识别&#xff0c;以解释陪诊系统是如何在医疗环境中发挥作用的。…

配置spring boot3后redis NOAUTH Authentication required

升级到spring boot3之后&#xff0c;redis报错 redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required检查完密码之后都没有问题&#xff0c;后来发现是配置的原因。 在application.properties配置文件里 加上.data 原来是spring.redis.passwor…

html5各行各业官网模板源码下载(1)

文章目录 1.来源2.源码模板2.1 HTML5白色简洁设计师网站模板 作者&#xff1a;xcLeigh 文章地址&#xff1a;https://blog.csdn.net/weixin_43151418/article/details/134682321 html5各行各业官网模板源码下载&#xff0c;这个主题覆盖各行业的html官网模板&#xff0c;效果模…

图解Redis适用场景

Redis以其速度而闻名。 1 业务数据缓存 1.1 通用数据缓存 string&#xff0c;int&#xff0c;list&#xff0c;map。Redis 最常见的用例是缓存对象以加速 Web 应用程序。 此用例中&#xff0c;Redis 将频繁请求的数据存储在内存。允许 Web 服务器快速返回频繁访问的数据。这…

Make sure bypassing Vue built-in sanitization is safe here.

一、问题描述 二、问题分析 XSS(跨站脚本攻击) XSS攻击通常指的是通过利用网页开发时留下的漏洞&#xff0c;通过巧妙的方法注入恶意指令代码到网页&#xff0c;使用户加载并执行攻击者恶意制造的网页程序。这些恶意网页程序通常是JavaScript&#xff0c;但实际上也可以包括J…

【注册表】Sublime Text添加到右键菜单

官网下载 windows下地地址: http://www.sublimetext.com/download_thanks?targetwin-x64设置右键菜单和菜单小图标 win R打开运行&#xff0c;并输入regedit打开注册表编辑器依次找到HKEY_CLASSESS_ROOT -> * -> Shell&#xff0c;下面新建项&#xff0c; 这个项的名…

PAT乙级(CPP基础STL)

万能头&#xff0c;库 #include<bits/stdc.h> string数组 //string的初始化 string s"abc"; string(6,A); //string取子串&#xff08;起始位置&#xff0c;长度&#xff09; string s"Hello World!"; cout << s.substr(6) << endl…

【新手解答7】深入探索 C 语言:代码缩进 + 变量作用域、静态变量 + 变量名和函数名重名

C语言的相关问题解答 写在最前面问题一&#xff1a;代码缩进问题二&#xff1a;C语言中的变量作用域变量作用域静态变量总结 问题三&#xff1a;变量名和函数名重名相关解析变量 sumC 语言中&#xff0c;sum 并不是一个内置的函数名或保留字变量名和函数名重名&#xff1f;总结…

Oracle中mybatis批量更新报错ORA-00933:SQL命令未正确结束

项目场景&#xff1a; 最近在开发项目的过程中遇见了这个问题&#xff1a;Oracle中批量更新的时候报错 ORA-00933&#xff1a;SQL命令未正确结束 问题描述 mybatis批量更新报错ORA-00933&#xff1a;SQL命令未正确结束 <foreach item"item" index"index&q…

【TinyALSA全解析(三)】tinyplay、tincap、pcm_open源码解析

tinyplay、tincap、pcm_open源码解析 一、本文的目的二、tinyplay.c源码分析三、tinycap.c源码分析四、pcm.c如何调度到Linux Kernel4.1 pcm_open解析4.1.1 pcm_open的主要流程4.1.2 流程说明4.1.3 调用方法 4.2 pcm_write解析 /*********************************************…

图解系列--HTTPS,认证

确保 Web 安全的HTTPS 1.HTTP 的缺点 1.1.通信使用明文可能会被窃听 加密处理防止被窃听 加密的对象可以有这么几个。 (1).通信的加密 HTTP 协议中没有加密机制&#xff0c;但可以通过和 SSL&#xff08;Secure Socket Layer&#xff0c;安全套接层&#xff09;或TLS&#xff…

android 特殊权限处理

运行时权限之特殊权限android.permission.SYSTEM_ALERT_WINDOW 以下为特殊权限的一种申请写法(android.permission.SYSTEM_ALERT_WINDOW) 在做双屏异显功能时,需要使用到Presentation, 使用Presentation需要android.permission.SYSTEM_ALERT_WINDOW权限, 于是就使用谷歌的权限框…

猜-MISC-bugku-解题步骤

——CTF解题专栏—— 题目信息&#xff1a; 题目&#xff1a;猜 作者&#xff1a;harry 提示&#xff1a; 解题附件&#xff1a;flag格式key{图中人物名字全拼} 解题思路&#xff1a; 这......头都没有&#xff0c;让我guess&#xff1f;&#xff1f;&#xff1f;详细信息看…

NASM安装和结合nodepad++进行编译的过程

mov ax,0x30 mov bx,0xc0 add ax,bx times 502 db 0 db 0x55 db 0xAA nasm安装地址: https://www.nasm.us/ 下载exe安装 在命令行提示符输入nasm编译命令 nasm exam.asm -f bin -o exam.bin 此时输入回车将会执行编译过程。 1&#xff0c;启动NotePad&#xff0c;在菜单上选…

web前端tips:js继承——寄生组合式继承

上篇文章给大家分享了 js继承中的 寄生式继承 web前端tips&#xff1a;js继承——寄生式继承 今天给大家分享一下 js 继承中的 寄生组合式继承 寄生组合式继承 寄生组合式继承是一种结合了寄生式继承和组合式继承的方式&#xff0c;它的目标是减少组合式继承中多余的调用父…

【Java 并发编程】进程线程、lock、设计模式、线程池...

博主&#xff1a;_LJaXi Or 東方幻想郷 专栏&#xff1a; Java | 从入门到入坟 Java 并发编程 并发编程多线程的入门类和接口线程组和线程优先级线程的状态及主要转化方法线程间的通信重排序和 happens-beforevolatilesynchronized 与锁CAS 与原子操作AQS计划任务Stream 并行计…

开放式耳机哪个品牌好用?超好用的耳机推荐,新手小白必看

在当今数不胜数的音频品牌中&#xff0c;寻找一款优秀的开放式耳机成为了许多音乐爱好者和新手小白的共同关注点&#xff0c;开放式耳机以其通透的音质和舒适的佩戴感受受到了广泛好评&#xff0c;但市场上的选择却让人眼花缭乱&#xff0c; 为了帮助新手小白在这个耳机的海洋…