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

Linux shell 重定向输入和输出

Linux shell 重定向输入和输出

  • 1. Standard I/O streams
  • 2. Redirecting to and from the standard file handles (标准文件句柄的重定向)
    • 2.1. `command > file`
    • 2.2. `command >> file`
    • 2.3. `command 2> file`
    • 2.4. `command 2>> file`
    • 2.5. `command < file`
    • 2.6. `command < infile > outfile`
    • 2.7. `command > file 2>&1`
    • 2.8. `command 2>&1 > file`
  • References

In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
在计算机领域,重定向是大多数命令行解释器所具有的功能,包括各种可以将标准流重定向用户规定目的地的 Unix shells。

1. Standard I/O streams

In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection.
源自 Bourne shell 的许多 Unix shell,可以将一个数字 (文件描述符) 放在重定向符号前,这样可以影响用于重定向的数据流。

The Unix standard I/O streams are:

File DescriptorNameDescription操作符
0stdinstandard input (标准输入)<, <<
1stdoutstandard output (标准输出)>, >>, 1>, 1>>
2stderrstandard error (标准错误输出)2>, 2>>

文件描述符 (File Descriptor) 是进程对其所打开文件的索引,形式上是个非负整数。Linux 会为每个运行的进程都分配这三个文件。

stdin 默认输入源是 Keyboard,stdoutstderr 默认输出目的地是 Text Terminal。

默认情况下,command > filestdout 重定向到 filecommand < filestdin 重定向到 file

在这里插入图片描述

使用 >>> 时,默认为 stdout (标准输出) 重定向,> 就是 1>1> 之间不能有空格。File Descriptor 0, 1, 2 与它后面的操作符 >, >>, <, << 是一个整体。

ls -a -l > yongqiang.txt 等同于 ls -a -l 1> yongqiang.txt

(base) yongqiang@yongqiang:~/software$ ls -a -l
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun  4 22:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ ls -a -l > yongqiang.txt
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:14 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
-rw-r--r--  1 yongqiang yongqiang         0 Jun 16 15:14 yongqiang.txt
(base) yongqiang@yongqiang:~/software$

yongqiang.txt 文件中有 -rw-r--r-- 1 yongqiang yongqiang 0 Jun 16 15:14 yongqiang.txt 行。yongqiang.txt 文件是在 stdout (标准输出) 重定向之前创建的,需要准备好输出目的地之后,stdout 才会发送到该目的地。

(base) yongqiang@yongqiang:~/software$ ls -a -l
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ ls -a -l 1> yongqiang.txt
(base) yongqiang@yongqiang:~/software$
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
total 864112
drwxr-xr-x  5 yongqiang yongqiang      4096 Jun 16 15:26 .
drwxr-xr-x 26 yongqiang yongqiang      4096 Jun  4 22:31 ..
-rwxr-xr-x  1 yongqiang yongqiang 103219356 Jul 14  2023 Miniconda3-latest-Linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang  47611219 Dec  7  2021 bazel-4.2.1-installer-linux-x86_64.sh
-rwxr-xr-x  1 yongqiang yongqiang 733973045 Feb 18 15:20 pycharm-professional-2023.1.5.tar.gz
-rw-r--r--  1 yongqiang yongqiang         0 Jun 16 15:26 yongqiang.txt
(base) yongqiang@yongqiang:~/software$

< - 输入重定向,命令默认从键盘获得输入,< 重定向从其它设备获得输入 (文件等)。
>, 1> - 输出 stdout 重定向。命令执行输出 stdout 默认打印到屏幕上,> or 1> 重定向 stdout 到其它输出设备 (文件等)。

<< - 输入追加重定向
>> - 输出追加重定向

2. Redirecting to and from the standard file handles (标准文件句柄的重定向)

2.1. command > file

command 命令的 stdout 重定向到文件 file 中。如果 file 已经存在,则清空原有文件。

command > file executes command, placing the output in file, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will clobber any existing data in file.
command > file 命令执行 command,然后将 stdout 的内容存入 file。注意任何 file 内的已经存在的内容将被清空,然后写入新内容。

clobber [ˈklɒbə(r)]:vt. 使受到 (严重经济损失),狠揍,猛打,惩罚,彻底战胜 (或击败),狠击,极大地打击 n. 衣服,随身物品

echo "yongqiangcheng" > file.txt 命令清空文件的内容,然后将 "yongqiangcheng" 写入 file.txt

(base) yongqiang@yongqiang:~/software$ echo `date` > yongqiang.txt
(base) yongqiang@yongqiang:~/software$ cat yongqiang.txt
Sun Jun 16 20:33:49 CST 2024
(base) yongqiang@yongqiang:~/software$

Note: 使用的是反引号 `, 而不是单引号 '。

2.2. command >> file

command 命令的 stdout 重定向到文件 file 中。如果 file 已经存在,则把信息加在原有文件后面。

To append output to the end of the file, rather than clobbering it, the >> operator is used: command1 >> file.
command1 >> filestdout 追加到文件末尾,使用 >> 操作符。

echo "yongqiangcheng" > file.txt 命令将 "yongqiangcheng" 追加到 file.txt 的后面。

2.3. command 2> file

For example, command 2> file executes command, directing the standard error stream to file.
执行 command,然后将标准错误 stderr 输出重定向到文件 file,清空文件中已有内容,然后写入新内容。

2.4. command 2>> file

执行 command,然后将标准错误 stderr 输出重定向追加到文件 file 末尾。

2.5. command < file

本来需要从键盘获取输入的命令会转移到文件读取内容。

Using command < file executes command, with file as the source of input, as opposed to the keyboard, which is the usual source for standard input.
执行 command,使用 file 作为用来替代键盘的输入源。

2.6. command < infile > outfile

command < infile > outfile combines the two capabilities: command reads from infile and writes to outfile.
同时替换输入和输出,执行 command,从文件 infile 读取内容,然后将 stdout 写入到 outfile 中。

2.7. command > file 2>&1

To write both stderr and stdout to file, the order should be reversed. stdout would first be redirected to the file, then stderr would additionally be redirected to the stdout handle that has already been changed to point at the file: command > file 2>&1.
首先将 stdout 重定向到 file,然后 stderr 将重定向到已经更改为指向 filestdout 句柄。

In shells derived from csh (the C shell), the syntax instead appends the & (ampersand) character to the redirect characters, thus achieving a similar result. The reason for this is to distinguish between a file named 1 and stdout, i.e. cat file 2>1 vs cat file 2>&1. In the first case, stderr is redirected to a file named 1 and in the second, stderr is redirected to stdout.
2>&1stderr 重定向到 stdout21 分别是 stderrstdout 的文件描述符。为了区别普通文件,当将 stderrstdout 作为重定向目的地时,需要在文件描述符前加上 &为了将 stdout 与文件名为 1 的文件区分开来。例如对于 cat file 2>1cat file 2>&1,前者会将 stderr 重定向至叫做 1 的文件,后者则将其重定向至 stdout

A simplified but non-POSIX conforming form of the command, command > file 2>&1 is: command &>file or command >&file.

2.8. command 2>&1 > file

It is possible to use 2>&1 before > but the result is commonly misunderstood. The rule is that any redirection sets the handle to the output stream independently. So 2>&1 sets handle 2 to whatever handle 1 points to, which at that point usually is stdout. Then > redirects handle 1 to something else, e.g. a file, but it does not change handle 2, which still points to stdout.
可以将 2>&1 放置在 > 前,但是这样并不能达到我们想要的效果。

In the following example, stdout is written to file, but stderr is redirected from stderr to stdout, i.e. sent to the screen: command 2>&1 > file.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] Redirection (computing), https://en.wikipedia.org/wiki/Redirection_(computing)

相关文章:

Linux shell 重定向输入和输出

Linux shell 重定向输入和输出 1. Standard I/O streams2. Redirecting to and from the standard file handles (标准文件句柄的重定向)2.1. command > file2.2. command >> file2.3. command 2> file2.4. command 2>> file2.5. command < file2.6. comm…...

electron录制工具-视频保存、编辑页面

效果如下 electron录屏-保存录制视频 资源 导出视频使用了 mp4-wasm&#xff0c;基本使用&#xff0c;可参考 此文 想法 1、点击按钮导出&#xff0c;弹出选择保存文件夹 2、保存成功后&#xff0c;自动打开保存后文件夹窗口并关闭窗口 实现 获取保存文件夹路径&#xff0…...

curl命令行发送post/get请求

文章目录 curl概述post请求get请求 curl概述 curl 是一个命令行实用程序&#xff0c;允许用户创建网络请求curl 在Windows、 Linux 和 Mac 上皆可使用 post请求 一个简单的 POST 请求 -X&#xff1a;指定与远程服务器通信时将使用哪种 HTTP 请求方法 curl -X POST http://ex…...

Redis 分片集群

一. 前言 前面文章介绍了主从集群和哨兵模式。其中主从集群可以通过读写分离的方式解决高并发场景下的读问题&#xff1b;而在主节点出现故障时&#xff0c;又可以通过哨兵模式的自动选举来实现高可用。 Redis 主从集群 && 哨兵模式 二. Redis 分片集群 2.1 分片集群…...

学习分享-Callable 和 Runnable 任务

前言 顺带回顾学习一下Callable 或 Runnable 任务 Callable 和 Runnable 任务 Callable 和 Runnable 是 Java 中用于定义任务的接口&#xff0c;它们主要用于并发编程&#xff0c;允许任务在独立的线程中运行。 Runnable 任务 Runnable 是一个函数式接口&#xff0c;只包含…...

three.js 基础01

1.场景创建 Scene() 2.常用形状集几何体「Geometry」[可设置长宽高等内容&#xff0c;如&#xff1a;new THREE.BoxGeometry(...)] 长方体 BoxGeometry圆柱体 CylinderGeometry 球体SphereGeometry圆锥体ConeGeometry矩形平面 PlaneGeometry 圆面体 CircleGeo…...

使用file.transferTo()做Java文件复制,目标文件存在时,是抛异常还是覆盖写入?

背景 最近在做一个项目&#xff0c;在服务端涉及到文件的复制操作&#xff0c;于是想到了 Java 中 FileInputStream 类的 transferTo() 方法。这里简单记录一下用法&#xff0c;另外&#xff0c;如果目标文件已经存在&#xff0c;该如何处理这种情况呢&#xff1f;是出现异常还…...

Python:线性查找法

什么是线性搜索算法&#xff1f; 线性搜索算法是一种基本的搜索技术&#xff0c;用于查找目标元素是否存在于一个集合&#xff08;通常是列表或数组&#xff09;中。该算法的工作原理非常简单&#xff1a;它从集合的第一个元素开始逐个检查&#xff0c;直到找到目标元素或遍历完…...

IDEA 设置主题、背景图片、背景颜色

一、设置主题 1、点击菜单 File -> Settings : 点击 Settings 菜单 2、点击 Editor -> Color Scheme -> Scheme, 小哈的 IDEA 版本号为 2022.2.3 , 官方默认提供了 4 种主题&#xff1a; Classic Light &#xff08;经典白&#xff09; ;Darcula &#xff08;暗黑主…...

【elementui源码解析】如何实现自动渲染md文档-第三篇

目录 1.前言 2.webpack.demo.js 3.markdown文档 4.fence.js 1&#xff09;tokens 2&#xff09;::: 3&#xff09; 5.containers.js 1&#xff09;markdown-it-container 2&#xff09;md.use() 3&#xff09;代码逻辑 4&#xff09;containers小结 6.congfig.js …...

this指针如何使C++成员指针可调用

在C中&#xff0c;this指针是一个隐藏的指针&#xff0c;指向当前对象实例。它在成员函数中自动可用&#xff0c;用于访问该对象的成员变量和成员函数。理解this指针的工作原理有助于理解为什么指向成员的指针是可调用的。在本文中&#xff0c;我们将详细探讨this指针的概念&am…...

Redis数据结构之字符串(sds)

Redis数据结构之字符串(sds) redisObject 定义如下 struct redisObject {unsigned type:4; //数据类型unsigned encoding:4; /*encoding 编码格式&#xff0c;及存储数据使用的数据结构&#xff0c;同一类型的数据&#xff0c;Redis 会根据数据量&#xff0c;占用内…...

tokenization(二)子词切分方法

文章目录 概述BPE构建词表词元化代码实现 WordPieceUnigram估算概率&#xff08;E&#xff09;删除词元&#xff08;M&#xff09; 参考资料 概述 接上回&#xff0c;子词词元化&#xff08;Subwords tokenization&#xff09;是平衡字符级别和词级别的一种方法&#xff0c;也…...

慈善组织管理系统设计

一、用户角色与权限 慈善组织管理系统设计首先需要考虑的是用户角色与权限的划分。系统应明确区分不同的用户角色&#xff0c;如管理员、项目负责人、财务人员、捐赠者等&#xff0c;并为每个角色分配相应的权限。管理员应拥有最高的权限&#xff0c;能够管理系统全局&#xf…...

大疆Pocket3手持记录仪格式化恢复方法

大疆Pocket系列是手持类产品&#xff0c;此类产品处理过不少像Pocket、Pocket2、Pocket3基本上涉及Pocket全系列&#xff0c;今天来看一个Pocket3误格式化之后的恢复方法。 故障存储: 120G存储卡 /文件系统&#xff1a;exFAT 故障现象: 在备份视频数据时由于操作失误导致初…...

Mybatis的面试题

1. 什么是一级缓存什么是二级缓存&#xff1f; MyBatis是一款优秀的持久层框架&#xff0c;它提供了一级缓存和二级缓存来提高数据库访问性能。 一级缓存 一级缓存是指在同一个SqlSession中进行的缓存。当MyBatis执行查询时&#xff0c;查询结果会被缓存在SqlSession的内存中…...

渗透测试之内核安全系列课程:Rootkit技术初探(五)

今天&#xff0c;我们来讲一下内核安全&#xff01; 本文章仅提供学习&#xff0c;切勿将其用于不法手段&#xff01; 目前&#xff0c;在渗透测试领域&#xff0c;主要分为了两个发展方向&#xff0c;分别为Web攻防领域和PWN&#xff08;二进制安全&#xff09;攻防领域。在…...

探索C嘎嘎的奇妙世界:第三关---缺省参数与函数重载

在c语言中,我们常常在对有参函数进行传参,这样的繁琐过程,C祖师爷对此进行了相关改进,多说无益,上干货: 1 缺省参数: 缺省参数是指在声明或定义函数时为函数的形参指定一个默认值&#xff08;默认参数&#xff09;。在调用该函数时&#xff0c;如果没有指定实参&#xff0c;则…...

docker拉取镜像太慢解决方案

前言 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子都在歌唱 创建daemon.json文件,输入以下信息 vim /etc/docker/daemon.json{"registry-mirrors": ["https://9cpn8tt6.mirror…...

仅凭一图,即刻定位,AI图像定位技术

AI图像定位技术&#xff0c;解锁空间密码&#xff01;仅凭一图&#xff0c;即刻定位&#xff0c;精准至经纬度坐标&#xff0c;让世界无处不晓。 试试看能否猜中这张自拍照的背景所在&#xff1f;可别低估了A的眼力&#xff0c;答案说不定会让你大吃一惊呢。 近期&#xff0c;…...

Springboot社区养老保险系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;社区养老保险系统小程序被用户普遍使用&#xff0c;为方…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...

【C++进阶篇】智能指针

C内存管理终极指南&#xff1a;智能指针从入门到源码剖析 一. 智能指针1.1 auto_ptr1.2 unique_ptr1.3 shared_ptr1.4 make_shared 二. 原理三. shared_ptr循环引用问题三. 线程安全问题四. 内存泄漏4.1 什么是内存泄漏4.2 危害4.3 避免内存泄漏 五. 最后 一. 智能指针 智能指…...

数学建模-滑翔伞伞翼面积的设计,运动状态计算和优化 !

我们考虑滑翔伞的伞翼面积设计问题以及运动状态描述。滑翔伞的性能主要取决于伞翼面积、气动特性以及飞行员的重量。我们的目标是建立数学模型来描述滑翔伞的运动状态,并优化伞翼面积的设计。 一、问题分析 滑翔伞在飞行过程中受到重力、升力和阻力的作用。升力和阻力与伞翼面…...

LCTF液晶可调谐滤波器在多光谱相机捕捉无人机目标检测中的作用

中达瑞和自2005年成立以来&#xff0c;一直在光谱成像领域深度钻研和发展&#xff0c;始终致力于研发高性能、高可靠性的光谱成像相机&#xff0c;为科研院校提供更优的产品和服务。在《低空背景下无人机目标的光谱特征研究及目标检测应用》这篇论文中提到中达瑞和 LCTF 作为多…...

Sklearn 机器学习 缺失值处理 获取填充失值的统计值

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 使用 Scikit-learn 处理缺失值并提取填充统计信息的完整指南 在机器学习项目中,数据清…...

DAY 26 函数专题1

函数定义与参数知识点回顾&#xff1a;1. 函数的定义2. 变量作用域&#xff1a;局部变量和全局变量3. 函数的参数类型&#xff1a;位置参数、默认参数、不定参数4. 传递参数的手段&#xff1a;关键词参数5 题目1&#xff1a;计算圆的面积 任务&#xff1a; 编写一…...

加密通信 + 行为分析:运营商行业安全防御体系重构

在数字经济蓬勃发展的时代&#xff0c;运营商作为信息通信网络的核心枢纽&#xff0c;承载着海量用户数据与关键业务传输&#xff0c;其安全防御体系的可靠性直接关乎国家安全、社会稳定与企业发展。随着网络攻击手段的不断升级&#xff0c;传统安全防护体系逐渐暴露出局限性&a…...

leetcode73-矩阵置零

leetcode 73 思路 记录 0 元素的位置&#xff1a;遍历整个矩阵&#xff0c;找出所有值为 0 的元素&#xff0c;并将它们的坐标记录在数组zeroPosition中置零操作&#xff1a;遍历记录的所有 0 元素位置&#xff0c;将每个位置对应的行和列的所有元素置为 0 具体步骤 初始化…...

【Zephyr 系列 16】构建 BLE + LoRa 协同通信系统:网关转发与混合调度实战

🧠关键词:Zephyr、BLE、LoRa、混合通信、事件驱动、网关中继、低功耗调度 📌面向读者:希望将 BLE 和 LoRa 结合应用于资产追踪、环境监测、远程数据采集等场景的开发者 📊篇幅预计:5300+ 字 🧭 背景与需求 在许多 IoT 项目中,单一通信方式往往难以兼顾近场数据采集…...