1. mkdir :创建目录
用法:mkdir [选项]... 目录...
若指定目录不存在则创建目录长选项必须使用的参数对于短选项时也是必需使用的。
-m, --mode=模式 设置权限模式(类似chmod),而不是rwxrwxrwx 减umask
-p, --parents 需要时创建目标目录的上层目录,但即使这些目录已存在也不当作错误处理
-v, --verbose 每次创建新目录都显示信息
-Z, --context=CTX 将每个创建的目录的SELinux 安全环境设置为CTX
[root@root tree]# mkdir -pv mylinux/{bin,conf,bib,logs,webapps/{docs,examples},work}
mkdir: 已创建目录 "mylinux"
mkdir: 已创建目录 "mylinux/bin"
mkdir: 已创建目录 "mylinux/conf"
mkdir: 已创建目录 "mylinux/bib"
mkdir: 已创建目录 "mylinux/logs"
mkdir: 已创建目录 "mylinux/webapps"
mkdir: 已创建目录 "mylinux/webapps/docs"
mkdir: 已创建目录 "mylinux/webapps/examples"
mkdir: 已创建目录 "mylinux/work"
[root@root tree]# tree
.
└── mylinux
├── bib
├── bin
├── conf
├── logs
├── webapps
│ ├── docs
│ └── examples
└── work
9 directories, 0 files
2. 显示文件内容
- more:逐屏显示 (适合文件内容较多的文件,一屏幕不能显示完整)
- less :逐屏显示 (适合文件内容较多的文件,一屏幕不能显示完整)
快捷键 | 作用 |
空格 | 上翻页 |
B (back) | 下翻页 |
+/ | 搜索 |
q | 退出 |
- cat :一次性显示完整的文件,适合文件内容较少的文件
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。-A, --show-all 等于-vET
-b, --number-nonblank 对非空输出行编号
-e 等于-vE
-E, --show-ends 在每行结束处显示"$"
-n, --number 对输出的所有行编号
-s, --squeeze-blank 不输出多行空行
-t 与-vT 等价
-T, --show-tabs 将跳格字符显示为^I
-u (被忽略)
-v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
[root@root ~]# cat file
hello
work
hi
how
are
you
[root@root ~]# cat -n file
1 hello
2 work
3 hi
4 how
5 are
6 you
7
[root@root ~]# cat -b file
1 hello
2 work
3 hi
4 how
5 are
6 you
[root@root ~]# cat -A file
hello $
work$
hi$
how $
are$
you$
$
[root@root ~]# cat -nA file
1 hello $
2 work$
3 hi$
4 how $
5 are$
6 you$
7 $
3.diff :逐行比较文件
用法: diff [OPTION]... FILES
Compare files line by line.-i :--ignore-case忽略大小写比较
-y :并列比较文件
-W :设置宽度比较文件和-y 一块使用。
[root@root tmp]# diff -y file file1
aBc123 3 | abc123 3
cds968 | asd669
oopp oopp
Qwe | qWE
[root@root tmp]# diff -i file file1
1,2c1,2
< aBc123 3
< cds968
---
> abc123 3
> asd669
[root@root tmp]# diff -yW 40 file file1
aBc123 3 | abc123 3
cds968 | asd669
oopp oopp
Qwe | qWE
4. rm :删除
用法:rm [选项]... 文件...
删除 (unlink) 文件。-f, --force 强制删除。忽略不存在的文件,不提示确认
-i 在删除前需要确认(对普通用户专用,普通用户删除若不加-i,则不做提示直接删除)
-I 在删除超过三个文件或者递归删除前要求确认。此选项比-i 提
示内容更少,但同样可以阻止大多数错误发生-r, -R, --recursive 递归删除目录及其内容
-v, --verbose 详细显示进行的步骤
root用户
[root@root tmp]# ls
file file1
[root@root tmp]# rm file
rm:是否删除普通文件 "file"?y
[root@root tmp]# ls
file1
[root@root tmp]# rm -f file1
[root@root tmp]# ls
[root@root tmp]#
[root@root tmp]# mkdir car/
[root@root tmp]# ls
car file file1
[root@root tmp]# cd car/
[root@root car]# touch file2
[root@root car]# ls
file2
[root@root car]# cd ..
[root@root tmp]# rm -r car/
rm:是否进入目录"car"? y
rm:是否删除普通空文件 "car/file2"?y
rm:是否删除目录 "car"?y
[root@root tmp]#
[root@root tmp]# mkdir car/
[root@root tmp]# cd car/
[root@root car]# touch file2
[root@root car]# ls
file2
[root@root car]# cd ..
[root@root tmp]# rm -rf car/
[root@root tmp]# ls
file file1
普通用户
[host@root ~]$ touch host1 host2
[host@root ~]$ mkdir host/
[host@root ~]$ ls
host host1 host2
[host@root ~]$ rm host1
[host@root ~]$ rm -i host2
rm:是否删除普通空文件 "host2"?y
[host@root ~]$ ls
host
[host@root ~]$ rm -rf host/
[host@root ~]$ ls
[host@root ~]$ touch host1 host2
[host@root ~]$ rm -iv host1
rm:是否删除普通空文件 "host1"?y
已删除"host1"
[host@root ~]$
5. touch :将每个文件的访问时间和修改时间改为当前时间,或创建文件
用法:touch [选项]... 文件...
将每个文件的访问时间和修改时间改为当前时间。不存在的文件将会被创建为空文件,除非使用-c 或-h 选项。
如果文件名为"-"则特殊处理,更改与标准输出相关的文件的访问时间。
长选项必须使用的参数对于短选项时也是必需使用的。
-a 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地
(当系统支持更改符号链接的所有者时,此选项才有用)
-m 只更改修改时间
-r, --reference=文件 使用指定文件的时间属性而非当前时间
-t STAMP 使用[[CC]YY]MMDDhhmm[.ss] 格式的时间而非当前时间
--time=WORD 使用WORD 指定的时间:access、atime、use 都等于-a
选项的效果,而modify、mtime 等于-m 选项的效果
[root@root tmp]# date
2020年 03月 16日 星期一 22:57:22 CST
[root@root tmp]# stat file
File: "file"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 524291 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:44:53.224826548 +0800
Modify: 2020-03-16 22:37:53.844825598 +0800
Change: 2020-03-16 22:37:53.844825598 +0800
-a :修改Access时间
[root@root tmp]# touch -a file
[root@root tmp]# stat file
File: "file"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 524291 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:59:10.258823384 +0800
Modify: 2020-03-16 22:37:53.844825598 +0800
Change: 2020-03-16 22:59:10.258823384 +0800
-m :修改Modify时间
[root@root tmp]# touch -m file
[root@root tmp]# stat file
File: "file"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 524291 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:59:10.258823384 +0800
Modify: 2020-03-16 22:59:28.321825585 +0800
Change: 2020-03-16 22:59:28.321825585 +0800
-c:不创建不存在的文件
[root@root tmp]# touch -c hhh
[root@root tmp]# ls
file file1
-r:通过一个文件更新另一个文件的时间
[root@root tmp]# stat file file1
File: "file"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 524291 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:59:10.258823384 +0800
Modify: 2020-03-16 22:59:28.321825585 +0800
Change: 2020-03-16 22:59:28.321825585 +0800
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 535485 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:37:53.844825598 +0800
Modify: 2020-03-16 22:37:53.844825598 +0800
Change: 2020-03-16 22:37:53.844825598 +0800
[root@root tmp]# touch -r file file1
[root@root tmp]# stat file file1
File: "file"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 524291 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:59:10.258823384 +0800
Modify: 2020-03-16 22:59:28.321825585 +0800
Change: 2020-03-16 22:59:28.321825585 +0800
File: "file1"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: 802h/2050d Inode: 535485 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-16 22:59:10.258823384 +0800
Modify: 2020-03-16 22:59:28.321825585 +0800
Change: 2020-03-16 23:03:55.735823612 +0800
6. ln :创建链接文件
硬链接:允许一个文件拥有多个有效的路径名。 可以通过设置硬链接来防止重要的文件被误删。
软连接(符号链接):类似于windows上面的快捷方式,记录的是一个另一个文件的位置信息。软连接上存在着主从之分,主文件删除,无法从软连接访问。
如上图,file1 ,file2文件分别都记录了 file文件 的inode索引节点号等信息。删除file1,file2其中的任何一个,file文件并没有被删除。
如上图:file2为file的软连接。记录着file1的路径。访问file2相当于从file1中访问inode节点号,从而访问file文件。file1和file2文件有主从之分。若file1被删除,file文件被删除,磁盘此位置被清空,file2并不能继续访问file文件。
-s :创建符号链接
[root@root tmp]# ls
file file1
[root@root tmp]# rm -rf *
[root@root tmp]# ls
[root@root tmp]# touch file
[root@root tmp]# ln file file_hard
[root@root tmp]# ln -s file file_sym
[root@root tmp]# ls
file file_hard file_sym
查看三个文件信息,file_sym中,lrw---中的“l”代表符号链接,-> file表示此文件指向file文件。
[root@root tmp]# ls -li
总用量 0
524291 -rw-r--r--. 2 root root 0 3月 17 02:29 file
524291 -rw-r--r--. 2 root root 0 3月 17 02:29 file_hard
535485 lrwxrwxrwx. 1 root root 4 3月 17 02:30 file_sym -> file
[root@root tmp]# echo hello > file
[root@root tmp]# cat file
hello
[root@root tmp]# cat file_hard
hello
[root@root tmp]# cat file_sym
hello
[root@root tmp]# rm -rf file
[root@root tmp]# cat file
cat: file: 没有那个文件或目录
[root@root tmp]# cat file_hard
hello
[root@root tmp]# cat file_sym
cat: file_sym: 没有那个文件或目录
[root@root tmp]# ls
file_hard file_sym
7. file :查看文件的类型
用法: file [OPTION...] [FILE...]
-b : 不现实文件名,只显示结果。
-f :列出文件中的文件的文件类型
-F : 制定符号替换输出结果 文件后的默认“:”
-i:显示文件的格式
[root@root tmp]# touch file
[root@root tmp]# ls
file
[root@root tmp]# file file
file: empty
[root@root tmp]# echo hello > file
[root@root tmp]# cat file
hello
[root@root tmp]# file file
file: ASCII text
[root@root tmp]# file -b file
ASCII text
-f :file -f 后面跟的文件里面有其他文件时,才可以用,并且文件中不能为空
[root@root tmp]# ls
file
[root@root tmp]# touch file1
[root@root tmp]# pwd
/tmp
[root@root tmp]# echo "/tmp/file1" > file
[root@root tmp]# file -f file
/tmp/file1: empty
[root@root tmp]# echo "how are you" > file1
[root@root tmp]# file -f file
/tmp/file1: ASCII text
-F :替换上述中的 /tmp/file1 :中的“:”
[root@root tmp]# file -F "-*-" -f file
/tmp/file1-*- ASCII text
-i :显示文件的格式。
[root@root tmp]# file -i file
file: text/plain; charset=us-ascii
8. cp :copy 复制文件或目录
用法:cp [选项]... [-T] 源文件 目标文件
或:cp [选项]... 源文件... 目录
或:cp [选项]... -t 目录 源文件...
将源文件复制至目标文件,或将多个源文件复制至目标目录。
-a, --archive 等于-dR --preserve=all
--backup[=CONTROL 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
--copy-contents 在递归处理是复制特殊文件内容
-d 等于--no-dereference --preserve=links,复制链接文件,并非源文件
-f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项
存在时则不需再选此项)
-i, --interactive 覆盖前询问(使前面的 -n 选项失效)
-H 跟随源文件中的命令行符号链接
-l, --link 链接文件而不复制
-L, --dereference 总是跟随符号链接
-n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)
-P, --no-dereference 不跟随源文件中的符号链接
-p 等于--preserve=模式,所有权,时间戳
--preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果
可能保持附加属性:环境、链接、xattr 等
-c same as --preserve=context
--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 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, --context=CONTEXT set security context of copy to CONTEXT
[root@root tmp]# ls
file
[root@root tmp]# ls
file file1
[root@root tmp]# cp /tmp/file file1
cp:是否覆盖"file1"? y
[root@root tmp]# diff file file1
[root@root tmp]# ln -s file file_sym
[root@root tmp]# cp file_sym file2
[root@root tmp]# ls
file file1 file2 file_sym
[root@root tmp]# ls -li
总用量 12
524291 -rw-r--r--. 1 root root 6 3月 17 04:00 file
535485 -rw-r--r--. 1 root root 6 3月 17 04:13 file1
535793 -rw-r--r--. 1 root root 6 3月 17 04:16 file2
535624 lrwxrwxrwx. 1 root root 4 3月 17 04:15 file_sym -> file
[root@root tmp]# echo hahaha > file
[root@root tmp]# diff file file_sym
[root@root tmp]# diff -y file file1
hahaha | hello
-b :复制符号链接文件,非源文件
[root@root tmp]# cp -d file_sym file3
[root@root tmp]# diff -y file file3
hehahehi hehahehi
[root@root tmp]# echo hahah > file
[root@root tmp]# diff -y file file3
hahah hahah
-p :保留原文件属性
[root@root tmp]# cp -p file file4
[root@root tmp]# ls -l file file4
-rw-r--r--. 1 root root 6 3月 17 04:25 file
-rw-r--r--. 1 root root 6 3月 17 04:25 file4
--parents :保留源文件的路径
[root@root tmp1]# tree
.
└── test
└── file
1 directory, 1 file
[root@root tmp1]# ls /mnt
[root@root tmp1]# cp --parents /tmp1/test/file /mnt
[root@root tmp1]# tree /mnt
/mnt
└── tmp1
└── test
└── file
2 directories, 1 file
9. find :查找指定路径下的指定文件
find [path...] -options [expression] [-print -exec -ok]
path :指要查找的目录路径
~
.
/
print :指查找的结果输出到标准输出
exec :可以对查找到的文件执行, 该参数给出的shell命令
command { } \ ;
options: -name 按名字查找
-type :按类型查找
-user :按用户查找
-group :按组查找
-perm :按权限查找
[root@root tmp]# touch file1
[root@root tmp]# find / -name file1
/tmp/file1
[root@root tmp]# ls
file1
[root@root tmp]# find / -name file1 -exec rm -rf {} \;
[root@root tmp]# ls
[root@root tmp]# touch file1
[root@root tmp]# find / -name file1 -exec cp -rf {} /mnt \;
cp: "/mnt/file1" 与"/mnt/file1" 为同一文件
[root@root tmp]# ls /mnt/
file1
[root@root tmp]# ln -s file file1
[root@root tmp]# ls
file file1
[root@root tmp]# find /tmp -type l -exec cp -r {} /mnt \;
[root@root tmp]# ls /mnt
file1
10. mv :更改文件或目录的存储位置,或重命名
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
-b :当目标文件存在时,先进行备份在覆盖-f :强制覆盖
-i :交互式覆盖,提醒
重命名,在本地目录下 即重命名
[root@root tmp]# ls
file1
[root@root tmp]# mv file1 file
[root@root tmp]# ls
file
移动文件
[root@root tmp]# mkdir you
[root@root tmp]# ls
file file1 you
[root@root tmp]# mv file you/
[root@root tmp]# ls
file1 you
[root@root tmp]# ls you/
file
[root@root tmp]# mv -t you/ file1
[root@root tmp]# ls you/
file file1
[root@root tmp]# ls
you
-b :先备份在覆盖
[root@root tmp]# ls
you
[root@root tmp]# ls you/
file file1
[root@root tmp]# touch file
[root@root tmp]# mv -b file you/
mv:是否覆盖"you/file"? y
[root@root tmp]# ls
you
[root@root tmp]# ls you/
file file~ file1
-f :强制覆盖
[root@root tmp]# ls
file you
[root@root tmp]# ls you/
file file1
[root@root tmp]# mv -f file you/
[root@root tmp]# ls
you
[root@root tmp]# ls you/
file file1
-i :交互式覆盖,出现提醒
[root@root tmp]# ls
file you
[root@root tmp]# ls you/
file file1
[root@root tmp]# mv -i file you/
mv:是否覆盖"you/file"? y
[root@root tmp]# ls
you
[root@root tmp]# ls you/
file file1
10. split :分割文件
用法:split [选项]... [输入 [前缀]]
将输入内容拆分为固定大小的分片并输出到"前缀aa"、"前缀ab",...;-a, --suffix-length=N 指定后缀长度为N (默认为2)
-b, --bytes=大小 指定每个输出文件的字节大小
-C, --line-bytes=大小 指定每个输出文件里最大行字节大小
-d, --numeric-suffixes 使用数字后缀代替字母后缀
-l, --lines=数值 指定每个输出文件有多少行
[root@root tmp]# dd if=/dev/zero of=data bs=100k count=1
记录了1+0 的读入
记录了1+0 的写出
102400字节(102 kB)已复制,0.000375069 秒,273 MB/秒
[root@root tmp]# ls
data you
[root@root tmp]# ls -ln data
-rw-r--r--. 1 0 0 102400 3月 17 06:53 data
[root@root tmp]# split -b 25k data
[root@root tmp]# ls
data xaa xab xac xad you
[root@root tmp]# split -b 25k -da 3 data
[root@root tmp]# ls
data x000 x001 x002 x003 xaa xab xac xad you
[root@root tmp]#