当前位置: 首页 > 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;…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

基于Flask实现的医疗保险欺诈识别监测模型

基于Flask实现的医疗保险欺诈识别监测模型 项目截图 项目简介 社会医疗保险是国家通过立法形式强制实施&#xff0c;由雇主和个人按一定比例缴纳保险费&#xff0c;建立社会医疗保险基金&#xff0c;支付雇员医疗费用的一种医疗保险制度&#xff0c; 它是促进社会文明和进步的…...

Java 加密常用的各种算法及其选择

在数字化时代&#xff0c;数据安全至关重要&#xff0c;Java 作为广泛应用的编程语言&#xff0c;提供了丰富的加密算法来保障数据的保密性、完整性和真实性。了解这些常用加密算法及其适用场景&#xff0c;有助于开发者在不同的业务需求中做出正确的选择。​ 一、对称加密算法…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

Go 并发编程基础:通道(Channel)的使用

在 Go 中&#xff0c;Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式&#xff0c;用于在多个 Goroutine 之间传递数据&#xff0c;从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...

如何在Windows本机安装Python并确保与Python.NET兼容

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…...

React从基础入门到高级实战:React 实战项目 - 项目五:微前端与模块化架构

React 实战项目&#xff1a;微前端与模块化架构 欢迎来到 React 开发教程专栏 的第 30 篇&#xff01;在前 29 篇文章中&#xff0c;我们从 React 的基础概念逐步深入到高级技巧&#xff0c;涵盖了组件设计、状态管理、路由配置、性能优化和企业级应用等核心内容。这一次&…...

Win系统权限提升篇UAC绕过DLL劫持未引号路径可控服务全检项目

应用场景&#xff1a; 1、常规某个机器被钓鱼后门攻击后&#xff0c;我们需要做更高权限操作或权限维持等。 2、内网域中某个机器被钓鱼后门攻击后&#xff0c;我们需要对后续内网域做安全测试。 #Win10&11-BypassUAC自动提权-MSF&UACME 为了远程执行目标的exe或者b…...

基于Uniapp的HarmonyOS 5.0体育应用开发攻略

一、技术架构设计 1.混合开发框架选型 &#xff08;1&#xff09;使用Uniapp 3.8版本支持ArkTS编译 &#xff08;2&#xff09;通过uni-harmony插件调用原生能力 &#xff08;3&#xff09;分层架构设计&#xff1a; graph TDA[UI层] -->|Vue语法| B(Uniapp框架)B --&g…...

Spring是如何实现无代理对象的循环依赖

无代理对象的循环依赖 什么是循环依赖解决方案实现方式测试验证 引入代理对象的影响创建代理对象问题分析 源码见&#xff1a;mini-spring 什么是循环依赖 循环依赖是指在对象创建过程中&#xff0c;两个或多个对象相互依赖&#xff0c;导致创建过程陷入死循环。以下通过一个简…...