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

linux中一切都是文件

linux中一切都是文件

image

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-包含此信息。

image

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文件系统会将这些信息作为文件公开给我们。 这使我们能够使用熟悉的工具来查看和使用信息。

image

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。

image

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

image

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中一切都是文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值