当前位置: 首页 > 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;自己搭建搭建方便局域网…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

装饰模式(Decorator Pattern)重构java邮件发奖系统实战

前言 现在我们有个如下的需求&#xff0c;设计一个邮件发奖的小系统&#xff0c; 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式&#xff08;Decorator Pattern&#xff09;允许向一个现有的对象添加新的功能&#xff0c;同时又不改变其…...

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

HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...

Vue2 第一节_Vue2上手_插值表达式{{}}_访问数据和修改数据_Vue开发者工具

文章目录 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染2. 插值表达式{{}}3. 访问数据和修改数据4. vue响应式5. Vue开发者工具--方便调试 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染 准备容器引包创建Vue实例 new Vue()指定配置项 ->渲染数据 准备一个容器,例如: …...

k8s业务程序联调工具-KtConnect

概述 原理 工具作用是建立了一个从本地到集群的单向VPN&#xff0c;根据VPN原理&#xff0c;打通两个内网必然需要借助一个公共中继节点&#xff0c;ktconnect工具巧妙的利用k8s原生的portforward能力&#xff0c;简化了建立连接的过程&#xff0c;apiserver间接起到了中继节…...

Caliper 配置文件解析:config.yaml

Caliper 是一个区块链性能基准测试工具,用于评估不同区块链平台的性能。下面我将详细解释你提供的 fisco-bcos.json 文件结构,并说明它与 config.yaml 文件的关系。 fisco-bcos.json 文件解析 这个文件是针对 FISCO-BCOS 区块链网络的 Caliper 配置文件,主要包含以下几个部…...

服务器--宝塔命令

一、宝塔面板安装命令 ⚠️ 必须使用 root 用户 或 sudo 权限执行&#xff01; sudo su - 1. CentOS 系统&#xff1a; yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh2. Ubuntu / Debian 系统…...

AI病理诊断七剑下天山,医疗未来触手可及

一、病理诊断困局&#xff1a;刀尖上的医学艺术 1.1 金标准背后的隐痛 病理诊断被誉为"诊断的诊断"&#xff0c;医生需通过显微镜观察组织切片&#xff0c;在细胞迷宫中捕捉癌变信号。某省病理质控报告显示&#xff0c;基层医院误诊率达12%-15%&#xff0c;专家会诊…...

【无标题】路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论

路径问题的革命性重构&#xff1a;基于二维拓扑收缩色动力学模型的零点隧穿理论 一、传统路径模型的根本缺陷 在经典正方形路径问题中&#xff08;图1&#xff09;&#xff1a; mermaid graph LR A((A)) --- B((B)) B --- C((C)) C --- D((D)) D --- A A -.- C[无直接路径] B -…...

Pydantic + Function Calling的结合

1、Pydantic Pydantic 是一个 Python 库&#xff0c;用于数据验证和设置管理&#xff0c;通过 Python 类型注解强制执行数据类型。它广泛用于 API 开发&#xff08;如 FastAPI&#xff09;、配置管理和数据解析&#xff0c;核心功能包括&#xff1a; 数据验证&#xff1a;通过…...