linux中一切都是文件

One of the defining features of Linux and other UNIX-like operating systems is that “everything is a file.” This is an oversimplification, but understanding what it means will help you understand how Linux works.

Linux和其他类似UNIX的操作系统的定义特征之一是“一切都是文件”。 这是一个过分的简化,但是了解其含义将帮助您了解Linux的工作方式。

Many things on Linux appear in your file system, but they aren’t actually files. They’re special files that represent hardware devices, system information, and other things — including a random number generator.

Linux上的许多内容都出现在文件系统中,但实际上并不是文件。 它们是代表硬件设备,系统信息和其他事物的特殊文件,包括随机数生成器。

These special files may be located in pseudo or virtual file systems such as /dev, which contains special files that represent devices, and /proc, which contains special files that represent system and process information.

这些特殊文件可能位于伪文件系统或虚拟文件系统中,例如/ dev(其中包含代表设备的特殊文件)和/ proc(其中包含代表系统和进程信息的特殊文件)。

/ proc (/proc)

For example, let’s say you want to find information about your CPU. The /proc directory contains a special file – /proc/cpuinfo – that contains this information.

例如,假设您要查找有关CPU的信息。 / proc目录包含一个特殊文件-/ proc / cpuinfo-包含此信息。

You don’t need a special command that tells you your CPU info – you can just read the contents of this file using any standard command that works with plain-text files. For example, you could use the command cat /proc/cpuinfo to print this file’s contents to the terminal – printing your CPU information to the terminal. You could even open /proc/cpuinfo in a text editor to view its contents.

您不需要一个特殊的命令来告诉您您的CPU信息,您只需使用适用于纯文本文件的任何标准命令即可读取该文件的内容。 例如,您可以使用命令cat / proc / cpuinfo将文件的内容打印到终端-将CPU信息打印到终端。 您甚至可以在文本编辑器中打开/ proc / cpuinfo来查看其内容。

Remember, /proc/cpuinfo isn’t actually a text file containing this information – the Linux kernel and the proc file system are exposing this information to us as a file. This allows us to use familiar tools to view and work with the information.

请记住,/ proc / cpuinfo实际上不是包含此信息的文本文件-Linux内核和proc文件系统会将这些信息作为文件公开给我们。 这使我们能够使用熟悉的工具来查看和使用信息。

The /proc directory also contains other similar files, for example:

/ proc目录还包含其他类似文件,例如:

  • /proc/uptime – Exposes the uptime of your Linux kernel – in other words, how long your system has been on without shutting down./ proc / uptime-公开Linux内核的正常运行时间-换句话说,您的系统在不关闭的情况下已经运行了多长时间。
  • /proc/version – Exposes the version of your Linux kernel./ proc / version-公开Linux内核的版本。

/ dev (/dev)

In the /dev directory, you’ll find files that represent devices – as well as files that represent other special things. For example, /dev/cdrom is your CD-ROM drive. /dev/sda represents your first hard drive, while /dev/sda1 represents the first partition on your first hard drive.

在/ dev目录中,您将找到代表设备的文件以及代表其他特殊内容的文件。 例如,/ dev / cdrom是您的CD-ROM驱动器。 / dev / sda代表您的第一个硬盘驱动器,而/ dev / sda1代表您的第一个硬盘驱动器的第一个分区。

Want to mount your CD-ROM? Run the mount command and specify /dev/cdrom as the device you want to mount. Want to partition your first hard drive? Run a disk-partitioning utility and specify /dev/sda as the hard disk you want to edit. Want to format the first partition on your first hard drive? Run a formatting command and tell it to format /dev/sda1.

要挂载CD-ROM吗? 运行mount命令,然后将/ dev / cdrom指定为要挂载的设备。 是否要分区第一个硬盘? 运行磁盘分区实用程序,然后将/ dev / sda指定为要编辑的硬盘。 是否要格式化第一个硬盘上的第一个分区? 运行格式化命令,并告诉它格式化/ dev / sda1。

As you can see, exposing these devices as part of the file system has its advantages. The file system provides a consistent “name space” that all applications can use to address and access the devices.

如您所见,将这些设备作为文件系统的一部分公开具有其优势。 文件系统提供了一个一致的“名称空间”,所有应用程序都可以使用该名称空间来访问和访问设备。

/ dev / null,/ dev / random和/ dev / zero (/dev/null, /dev/random, and /dev/zero)

The /dev file system doesn’t just contain files that represent physical devices. Here are three of the most notable special devices it contains:

/ dev文件系统不仅包含代表物理设备的文件。 以下是其中包含的三个最著名的特殊设备:

  • /dev/null – Discards all data written to it – think of it as a trash can or black hole. If you ever see a comment telling you to send complains to /dev/null – that’s a geeky way of saying “throw them in the trash.”/ dev / null –丢弃所有写入其中的数据–将其视为垃圾桶或黑洞。 如果您看到一条评论告诉您将投诉发送到/ dev / null,则这是一种怪异的说法,即“将其扔进垃圾桶”。
  • /dev/random – Produces randomness using environmental noise. It’s a random number generator you can tap into./ dev / random –使用环境噪声产生随机性。 您可以利用它是一个随机数生成器。
  • /dev/zero – Produces zeros – a constant stream of zeros./ dev / zero –产生零–恒定的零流。

If you think of these three as files, you won’t see a use for them. Instead, think of them as tools.

如果您将这三个文件视为文件,则不会看到它们的用途。 而是将它们视为工具。

For example, by default, Linux commands produce error messages and other output that they print to the standard output, normally the terminal. If you want to run a command and don’t care about its output, you can redirect that output to /dev/null. Redirecting a command’s output to /dev/null immediately discards it. Instead of having every command implement its own “quiet mode,” you can use this method with any command.

例如,默认情况下,Linux命令会生成错误消息和其他输出,并将它们输出到标准输出(通常是终端)。 如果要运行命令而不关心其输出,可以将该输出重定向到/ dev / null。 将命令的输出重定向到/ dev / null立即将其丢弃。 您可以使每个命令都使用此方法,而不是让每个命令都实现自己的“安静模式”。

command > /dev/null

命令> / dev / null

If you wanted a source of randomness – say, for generating an encryption key, you wouldn’t need to write your own random number generator – you could use /dev/random.

如果您想要一个随机性源(例如,生成一个加密密钥,则不需要编写自己的随机数生成器),则可以使用/ dev / random。

To erase a hard drive’s contents by writing 0’s to it, you don’t need a special utility dedicated to zero’ing a drive – you could use standard utilities and /dev/zero. For example, the dd command reads from a location and writes to another location. The following command would read zeros from /dev/zero and write them directly to the first hard disk partition on your system, completely erasing its contents.

要通过向硬盘写入0来擦除硬盘的内容,您不需要专用于将驱动器归零的特殊实用程序–您可以使用标准实用程序和/ dev / zero。 例如, dd命令从一个位置读取并写入另一个位置。 以下命令将从/ dev / zero中读取零,并将它们直接写入系统中的第一个硬盘分区,从而完全擦除其内容。

(Warning: This command will erase all data on your first partition if you run it. Only run this command if you want to destroy data.)

( 警告 :如果运行该命令,它将擦除第一个分区上的所有数据。如果要销毁数据,请仅运行此命令。)

dd if=/dev/zero of=/dev/sda1

dd if = / dev / zero of = / dev / sda1

Here we’re using dd with special files (/dev/zero and /dev/sda1), but we could also use dd to read from and write to actual files. The same command works both for manipulating devices directly and working with files.

在这里,我们将dd与特殊文件(/ dev / zero和/ dev / sda1)一起使用,但是我们也可以使用dd读取和写入实际文件。 相同的命令既可用于直接操作设备,也可用于处理文件。

澄清度 (Clarification)

In practice, it’s more accurate to say that “everything is a stream of bytes” than “everything is a file.” /dev/random isn’t a file, but it certainly is a stream of bytes. And, although these things technically aren’t files, they are accessible in the file system – the file system is a universal “name space” where everything is accessible. Want to access a random number generator or read directly from a device? You’ll find both in the file system; no other form of addressing needed.

实际上,说“一切都是字节流”比“一切都是文件”更为准确。 / dev / random不是文件,但肯定是字节流。 而且,尽管从技术上讲这些东西不是文件,但它们可以在文件系统中访问-文件系统是通用的“名称空间”,在这里可以访问所有内容。 想要访问随机数生成器还是直接从设备读取? 您会在文件系统中找到它们。 不需要其他形式的寻址。

Of course, some things aren’t actually files – processes running on your system aren’t a part of the file system. “Everything is a file” is inaccurate, but lots of things do behave as files.

当然,有些东西实际上不是文件–系统上运行的进程不是文件系统的一部分。 “一切都是文件”是不准确的,但是很多事情的确表现为文件。

翻译自: https://www.howtogeek.com/117939/htg-explains-what-everything-is-a-file-means-on-linux/

linux中一切都是文件

linux中一切都是文件_在Linux中,“一切都是文件”是什么意思?相关推荐

  1. 中lisp文件_关于 Emacs 中的变量你需要知道的事情 | Linux 中国

    学习 Elisp 是如何处理变量的,以及如何在你的脚本与配置中使用它们. 来源:https://linux.cn/article-12150-1.html 作者:Clemens Radermacher ...

  2. linux系统中自动生成snap文件_在Linux操作系统下自动生成Makefile的方法

    在Linux操作系统下进行开发,编写Makefile似乎是不可缺少的事情.但是对于一个比较大的工程,编写一个符合规范的Makefile并非易事.而且由于Makefile的各种显式,隐式规则,加之平时并 ...

  3. 转储sql文件_在Linux上SQL Server中更改SQL转储文件位置

    转储sql文件 In this article, we will talk about SQL Dump files and the process to change the dump direct ...

  4. linux中打开pdf文件_在Linux中减少PDF文件大小

    linux中打开pdf文件 In our Linux system, If we have a large PDF file, we may want to reduce it's size. We ...

  5. linux 解压所有以zip结尾的文件_在 Linux 上压缩文件:zip 命令的各种变体及用法...

    除了压缩和解压缩文件外,你还可以使用 zip 命令执行许多有趣的操作.这是一些其他的 zip 选项以及它们如何提供帮助. -- Sandra Henry-stocker(作者) 为了节省一些磁盘空间并 ...

  6. python博客下载本地文件_解决django无法访问本地static文件(js,css,img)网页里js,cs都加载不了...

    1.今天网上下载一个博客项目,发现本地访问,js,css加载不了. 我想应该是项目上线的安全措施,但是我想调试项目.找到方法如下 在settings.py里面编辑 添加 STATICFILES_DIR ...

  7. linux清除configure文件_在Linux操作系统下自动生成Makefile的方法

    在Linux操作系统下进行开发,编写Makefile似乎是不可缺少的事情.但是对于一个比较大的工程,编写一个符合规范的Makefile并非易事.而且由于Makefile的各种显式,隐式规则,加之平时并 ...

  8. linux 穿件文件_关于Linux的25件事

    linux 穿件文件 今天是Linux 25年的历史,这是有史以来最成功的软件. 在本周的LinuxCon上,Linux基金会的Jim Zemlin在主题演讲中表达了钦佩,赞美和激动,他说:" ...

  9. linux major头文件_《Linux设备驱动程序》(四)——字符设备驱动(上)

    之前我们说过,Linux设备主要分为三类:字符设备.块设备和网络接口. 字符设备相对于另外两个设备更加容易理解,同时,这类设备也适合大多数简单的硬件设备,因此,接下来我们学习一下字符设备驱动. 字符驱 ...

最新文章

  1. CakePHP 2.x CookBook 中文版 第二章 安装
  2. OceanBase数据库实践入门——性能测试建议
  3. html 获取本地txt输出,html 5 读取本地文件API
  4. iOS点击获取短信验证码按钮
  5. 汇编语言 王爽 第四版 第三章 检测点3.1
  6. 【OCR】AspriseOCR C# 英文、数字识别(中文不行)
  7. PHPExcel 插件使用详解
  8. 持久层框架有哪些?区别是什么?Mybatis框架的优点和缺点
  9. 计算机tpu定义,tpu材料
  10. VARCHART XGantt系列教程:如何在分组视图中更好地显示节点
  11. 如何隐藏电脑里的文件或者文件夹?
  12. Codeforces - Mike and Feet
  13. Error: Incorrect contents fetched, please reload.
  14. bugku writeup(misc_1)
  15. 科达出征珠海航展,共筑蓝天梦想
  16. autocad ios 虚线_CAD交互绘制虚线(com接口)
  17. 使用DeepWalk从图中提取特征
  18. Spring Boot、Spring Cloud 自定义配置文件(如何整合配置中心)
  19. bat批量安装软件,完成最后删除文件夹里所有安装包
  20. 苹果系统 如何快速访问服务器,4如何在iPhone / Windows / Mac上快速访问iCloud照片

热门文章

  1. Material icons全部图标截图(共1117个)
  2. 低温烹饪过程中真空压力的自动控制
  3. 【TrackVis 教程】基于DTI的纤维追踪及显示
  4. java数组释放空间函数_D4java基础(语句,函数,数组)
  5. Android定时关机
  6. 万科股权之争,在公司章程方面给了你怎样的启发?
  7. 问题排查_服务调用失败
  8. 自顶向下,逐步求精的案例
  9. failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: ErrCode:503, ErrM
  10. 数据结构与算法_04_回头是岸依好汉(回溯算法)