文件管理
linux文件管理主要包括目录管理和文件管理,其中目录管理包括查看目录文件名、查看当前目录、切换目录、创建目录、删除目录、复制目录、移动目录等,文件管理包括文件内容查看、文件属性修改、删除文件等。
常用命令
1、查看目录文件
用法:ls [选项] [文件]
选项:
-a, --all 不隐藏任何以. 开始的项目
-A, --almost-all 列出除. 及.. 以外的任何项目
--author 与-l 同时使用时列出每个文件的作者
-b, --escape 以八进制溢出序列表示不可打印的字符
--block-size=SIZE 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 ~
-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 类似-l,但不列出所有者
--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/or -s, print human readable sizes
(e.g., 1K 234M 2G)
--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)
--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
-l 使用较长格式列出信息
-L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示
的对象而并非符号链接本身的信息
-m 所有项目以逗号分隔,并填满整行行宽
-n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号
-N, --literal 输出未经处理的项目名称 (如不特别处理控制字符)
-o 类似 -l,但不列出有关组的信息
-p, --indicator-style=slash 对目录加上表示符号"/"
-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
-r, --reverse 逆序排列
-R, --recursive 递归显示子目录
-s, --size 以块数形式显示每个文件分配的尺寸
-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 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 (newest first)
--time-style=STYLE with -l, show times using style STYLE:
full-iso, long-iso, iso, locale, or +FORMAT;
FORMAT is interpreted like in 'date'; if FORMAT
is FORMAT1<newline>FORMAT2, then FORMAT1 applies
to non-recent files and FORMAT2 to recent files;
if STYLE is prefixed with 'posix-', STYLE
takes effect only outside the POSIX locale
-t sort by modification time, newest first
-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 with -q or -b
常用的选项有-a -d -h -l,可以单独使用,也可以组合一起使用。
shiyanlou:~/ $ ls -a /home [19:05:59]
. .. labex project shiyanlou
shiyanlou:~/ $ ls -l /home [19:06:21]
总用量 12
drwxr-xr-x 10 labex labex 4096 1月 6 2021 labex
drwxrwxr-x 2 root public 4096 1月 6 2021 project
drwxr-xr-x 16 shiyanlou shiyanlou 4096 6月 19 18:46 shiyanlou
shiyanlou:~/ $ ls -al /home [19:06:35]
总用量 20
drwxr-xr-x 5 root root 4096 1月 6 2021 .
drwxr-xr-x 22 root root 4096 6月 19 18:45 ..
drwxr-xr-x 10 labex labex 4096 1月 6 2021 labex
drwxrwxr-x 2 root public 4096 1月 6 2021 project
drwxr-xr-x 16 shiyanlou shiyanlou 4096 6月 19 18:46 shiyanlou
shiyanlou:~/ $ ls -alh /home [19:07:23]
总用量 20K
drwxr-xr-x 5 root root 4.0K 1月 6 2021 .
drwxr-xr-x 22 root root 4.0K 6月 19 18:45 ..
drwxr-xr-x 10 labex labex 4.0K 1月 6 2021 labex
drwxrwxr-x 2 root public 4.0K 1月 6 2021 project
drwxr-xr-x 16 shiyanlou shiyanlou 4.0K 6月 19 18:46 shiyanlou
2、查看当前目录
pwd [-P]
显示目前所在的目录。
shiyanlou:~/ $ pwd -P [19:16:43]
/home/shiyanlou
shiyanlou:~/ $ pwd [19:16:56]
/home/shiyanlou
3、切换目录
cd [相对路径或绝对路径]
绝对路径:
路径的写法,由根目录 / 写起,例如: /usr/share/doc 这个目录。
相对路径:
路径的写法,不是由 / 写起,例如由 /usr/share/doc 要到 /usr/share/man 底下时,可以写成: cd …/man 这就是相对路径的写法。
4、创建目录
mkdir [选项] 目录
选项:
-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 directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
创建test目录
mkdir test
创建多层目录,使用-p选项
mkdir -p test/test1/test2
创建test目录,并使用配置属性
mkdir -m 711 test
5、删除目录
用法:rmdir [选项] 目录
选项:
--ignore-fail-on-non-empty
忽略仅由目录非空产生的所有错误
-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
删除空目录,空目录指目录下面不再有目录和文件,如果不是空目录则会报错。
rmdir test
删除多个空目录。
rmdir -p test/test1/test2
用法:rm [选项] 文件
选项:
-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 递归删除一个层级时,跳过所有不符合命令行参
数的文件系统上的文件
--no-preserve-root do not treat '/' specially
--preserve-root do not remove '/' (default)
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
递归删除test目录及下面所有目录文件
rm -r test
6、复制目录
用法:cp [选项] [-T] 源文件 目标文件
或:cp [选项] 源文件 目录
或:cp [选项] -t 目录 源文件
选项:
-a, --archive 等于-dR --preserve=all
--attributes-only 仅复制属性而不复制数据 --backup[=CONTROL 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
--copy-contents 在递归处理是复制特殊文件内容
-d 等于--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 不要覆盖已存在的文件(使前面的 -i 选项失效)
-P, --no-dereference 不跟随源文件中的符号链接
-p 等于--preserve=模式,所有权,时间戳
--preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果
可能保持附加属性:环境、链接、xattr 等
--sno-preserve=属性列表 不保留指定的文件属性
--parents 复制前在目标目录创建来源文件路径中的所有目录
-R, -r, --recursive 递归复制目录及其子目录内的所有内容
--reflink[=WHEN] 控制克隆/CoW 副本。请查看下面的内如。
--remove-destination 尝试打开目标文件前先删除已存在的目的地
文件 (相对于 --force 选项)
--sparse=WHEN 控制创建稀疏文件的方式
--strip-trailing-slashes 删除参数中所有源文件/目录末端的斜杠
-s, --symbolic-link 只创建符号链接而不复制文件
-S, --suffix=后缀 自行指定备份文件的后缀
-t, --target-directory=目录 将所有参数指定的源文件/目录
复制至目标目录
-T, --no-target-directory 将目标目录视作普通文件
-u, --update 只在源文件比目标文件新,或目标文件
不存在时才进行复制
-v, --verbose 显示详细的进行步骤
-x, --one-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
cp命令参数比较多,常用的参数有-a -i -p -r。
复制test1目录到test2目录下
cp -r ./test1 ./test2
7、移动目录
用法:mv [选项] [-T] 源文件 目标文件
或:mv [选项] 源文件 目录
或:mv [选项] -t 目录 源文件
选项:
--backup[=CONTROL] 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
-f, --force 覆盖前不询问
-i, --interactive 覆盖前询问
-n, --no-clobber 不覆盖已存在文件
如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
--strip-trailing-slashes 去掉每个源文件参数尾部的斜线
-S, --suffix=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
移动test目录到test1目录下面
mv ./test ./test1
移动test目录到test1目录下,如果覆盖不询问
mv -f ./test ./test1
移动test目录到test1目录下,覆盖时询问
mv -i /test ./test1
修改文件名test1为test2
mv test1 test2
8、文件查看
1.由第一行开始显示文件内容
用法:cat [选项] 文件
选项:
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t 与-vT 等价
-T, --show-tabs 将跳格字符显示为^I
-u (被忽略)
-v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
查看文件的内容。
shiyanlou:~/ $ cat test [20:48:18]
this is a line.
this is a new line.
查看文件的内容,相当于使用参数-vET。
shiyanlou:~/ $ cat -A test [20:54:18]
this is a line.$
this is a new line.$
2.显示的时候,顺道输出行号
用法:nl [选项]... [文件]...
Write each FILE to standard output, with line numbers added.
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
-b, --body-numbering=样式 使用指定样式编号文件的正文行目
-d, --section-delimiter=CC 使用指定的CC 分割逻辑页数
-f, --footer-numbering=样式 使用指定样式编号文件的页脚行目
-h, --header-numbering=样式 使用指定样式编号文件的页眉行目
-i, --page-increment=数值 设置每一行遍历后的自动递增值
-l, --join-blank-lines=数值 设置数值为多少的若干空行被视作一行
-n, --number-format=格式 根据指定格式插入行号
-p, --no-renumber 在逻辑页数切换时不将行号值复位
-s, --number-separator=字符串 可能的话在行号后添加字符串
-v, --starting-line-number=数字 每个逻辑页上的第一行的行号
-w, --number-width=数字 为行号使用指定的栏数
--help 显示此帮助信息并退出
--version 显示版本信息并退出
默认的选项设置是-v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn。CC 是用于分隔
逻辑页数的两个分界符,其中缺失的第二个字符暗含了":",如果您要指定"",
请输入""。可用的样式如下:
a 对所有行编号
t 对非空行编号
n 不编行号
pBRE 只对符合正则表达式BRE 的行编号
FORMAT 是下列之一:
ln 左对齐,空格不用0 填充
rn 右对齐,空格不用0 填充
rz 右对齐,空格用0 填充
查看test文件的内容,并输出序号
shiyanlou:~/ $ nl test [21:44:53]
1 this is a line.
2 this is a new line.
还有两个不常用的命令,用于查看文件的头几行和末尾几行。
head -n num 文件
tail -n num 文件
9、文件创建
用法:touch [选项] 文件
选项:
-a 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地
(当系统支持更改符号链接的所有者时,此选项才有用)
-m 只更改修改时间
-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
新建一个文件test.txt
touch test.txt
修改文件的访问时间
touch -t 12121212 test.txt
10、文件修改
1.修改文件所属的用户和组
用法:chown [选项] [所有者][:[组]] 文件
或:chown [选项] --reference=参考文件 文件
选项:
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)
--from=当前所有者:当前所属组
只当每个文件的所有者和组符合选项所指定时才更改所
有者和组。其中一个可以省略,这时已省略的属性就不
需要符合原有的属性。
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's owner and group rather than
specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively
The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one takes effect.
-H if a command line argument is a symbolic link
to a directory, traverse it
-L traverse every symbolic link to a directory
encountered
-P do not traverse any symbolic links (default)
更改文件test所属的用户为root,需要使用特权命令sudo。
sudo chown root test
更改文件test所属的用户:组为root:root。
sudo chown root:root test
2.修改文件的9个基本属性
用法:chmod [选项] 模式[,模式] 文件
或:chmod [选项] 八进制模式 文件
或:chmod [选项] --reference=参考文件 文件
选项:
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively

文件的基本权限就有九个,分别是 owner/group/others(拥有者/组/其他) 三种身份各有自己的 read/write/execute 权限。
文件的权限字符为: -rwxrwxrwx , 这九个权限是三个三个一组的。

修改test文件用户权限为rwx,所属用户组权限为rx,其他权限为r。
chmod u=rwx,g=rx,o=r test
去掉test文件全部用户的x权限
chmod a-x test
命令总结
文件可以看成一种数据结构,对于数据结构的操作可以归结为增删改查。因此,对于文件的操作,也主要分为增删改查四个操作。下面是对于文件操作命令的总结:
| 命令 | 说明 |
|---|---|
| mkdir | 创建新空目录 |
| rmdir | 删除目录 |
| rm | 删除目录和文件 |
| cp | 复制目录或文件 |
| mv | 移动目录或文件,修改文件名称 |
| cd | 切换目录 |
| ls | 查看指定目录 |
| pwd | 查看当前目录 |
| touch | 创建新文件或修改文件时间戳 |
| chown | 变更文件所有者 |
| chmod | 修改文件权限 |
| cat、tac、nl、more、less、head、tail | 查看文件内容 |
| file | 查看文件类型 |
| vim | 进行文件编辑 |
733

被折叠的 条评论
为什么被折叠?



