探索Linux文件系统
现在我们知道了如何在文件系统中切换目录,是时候开始指导我们的Linux系统了。在开始之前,我们将学习更多的命令,这些命令在此过程中将非常有用:
- ls – List directory contents
- file --Determine file type
- less – View file contents
ls
ls
用,我们可以查看目录内容并确定各种重要的文件和目录属性。可以简单地输入ls来查看当前工作目录中包含的文件和子目录的列表:
[root@iZerb5rob3dcf6Z ~]# ls
My_Hexo node_modules package-lock.json
除了当前的工作目录,我们还可以指定要列出的目录,如下所示:
[root@iZerb5rob3dcf6Z ~]# ls /usr/
bin etc games include lib lib64 libexec local sbin share src tmp
甚至指定多个目录。
[root@iZerb5rob3dcf6Z ~]# ls / /usr
/:
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
/usr:
bin etc games include lib lib64 libexec local sbin share src tmp
我们还可以更改输出格式以显示更多细节:
#-l 以长格式输出
[root@iZerb5rob3dcf6Z ~]# ls -l
总用量 52
drwxr-xr-x 6 root root 4096 6月 10 09:19 My_Hexo
drwxr-xr-x 134 root root 4096 6月 9 20:37 node_modules
-rw-r--r-- 1 root root 43129 6月 9 20:37 package-lock.json
选项和参数
command -options argument
大多数命令使用由单个字符加破折号组成的选项,例如“ -l”,但是许多命令也支持较长的选项,即由单词加两个破折号组成。另外,许多命令允许多个短期选择要串在一起。eg:ls -lt
#-l选项用于生成长格式输出,而- t选项用于按文件的修改时间对结果进行排序。
[root@iZerb5rob3dcf6Z ~]# ls -lt
总用量 52
drwxr-xr-x 6 root root 4096 6月 10 09:19 My_Hexo
-rw-r--r-- 1 root root 43129 6月 9 20:37 package-lock.json
drwxr-xr-x 134 root root 4096 6月 9 20:37 node_modules
我们将添加长选项--reverse
来反转排序顺序:
[root@iZerb5rob3dcf6Z ~]# ls -lt --reverse
总用量 52
drwxr-xr-x 134 root root 4096 6月 9 20:37 node_modules
-rw-r--r-- 1 root root 43129 6月 9 20:37 package-lock.json
drwxr-xr-x 6 root root 4096 6月 10 09:19 My_Hexo
ls
命令具有大量可能的选项。最常见的如下:
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below
-B, --ignore-backups
do not list implied entries ending with ~
-d, --directory
list directories themselves, not their contents
-D, --dired
generate output designed for Emacs' dired mode
-f do not sort, enable -aU, disable -ls --color
-F, --classify
append indicator (one of */=>@|) to entries
-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)
-l use a long listing format
-r, --reverse
reverse order while sorting
-R, --recursive
list subdirectories recursively
-s, --size
print the allocated size of each file, in blocks
-S sort by file size
--sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
--time=WORD
with -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c); also use specified time
as sort key if --sort=time
-t sort by modification time, newest first
file
使用file
命令确定文件的类型。Linux中不需要文件名来反映文件的内容。 通常希望文件名(例如“ picture.jpg”)包含JPEG压缩图像,但在Linux中则不需要。 我们可以这样调用文件命令:
file filename
[root@iZerb5rob3dcf6Z ~]# file My_Hexo/
My_Hexo/: directory
[root@iZerb5rob3dcf6Z ~]# file package-lock.json
package-lock.json: ASCII text
[root@iZerb5rob3dcf6Z ~]#
less
我们为什么要检查文本文件? 因为许多包含系统设置的文件(称为配置文件)都以这种格式存储,并且能够读取它们,这使我们对系统的工作方式有了更深入的了解。 此外,系统使用的许多实际程序(称为脚本)都以这种格式存储。 less
命令的用法如下:
[root@iZerb5rob3dcf6Z ~]# less /etc/passwd
一旦less
程序启动,我们就可以查看文件的内容。 如果文件长于一页,我们可以上下滚动。 “ q”
键退出。下表列出了少用的最常见的键盘命令。
less is more #少即是多
less
程序被设计为对称为more
的早期Unix程序的改进替代。该程序可轻松查看页面中的长文本文档。 按页面方式。 更多的程序只能向前翻页,而less
程序则允许向前和向后翻页,并且还具有许多其他功能。
Exploring The File System
以下列出了我们可以探索的一些目录。闲时尝试更多!