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

Linux 系统中的 /dev/null 文件

Linux 系统中的 /dev/null 文件

  • 1. Usage
  • References

In some operating systems, the null device is a device file that discards all data written to it but reports that the write operation succeeded.
/dev/null (空设备) 在类 Unix 系统中是一个特殊的设备文件,它丢弃一切写入其中的数据,但报告写入操作成功,读取它则会立即得到一个 EOF。/dev/null 也被称为比特桶或者黑洞 (bit bucket or black hole)。

This device is called /dev/null on Unix and Unix-like systems, NUL: (see TOPS-20) or NUL on CP/M and DOS (internally \DEV\NUL), nul on OS/2 and newer Windows systems (internally \Device\Null on Windows NT), NIL: on Amiga operating systems, and NL: on OpenVMS.

In Windows Powershell, the equivalent is $null. It provides no data to any process that reads from it, yielding EOF immediately. In IBM operating systems DOS/360 and successors and also in OS/360 and successors such files would be assigned in JCL to DD DUMMY.

1. Usage

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.
空设备通常被用于丢弃不需要的输出流,或作为用于输入流的空文件。这些操作通常由重定向完成。

The /dev/null device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.
/dev/null 是一个特殊文件,而不是目录,因此不能使用 Unix 命令 mv 将文件移动到其中。使用 rm 命令才是 Unix 中删除文件的正确方法。

This will return an End of File (EOF) character if you try to read it using the cat command.

(base) yongqiang@yongqiang:~$ ls -l /dev/null
crw-rw-rw- 1 root root 1, 3 Jun 17 23:02 /dev/null
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo "yongqiang" > /dev/null
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ cat /dev/null
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ stat /dev/nullFile: /dev/nullSize: 0               Blocks: 0          IO Block: 4096   character special file
Device: 5h/5d   Inode: 3           Links: 1     Device type: 1,3
Access: (0666/crw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-06-17 23:02:19.040000000 +0800
Modify: 2024-06-17 23:02:19.040000000 +0800
Change: 2024-06-17 23:02:19.040000000 +0800Birth: -
(base) yongqiang@yongqiang:~$

Redirecting stderr to stdout first, and then redirect stdout to /dev/null.

command > /dev/null 2>&1

Notice the 2>&1 at the end. We redirect stderr(2) to stdout(1). We use &1 to mention to the shell that the destination file is a file descriptor and not a file name.

So if we use 2>1, we will only redirect stderr to a file called 1.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] Null device, https://en.wikipedia.org/wiki/Null_device

相关文章:

  • HarmonyOS父子组件传递参数
  • ls命令的参数选项
  • 【初阶数据结构】二叉树(附题)
  • 大厂笔试真题讲解—京东23—夹吃棋
  • 线性代数|机器学习-P15矩阵A的低秩变换下的逆矩阵
  • Java中的Socket编程详解
  • 利用nodejs实现图片上传后端,并实现回显
  • 内存优化技巧:让数据处理更高效
  • 【数据结构】排序(下)
  • 前端基础操作1——利用nvm任意切换(管理)node版本
  • Nuxt快速学习开发 - Nuxt3静态资源Assets
  • Vue3 + Ant-Design 中 a-date-picke 实现选择切换年份 没有鼠标光标,输入框内自带‘年’
  • leetcode27移除元素
  • 无版权图片素材搜索网站,解决无版权图片查找问题
  • 逆向学习 MFC 篇:视图分割和在 C++ 的 Windows 窗口程序中添加图标的方法
  • 网络传输文件的问题
  • 2017 前端面试准备 - 收藏集 - 掘金
  • 230. Kth Smallest Element in a BST
  • Centos6.8 使用rpm安装mysql5.7
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • JAVA SE 6 GC调优笔记
  • Sublime text 3 3103 注册码
  • vagrant 添加本地 box 安装 laravel homestead
  • webpack入门学习手记(二)
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 精彩代码 vue.js
  • 使用common-codec进行md5加密
  • 小试R空间处理新库sf
  • 原生 js 实现移动端 Touch 滑动反弹
  • 《天龙八部3D》Unity技术方案揭秘
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • # Redis 入门到精通(八)-- 服务器配置-redis.conf配置与高级数据类型
  • #Lua:Lua调用C++生成的DLL库
  • $.ajax()
  • (5)STL算法之复制
  • (C语言)共用体union的用法举例
  • (zhuan) 一些RL的文献(及笔记)
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (初研) Sentence-embedding fine-tune notebook
  • (三)c52学习之旅-点亮LED灯
  • (三)mysql_MYSQL(三)
  • (推荐)叮当——中文语音对话机器人
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (一)Neo4j下载安装以及初次使用
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转)Google的Objective-C编码规范
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET文档生成工具ADB使用图文教程
  • /bin/rm: 参数列表过长"的解决办法
  • /proc/stat文件详解(翻译)
  • @ModelAttribute 注解
  • []error LNK2001: unresolved external symbol _m
  • [100天算法】-不同路径 III(day 73)