1 文件系统结构
FHS(Filesystem Hierarchy Standard:文件系统层次化标准)是大多数Linux版本采用的文件组织形式。Linux中特定的目录存放特定的文件,FHS并不是强制性的。
文件和目录被组织成一个单根倒置的树形结构。从根目录开始,向下级延伸。“/”表示根目录。
tree命令以树状结构列出目录的内容
tree [-acdfghilnpqrstuvxACDFQNSUX] [-L level [-R]] [-H baseHREF] [-T title] [-o filename] [--nolinks] [-P pattern] [-I pattern] [--inodes] [--device] [--noreport] [--dirs‐first] [--version] [--help] [--filelimit #] [--si] [--prune] [--du] [--timefmt format] [--matchdirs] [--fromfile] [--] [directory ...]
tree命令常用选项说明:
选项 | 说明 |
-a | 显示所有文件和目录 |
-A | 使用ASNI绘图字符显示树状图而非以ASCII字符组合 |
-C | 在文件和目录清单上色彩,便于区分各种类型 |
-d | 只显示目录名称 |
-D | 列出文件或目录的更改时间 |
-f | 在每个文件或目录之前,显示完整的相对路径名称 |
-F | 在执行文件,目录,Socket,符号连接,管道名称,各自加上“*”,“/”,“@”,“|”号 |
-g | 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码 |
-i | 不以阶梯状列出文件或目录名称 |
-I pattern | i的大写字母选项表示不显示符合pattern的文件或目录 |
-L | 显示目录树结构的深度 |
-l | 如果遇到性质为符号连接的目录,直接列出该连接所指向的原始目录 |
-n | 不在文件和目录清单加上色彩 |
-N | 直接列出文件和目录名称,包括控制字符 |
-p | 列出权限标示 |
-P pattern | 只显示符合pattern的文件或目录名称 |
-q | 用“?”号取代控制字符,列出文件和目录名称 |
-s | 列出文件或目录大小 |
-t | 用文件和目录的更改时间排序 |
-u | 列出文件或目录的拥有者名称,没有对应名称时,则显示用户识别码 |
-x | 将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排斥在寻找范围外 |
命令tree -f -p -L /etc/查看/etc目录先深度为2级的文件,显示文件的绝对路径及权限
目录名称 | 作用 | |
/boot | 系统启动引导文件存放目录。内核文件(vmlinuxz)以及引导加载器(bootloader)都存放在此目录下 | |
/bin | 存放所有用户使用的基本命令,该目录不能独立分区,OS在启动时会使用到该目录下的文件 | |
/sbin | 存放管理的基本命令,该目录不能独立分区,OS在启动时会使用到该目录下的文件 | |
/lib | 存放系统启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules);还有个目录叫/lib64,他是专用于x86_64系统上的辅助共享库文件存放的位置 | |
/etc | 配置文件目录 | |
/home | 普通用户的家目录 | |
/root | 管理员用户的家目录 | |
/media | 便携式移动设备挂载点 | |
/mnt | 临时文件系统挂载点 | |
/dev | 设备文件及特殊文件存储位置 | |
/opt | 第三方引用程序的安装位置 | |
/srv | 系统上运行的服务用到的数据 | |
/tmp | 临时文件存储位置 |
2 文件类型
Linux中一切皆文件,通过“ls -l”命令查看文件属性时,排在左边第一个字符为文件类型,比如常见的文件类型“-”普通文件以及“d”目录文件。
文件类型 | 标识符 | 说明 |
普通文件 | - | |
目录文件 | d | directory |
符号链接文件 | l | link |
块设备文件 | b | block |
字符设备文件 | c | charecter |
管道文件 | p | pipe |
套接字文件 | s | socket |
普通文件
目录文件
链接文件
管道文件
字符设备文件
块设备文件
套接字文件
3 文件操作常见命令
(1) pwd显示当前工作目录
shell和系统进程默认有一个当前的工作目录 CWD:current work directory
pwd 显示当前工作目录
#常用选项
-P #显示真实物理路径
-L #显示链接路径(默认)
(2)cd 切换目标目录名
使用“.”和“..”表示相对路径,进行切换。cd后不跟目录,默认切换到登录者的家目录
cd后使用"-",表示上一个工作目录。使用“~”切换回家目录,使用“~tom”切换到tom的家目录下.
(3)新建目录 mkdir
mkdir 创建目录
mkdir [OPTION]... DIRECTORY...
常用选项
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
print a message for each created director
(4)删除空目录rmdir
rmdir [OPTION]... DIRECTORY...
常用选项
-p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'
-v, --verbose
output a diagnostic for every directory processed
(5)列出目录内容
ls [OPTION]... [FILE]...
常用选项
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
with -l, print the author of each file
-b, --escape
print C-style escapes for nongraphic characters
--block-size=SIZE
with -l, scale sizes by SIZE when printing them; e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups
do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest
first
-C list entries by columns
--color[=WHEN]
colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below
-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
--file-type
likewise, except do not append '*'
--format=WORD
across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
--full-time
like -l --time-style=full-iso
-g like -l, but do not list owner
--group-directories-first
group directories before files;
can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping
-G, --no-group
in a long listing, don't print group names
-h, --human-readable
with -l and -s, print sizes like 1K 234M 2G etc.
--si likewise, but use powers of 1000 not 1024
-H, --dereference-command-line
follow symbolic links listed on the command line
--dereference-command-line-symlink-to-dir
follow each command line symbolic link
that points to a directory
--hide=PATTERN
do not list implied entries matching shell PATTERN (overridden by -a or -A)
--hyperlink[=WHEN]
hyperlink file names; WHEN can be 'always' (default if omitted), 'auto', or 'never'
--indicator-style=WORD
append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F)
-i, --inode
print the index number of each file
-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN
-k, --kibibytes
default to 1024-byte blocks for disk usage; used only with -s and per directory totals
-l use a long listing format
-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself
-m fill width with a comma separated list of entries
-n, --numeric-uid-gid
like -l, but list numeric user and group IDs
-N, --literal
print entry names without quoting
-o like -l, but do not list group information
-p, --indicator-style=slash
append / indicator to directories
-q, --hide-control-chars
print ? instead of nongraphic characters
--show-control-chars
show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal)
-Q, --quote-name
enclose entry names in double quotes
--quoting-style=WORD
use quoting style WORD for entry names: literal, locale, shell, shell-always, shell-escape, shell-escape-always, c, escape (overrides QUOTING_STYLE environment vari‐
able)
-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, largest first
--sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
--time=WORD
change the default of using modification times; access time (-u): atime, access, use; change time (-c): ctime, status; birth time: birth, creation;
with -l, WORD determines which time to show; with --sort=time, sort by WORD (newest first)
--time-style=TIME_STYLE
time/date format with -l; see TIME_STYLE below
-t sort by time, newest first; see --time
-T, --tabsize=COLS
assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-w, --width=COLS
set output width to COLS. 0 means no limit
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-Z, --context
print any security context of each file
-1 list one file per line. Avoid '\n' with -q or -b
以人易懂的方式显示文件目录大小
查看文件的inode号
按时间从新到旧的顺序显示
递归显示目录
显示不同类型的文件并添加对应的标识
(6) 文件的时间属性
一个文件可分为两部分:
一部分为文件的内容,即文件的数据(data)。通过cat命令查看到的内容存储在硬盘的数据空间(data block)中。
另一部分是文件的属性,即文件的元数据(meta data),比如:文件类型,大小,节点号(inode number),权限,所有者,所属组,时间等。这部分内容存放在硬盘中专门的节点空间(inode block)。
每一个文件具有三种时间属性:atime,ctime,mtime
atime:access time,记录最后一次的访问时间
mtime:modify time,记录最后一次文件数据部分的修改时间
ctime:change time,记录最后一次文件元数据的修改时间
centos6开始默认开启了一个“relatime”的挂载选项,此选项要求文件的atime只有满足两个条件之一才会更新。
条件1:文件的atime时间超过一天以上
条件2:文件的mtime比atime更晚
查看文件属性信息命令
① stat 查看文件类型
stat [OPTION]... FILE...
常用选项
-c --format=FORMAT
use the specified FORMAT instead of the default; output a newline after each use of FORMAT
-f, --file-system
display file system status instead of file status
-t, --terse
print the information in terse form
② file确定文件的类型
file [-bcdEhiklLNnprsSvzZ0] [--apple] [--exclude-quiet] [--extension] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] [-P name=value] file ...
file -C [-m magicfiles]
常用选项
-b, --brief
Do not prepend filenames to output lines (brief mode).
-F, --separator separator
Use the specified string as the separator between the filename and the file result returned. Defaults to ‘:’.
-f, --files-from namefile
Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or at least one filename argument must be present;
to test the standard input, use ‘-’ as a filename argument. Please note that namefile is unwrapped and the enclosed filenames are processed when this option is en‐
countered and before any further options processing is done. This allows one to process multiple lists of files with different command line arguments on the same file
invocation. Thus if you want to set the delimiter, you need to do it before you specify the list of files, like: “-F @ -f namefile”, instead of: “-f namefile -F @”.
-L, --dereference
This option causes symlinks to be followed, as the like-named option in ls(1) (on systems that support symbolic links). This is the default if the environment vari‐
able POSIXLY_CORRECT is defined.
-i, --mime
Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say ‘text/plain; charset=us-ascii’ rather than
“ASCII text”.
示例
(7) 创建文件
touch创建普通文件,修改文件的access 和修改时间
touch [OPTION]... FILE...
DESCRIPTION Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h is supplied.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create
do not create any files
-d, --date=STRING
parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference
affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE
use this file's times instead of current time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD
change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m
格式[[CC]YY]MMDDhhmm[.ss]
CC 表示两位数字的世纪,
YY表示两位数字的年份
MM表示月份
DD表示一个月中第几天
hh每天的几点钟
mm表示分钟
SS表示第几秒
创建新文件并刷新文件的时间戳
(8)拷贝文件或目录cp
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive
same as -dR --preserve=all
--attributes-only
don't copy the file data, just the attributes
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents
copy contents of special files when recursive
-d same as --no-dereference --preserve=links
-f, --force
if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
-i, --interactive
prompt before overwrite (overrides a previous -n option)
-H follow command-line symbolic links in SOURCE
-l, --link
hard link files instead of copying
-L, --dereference
always follow symbolic links in SOURCE
-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)
-P, --no-dereference
never follow symbolic links in SOURCE
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
-c deprecated, same as --preserve=context
--no-preserve=ATTR_LIST
don't preserve the specified attributes
--parents
use full source file name under DIRECTORY
-R, -r, --recursive
copy directories recursively
--reflink[=WHEN]
control clone/CoW copies. See below
--remove-destination
remove each existing destination file before attempting to open it (contrast with --force)
--sparse=WHEN
control creation of sparse files. See below
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-s, --symbolic-link
make symbolic links instead of copying
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-x, --one-file-system
stay on this file system
-Z set SELinux security context of destination file to default type
--context[=CTX]
like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
--help display this help and exit
--version
output version information and exit
源文件 | 目标文件不存在 | 目标文件存在且为文件 | 目标文件存在且为目录 |
一个文件 | 会创建DEST文件, 再将SRC文件内容 复制到DEST中 | 将SRC中的内容覆盖至 DEST中,目标文件中的原 始数据将会丢失。建议复 制时使用-i选项 | 在该目录下创建与原文件 同名的文件,并将SRC文件 中的内容复制到DEST中 |
多个文件 | 提示错误 | 提示错误 | 在该目录下分别创建与原 文件同名的文件,并将SRC 文件中的内容一一复制到 对应的DEST中 |
目录文件 | 创建指定DEST同名目 录,复制SRC目录中所 有文件至DEST下 | 提示错误 | 在DEST下创建于原目录同 名的目录,并将SRC中的内 容复制到新目录中 |
示例
复制整个目录到目标目录下,cp -a复制目录(包含递归复制,并且保留属主属组)
复制目录时需要加-r选项递归复制目录,
cp -b如果目标文件存在时,在目标文件所在的目录会备份一份文件。然后再进行新的文件生成。
复制备份而不覆盖 --backup=numbered
(9)移动或重命名文件 mv
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force
do not prompt before overwriting
-i, --interactive
prompt before overwrite
-n, --no-clobber
do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-Z, --context
set SELinux security context of destination file to default type
示例
(10)删除文件或目录rm
rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).
-f, --force
ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
--interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
--one-file-system
when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
--no-preserve-root
do not treat '/' specially
--preserve-root[=all]
do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent
-r, -R, --recursive
remove directories and their contents recursively
-d, --dir
remove empty directories
-v, --verbose
explain what is being done
--help display this help and exit
--version
output version information and exit
rm默认只删除文件不删除目录
不使用-f强制删除选项在rockey中删除文件夹会提示,带有“-”的文件rm不能处理需要特殊处理。
在ubuntu中删除是不进行提示的,必须谨慎使用rm命令
(11)创建链接ln
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...
DESCRIPTION In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each destination (name of new link) should not already exist. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-d, -F, --directory
allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)
-d, -F, --directory
allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)
-f, --force
remove existing destination files
-i, --interactive
prompt whether to remove destinations
-L, --logical
dereference TARGETs that are symbolic links
-n, --no-dereference
treat LINK_NAME as a normal file if it is a symbolic link to a directory
-P, --physical
make hard links directly to symbolic links
-r, --relative
create symbolic links relative to link location
-s, --symbolic
make symbolic links instead of hard links
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
specify the DIRECTORY in which to create the links
-T, --no-target-directory
treat LINK_NAME as a normal file always
-v, --verbose
print name of each linked file
①创建硬链接
硬链接inode号对应多个文件名,一个文件被多个别名文件引用了多次。
硬链接特点:
- 文件有相同的inode及data block
- 只能对已存在的文件进行创建
- 不能跨分区创建硬链接
- 删除一个硬连接文件并不影响其他有相同ionde号的文件,直到没有文件引用该inode号文件最终会被删除
- 不能对目录创建硬链接
- 创建链接后链接数会增加
②创建软链接
软链接就是一个普通文件,有自己inode号以及用户数据块存储的内容。
- 软链接有自己的文件属性及权限等
- 可对不存在的文件或目录创建软链接
- 软链接可以跨分区创建
- 软链接可以对文件或目录创建
- 创建软链接时,链接计数不会增加
- 删除链接并不影响被指向的文件,但是指向的文件被删除,则相关的软链接被称为死链接,若被指向路径文件被重新创建,死链接可恢复为正常的链接