当前位置: 首页 > news >正文

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=文件名

相关文章:

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

目录 一、truncate功能概述实例&#xff08;可用于删除文件末尾指定大小的内容&#xff09; 二、head功能概述实例&#xff08;可用于删除文件末尾指定大小的内容&#xff09; 三、tail功能概述&#xff1a;实例&#xff08;可用于删除文件开头指定大小的内容&#xff09; 四、…...

Armbian安装python环境和pip国内源

文章目录 安装python环境配置pip为国内源 安装python环境 更新软件包列表&#xff1a; sudo apt update安装 Python 3&#xff08;通常是最新版本&#xff09;&#xff1a; sudo apt install python3验证 Python 安装是否成功&#xff1a; python3 --version运行上述命令后&…...

宿主Linux——KVM安装Windows7系统

KVM虚拟技术 KVM(Kernel-based Virtual Machine) 是基于Linux内核的开源虚拟化技术&#xff0c;在一台物理机上可同时运行多个虚拟系统。KVM使用硬件虚拟化扩展&#xff0c;例如Intel的VT和AMD的AMD-V&#xff0c;在性能方面更加高效&#xff0c;可提供更好的计算能力和响应速…...

Mysql更新Blob存储的Josn数据

Mysql更新blob存储的Josn数据 记录一次mysql操作blob格式存储的json字符串数据 1、检查版本 -- 版本5.7以上才可以能执行json操作 select version(); 2、创建测试数据 -- 创建测试表及测试数据 CREATE TABLE test_json_table AS SELECT UUID(), {"test1": {"…...

C语言——指针(三)

&#x1f4dd;前言&#xff1a; 上篇文章C语言——指针&#xff08;二&#xff09;中对&#xff1a;指针的运算和指针变量类型对指针使用的影响开展了进一步的探讨&#xff0c;这篇文章我们继续学习一下指针与一维数组之间的关系&#xff1a; 1&#xff0c;对数组名的理解 2&am…...

VIR-SLAM代码分析3——VIR_VINS详解之estimator.cpp/.h

前言 续接上一篇&#xff0c;本本篇接着介绍VIR-SLAM中estimator.cpp/.h文件的函数&#xff0c;尤其是和UWB相关的相比于VINS改动过的函数&#xff0c;仍然以具体功能情况代码注释的形式进行介绍。 重点函数介绍 优化函数&#xff0c;代码是先优化&#xff0c;后边缘化。 …...

大模型的RPA应用 | 代理流程自动化(APA),开启智能自动化新纪元

随着技术创新的持续推进&#xff0c;自动化技术已经变得至关重要&#xff0c;成为驱动企业和社会向前发展的核心动力。在自动化的里程碑中&#xff0c;机器人流程自动化&#xff08;RPA&#xff09;已经有效地将简单、重复且规则性的任务自动化。可是随着对处理更为复杂、多变且…...

爬虫学习 异步爬虫(五)

多线程 多进程 协程 进程 运行中的程序 线程 被CPU调度的执行过程,操作系统 运算调度的min单位 在进程之中,进程中实际运作单位 from threading import Thread#创建任务 def func(name):for i in range(100):print(name,i)if __name__ __main__:#创建线程t1 Thread(target …...

【Openstack Train安装】六、Keystone安装

OpenStack是一个云计算平台的项目&#xff0c;其中Keystone是一个身份认证服务组件&#xff0c;它提供了认证、授权和目录的服务。其他OpenStack服务组件都需要使用Keystone来验证用户的身份和权限&#xff0c;并且彼此之间需要相互协作。当一个OpenStack服务组件接收到用户的请…...

java学习part22包装类

119-面向对象(高级)-包装类的理解_基本数据类型与包装类间的转换_哔哩哔哩_bilibili 1.包装类 2.基本转包装方式 2.1new方式 源码 2.2valueof&#xff08;&#xff09; 3.包装转基本 4.基本类型和包装类型的默认值不一样 比如boolean默认false Boolean默认null&#xff08;对…...

【场景测试用例】二维码

测试思路&#xff1a; UI 不同设备&#xff0c;不同浏览器下的外观和布局一致用户友好性 二维码足够清晰且大小合适是否有错误提示是否有扫描成功/失败提示启动&#xff0c;扫描过程 功能 验证识别功能 二维码完整且有效二维码失效二维码不完整/过于模糊空白二维码测试不同大小…...

如何提高销售技巧,增加客户的成交率?

如何提高销售技巧&#xff0c;增加客户的成交率&#xff1f; 在如今的市场环境中&#xff0c;销售技巧的高低往往决定了你是否能够成功地打动客户的心。想要提高销售业绩&#xff0c;除了产品质量和服务的保障&#xff0c;更需要你精进销售技巧&#xff0c;从而让客户愿意为你…...

软件设计之生成器模式

理解生成器模式在于&#xff1a;一个对象若由多个部分组成&#xff0c;只要构建好这些部分然后拼接到一起就组成了一个完整的对象。比如一台电脑&#xff0c;它的类型可以不一样&#xff0c;可以是苹果的&#xff0c;可以是联想的&#xff0c;等等。同一款电脑它的组件也不一样…...

【Vulnhub 靶场】【CEREAL: 1】【困难】【20210529】

1、环境介绍 靶场介绍&#xff1a;https://www.vulnhub.com/entry/cereal-1,703/ 靶场下载&#xff1a;https://download.vulnhub.com/cereal/Cereal.ova 靶场难度&#xff1a;困难 发布日期&#xff1a;2021年5月29日 文件大小&#xff1a;1.1 GB 靶场作者&#xff1a;Thomas…...

【Vulnhub靶机】Jarbas--Jenkins

文章目录 信息收集主机发现端口扫描目录爆破 漏洞探测whatwebhash-identifierwhatweb 文档说明&#xff1a;https://www.vulnhub.com/entry/jarbas-1,232/ 靶机下载&#xff1a;Download (Mirror): 信息收集 主机发现 扫描C段 sudo nmap -sn 10.9.75.0/24端口扫描 sudo nma…...

Java面向对象第8天

精华笔记&#xff1a; 接口&#xff1a; 是一种引用数据类型 由interface定义 只能包含常量和抽象方法 不能被实例化 接口是需要被实现/继承的&#xff0c;实现类/派生类&#xff1a;必须重写接口中的所有抽象方法 一个类可以实现多个接口&#xff0c;用逗号分隔。若又继承…...

数据结构与算法复习笔记

1.数据结构基本概念 数据结构: 它是研究计算机数据间关系&#xff0c;包括数据的逻辑结构和存储结构及其操作。 数据&#xff08;Data&#xff09;&#xff1a;数据即信息的载体&#xff0c;是能够输入到计算机中并且能被计算机识别、存储和处理的符号总称。 数据元素&#xf…...

关于微服务的思考

目录 什么是微服务 定义 特点 利弊 引入时机 需要哪些治理环节 从单体架构到微服务架构的演进 单体架构 集群和垂直化 SOA 微服务架构 如何实现微服务架构 服务拆分 主流微服务解决方案 基础设施 下一代微服务架构Service Mesh 什么是Service Mesh&#xff1f…...

计算机毕业设计 基于Web的课程设计选题管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…...

群晖NAS:docker(Container Manager)、npm安装Verdaccio并常见命令集合

群晖NAS&#xff1a;docker&#xff08;Container Manager&#xff09;、npm安装Verdaccio并常见命令集合 自建 npm 资源库&#xff0c;使用Verdaccio。如果觉得麻烦&#xff0c;直接可以在外网注册 https://www.npmjs.com/ 网站。大同小异&#xff0c;自己搭建搭建方便局域网…...

uniapp 对接腾讯云IM群组成员管理(增删改查)

UniApp 实战&#xff1a;腾讯云IM群组成员管理&#xff08;增删改查&#xff09; 一、前言 在社交类App开发中&#xff0c;群组成员管理是核心功能之一。本文将基于UniApp框架&#xff0c;结合腾讯云IM SDK&#xff0c;详细讲解如何实现群组成员的增删改查全流程。 权限校验…...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

HTML 列表、表格、表单

1 列表标签 作用&#xff1a;布局内容排列整齐的区域 列表分类&#xff1a;无序列表、有序列表、定义列表。 例如&#xff1a; 1.1 无序列表 标签&#xff1a;ul 嵌套 li&#xff0c;ul是无序列表&#xff0c;li是列表条目。 注意事项&#xff1a; ul 标签里面只能包裹 li…...

MVC 数据库

MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)

宇树机器人多姿态起立控制强化学习框架论文解析 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架&#xff08;一&#xff09; 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

自然语言处理——Transformer

自然语言处理——Transformer 自注意力机制多头注意力机制Transformer 虽然循环神经网络可以对具有序列特性的数据非常有效&#xff0c;它能挖掘数据中的时序信息以及语义信息&#xff0c;但是它有一个很大的缺陷——很难并行化。 我们可以考虑用CNN来替代RNN&#xff0c;但是…...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

Xen Server服务器释放磁盘空间

disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...

云原生安全实战:API网关Kong的鉴权与限流详解

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关&#xff08;API Gateway&#xff09; API网关是微服务架构中的核心组件&#xff0c;负责统一管理所有API的流量入口。它像一座…...