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.
功能概述
- 缩小或扩展文件到指定的大小;
- (默认)文件不存在会被创建;
- 如果文件大于指定的大小,额外的(末尾的)数据将被丢弃;
- 如果文件小于指定的大小,则对其进行扩展,并且扩展部分读取为零字节;
实例(可用于删除文件末尾指定大小的内容)
-
-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.....| -
-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 |.....| -
-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
实例(可用于删除文件末尾指定大小的内容)
-
-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$ -
-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.
功能概述:
- -n:显示文件的最后 n 行,默认为 10 行。
- -f:实时追踪文件的变化并输出新增的内容。
- -q:不显示文件名。
- -s:设置输出的间隔时间(秒)。
- -c:以字节为单位显示指定范围的内容。
实例(可用于删除文件开头指定大小的内容)
-
-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 -
-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 -
-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功能概述实例(可用于删除文件末尾指定大小的内容) 二、head功能概述实例(可用于删除文件末尾指定大小的内容) 三、tail功能概述:实例(可用于删除文件开头指定大小的内容) 四、…...
Armbian安装python环境和pip国内源
文章目录 安装python环境配置pip为国内源 安装python环境 更新软件包列表: sudo apt update安装 Python 3(通常是最新版本): sudo apt install python3验证 Python 安装是否成功: python3 --version运行上述命令后&…...
宿主Linux——KVM安装Windows7系统
KVM虚拟技术 KVM(Kernel-based Virtual Machine) 是基于Linux内核的开源虚拟化技术,在一台物理机上可同时运行多个虚拟系统。KVM使用硬件虚拟化扩展,例如Intel的VT和AMD的AMD-V,在性能方面更加高效,可提供更好的计算能力和响应速…...
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语言——指针(三)
📝前言: 上篇文章C语言——指针(二)中对:指针的运算和指针变量类型对指针使用的影响开展了进一步的探讨,这篇文章我们继续学习一下指针与一维数组之间的关系: 1,对数组名的理解 2&am…...
VIR-SLAM代码分析3——VIR_VINS详解之estimator.cpp/.h
前言 续接上一篇,本本篇接着介绍VIR-SLAM中estimator.cpp/.h文件的函数,尤其是和UWB相关的相比于VINS改动过的函数,仍然以具体功能情况代码注释的形式进行介绍。 重点函数介绍 优化函数,代码是先优化,后边缘化。 …...
大模型的RPA应用 | 代理流程自动化(APA),开启智能自动化新纪元
随着技术创新的持续推进,自动化技术已经变得至关重要,成为驱动企业和社会向前发展的核心动力。在自动化的里程碑中,机器人流程自动化(RPA)已经有效地将简单、重复且规则性的任务自动化。可是随着对处理更为复杂、多变且…...
爬虫学习 异步爬虫(五)
多线程 多进程 协程 进程 运行中的程序 线程 被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是一个云计算平台的项目,其中Keystone是一个身份认证服务组件,它提供了认证、授权和目录的服务。其他OpenStack服务组件都需要使用Keystone来验证用户的身份和权限,并且彼此之间需要相互协作。当一个OpenStack服务组件接收到用户的请…...
java学习part22包装类
119-面向对象(高级)-包装类的理解_基本数据类型与包装类间的转换_哔哩哔哩_bilibili 1.包装类 2.基本转包装方式 2.1new方式 源码 2.2valueof() 3.包装转基本 4.基本类型和包装类型的默认值不一样 比如boolean默认false Boolean默认null(对…...
【场景测试用例】二维码
测试思路: UI 不同设备,不同浏览器下的外观和布局一致用户友好性 二维码足够清晰且大小合适是否有错误提示是否有扫描成功/失败提示启动,扫描过程 功能 验证识别功能 二维码完整且有效二维码失效二维码不完整/过于模糊空白二维码测试不同大小…...
如何提高销售技巧,增加客户的成交率?
如何提高销售技巧,增加客户的成交率? 在如今的市场环境中,销售技巧的高低往往决定了你是否能够成功地打动客户的心。想要提高销售业绩,除了产品质量和服务的保障,更需要你精进销售技巧,从而让客户愿意为你…...
软件设计之生成器模式
理解生成器模式在于:一个对象若由多个部分组成,只要构建好这些部分然后拼接到一起就组成了一个完整的对象。比如一台电脑,它的类型可以不一样,可以是苹果的,可以是联想的,等等。同一款电脑它的组件也不一样…...
【Vulnhub 靶场】【CEREAL: 1】【困难】【20210529】
1、环境介绍 靶场介绍:https://www.vulnhub.com/entry/cereal-1,703/ 靶场下载:https://download.vulnhub.com/cereal/Cereal.ova 靶场难度:困难 发布日期:2021年5月29日 文件大小:1.1 GB 靶场作者:Thomas…...
【Vulnhub靶机】Jarbas--Jenkins
文章目录 信息收集主机发现端口扫描目录爆破 漏洞探测whatwebhash-identifierwhatweb 文档说明:https://www.vulnhub.com/entry/jarbas-1,232/ 靶机下载:Download (Mirror): 信息收集 主机发现 扫描C段 sudo nmap -sn 10.9.75.0/24端口扫描 sudo nma…...
Java面向对象第8天
精华笔记: 接口: 是一种引用数据类型 由interface定义 只能包含常量和抽象方法 不能被实例化 接口是需要被实现/继承的,实现类/派生类:必须重写接口中的所有抽象方法 一个类可以实现多个接口,用逗号分隔。若又继承…...
数据结构与算法复习笔记
1.数据结构基本概念 数据结构: 它是研究计算机数据间关系,包括数据的逻辑结构和存储结构及其操作。 数据(Data):数据即信息的载体,是能够输入到计算机中并且能被计算机识别、存储和处理的符号总称。 数据元素…...
关于微服务的思考
目录 什么是微服务 定义 特点 利弊 引入时机 需要哪些治理环节 从单体架构到微服务架构的演进 单体架构 集群和垂直化 SOA 微服务架构 如何实现微服务架构 服务拆分 主流微服务解决方案 基础设施 下一代微服务架构Service Mesh 什么是Service Mesh?…...
计算机毕业设计 基于Web的课程设计选题管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
博主介绍:✌从事软件开发10年之余,专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ 🍅文末获取源码联系🍅 👇🏻 精…...
群晖NAS:docker(Container Manager)、npm安装Verdaccio并常见命令集合
群晖NAS:docker(Container Manager)、npm安装Verdaccio并常见命令集合 自建 npm 资源库,使用Verdaccio。如果觉得麻烦,直接可以在外网注册 https://www.npmjs.com/ 网站。大同小异,自己搭建搭建方便局域网…...
赋能智能体大脑:在快马平台中集成AI模型实现高级对话能力
在探索AI辅助开发的过程中,我发现智能体的核心能力很大程度上取决于其"大脑"——也就是背后支撑决策的AI模型。最近在InsCode(快马)平台实践了一个很有意思的项目:如何为智能体集成AI模型来实现高级对话功能。整个过程让我深刻体会到ÿ…...
高纯水系统如何保障锂电池生产良率?
在锂电池制造过程中,生产用水纯度直接关联产品性能与安全。随着新能源汽车与储能产业快速发展,行业对电池一致性与稳定性的要求持续提升,超纯水已成为核心制程环节的关键辅材。一、锂电池生产用水标准 锂电池生产涉及正负材料制备、浆料调配、…...
接口实现第二步骤
接口实现流程模块化路由 -> API 接口规范文档定义模型类 -> 数据库表 (数据库设计文档)在 crud 文件夹里面创建文件,封装操作数据库的方法在路由处理函数里面调用 crud 封装好的方法,响应结果定义模型类规范基类,…...
【3步修复】华硕游戏本色彩配置文件丢失解决方案
【3步修复】华硕游戏本色彩配置文件丢失解决方案 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Strix, Scar, and other mo…...
长生露模式系统开发
模式系统设计 长生露模式通常指结合健康管理、会员服务或直销体系的综合系统。开发需明确业务模式定位,如会员积分、分销奖励或健康数据追踪。核心模块包括用户分层、权益分配、数据分析和后台管理。技术架构选择 采用微服务架构确保系统可扩展性,推荐Sp…...
GHelper终极指南:如何用开源工具彻底掌控华硕笔记本性能
GHelper终极指南:如何用开源工具彻底掌控华硕笔记本性能 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Strix, …...
后台管理系统布局设计指南:从架构到实践的全方位解析
后台管理系统布局设计指南:从架构到实践的全方位解析 【免费下载链接】vue3-element-admin 🔥基于 Vue 3 Vite 7 TypeScript element-plus 构建的后台管理前端模板(配套后端源码),vue-element-admin 的 vue3 版本。 …...
Tealdeer终极指南:5分钟掌握命令行工具的快速使用技巧
Tealdeer终极指南:5分钟掌握命令行工具的快速使用技巧 【免费下载链接】tealdeer A very fast implementation of tldr in Rust. 项目地址: https://gitcode.com/gh_mirrors/te/tealdeer Tealdeer是一个基于Rust语言开发的极速tldr客户端实现,为命…...
116. 为项目监控员生成的警报添加标签
Procedure 程序To label alerts for Project Monitors, you must configure the Prometheus Federator Helm charts values section. This is done by adding additionalRuleLabels under defaultRules within helmProjectOperator. You can perform this modification during…...
Jetson Orin Nano环境搭建避坑实录:从JetPack到PyQt5,我踩过的那些‘坑’都帮你填平了
Jetson Orin Nano环境搭建避坑实录:从JetPack到PyQt5的实战指南 第一次拿到Jetson Orin Nano这块开发板时,我天真地以为按照官方文档就能轻松搞定所有环境配置。结果从JetPack安装到PyQt5编译,几乎每一步都遇到了意想不到的问题。这篇文章不会…...
