书写格式
命令
用法:
常用参数:
示例:
目录管理命令:mkdir、rmdir
mkdir
用法:创建目录
常用参数:
-p 递归创建目录
-m 设置文件权限 类似 chmod
-v 显示创建过程
-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
示例:
1:创建一个目录MySQL
[root@riyimei tmp]# mkdir mysql
[root@riyimei tmp]#
[root@riyimei tmp]#
[root@riyimei tmp]# mkdir -v mysql1
mkdir: created directory ‘mysql1’
[root@riyimei tmp]#
2:在MySQL底下再创建一个子目录rpm
[root@riyimei tmp]# mkdir -pv /tmp/mysql/rpm
mkdir: created directory ‘/tmp/mysql/rpm’
[root@riyimei tmp]# ls -l /tmp/mysql
total 0
drwxr-x--- 2 root root 279 Jul 25 19:19 rpm
[root@riyimei tmp]#
3:创建一个目录权限为644的目录config
[root@riyimei tmp]# mkdir -pv -m 644 /tmp/mysql/config
mkdir: created directory ‘/tmp/mysql/config’
[root@riyimei tmp]# ls -l /tmp/mysql/config/
total 0
drw-r--r-- 2 root root 6 Jul 25 19:26 config
drwxr-x--- 2 root root 279 Jul 25 19:19 rpm
[root@riyimei tmp]#
rmdir
用法:只能删除空目录
常用参数:
-p 递归删除
-v 显示删除过程
示例:
1:删除一个空目录
[root@riyimei tmp]# rmdir test/
rmdir: failed to remove ‘test/’: Directory not empty
[root@riyimei tmp]#
[root@riyimei tmp]#
[root@riyimei tmp]# rm -rf /tmp/test/liweiming
[root@riyimei tmp]#
[root@riyimei tmp]# rmdir -v test
rmdir: removing directory, ‘test’
[root@riyimei tmp]#
文件管理命令:cp、mv、rm
cp
用法:复制文件或者目录
当目的地为目录里时,将文件复制到目录里面 当目的地为文件时: 1、文件不能为目录 2、只能复制单个文件 文件复制到文件的时候,即是覆盖文件(内容不变)
常用参数:
-p 复制链接文件时 不复制链接文件的源文件
-a 用于归档文件
-L 复制链接文件时,直接复制其源文件,名称为链接名称
-r,R 递归复制
-i 交互式复制
-f
-n
-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
示例:
mv
用法:
常用参数:
示例:
文件查看命令:cat、tac、head、tail、more、less
cat
用法:
常用参数:
示例:
tac
用法:
常用参数:
示例:
head
用法:
常用参数:
示例:
tail
用法:
常用参数:
示例:
move
用法:
常用参数:
示例:
less
用法:
常用参数:
示例:
文件查找:find
find
用法:在目录层级中查找文件
find命令机制是遍历查询(逐个查找)的方法,在各个层级遍历,最终找到目标文件
find命令的优点:查找精确、实时查找
find命令的缺点:消耗资源、查询需要时间很长
常用参数:
格式
find [patch 路径 ] [optins 参数 ] [tests 条件 ] [actions 执行动作]
patch:查找路径 默认为当前路径
options:选择参数
tests:匹配文件条件
actions:查找之后find对查询的结果之后执行的动作
options
-maxdepth levels 查找路径的最大层级深度
-mindepth levels 查找路径的最小层级深度
- example:
查看/tmp目录下的一级子文件
[root@riyimei ~]# find /tmp/ -maxdepth 1
/tmp/
/tmp/.XIM-unix
/tmp/.ICE-unix
/tmp/.Test-unix
/tmp/.X11-unix
/tmp/.font-unix
/tmp/mysql
/tmp/mysql1
[root@riyimei ~]#
显示最大级
[root@riyimei ~]# find /tmp/ -maxdepth 4
/tmp/
/tmp/.XIM-unix
/tmp/.ICE-unix
/tmp/.Test-unix
/tmp/.X11-unix
/tmp/.font-unix
/tmp/mysql
/tmp/mysql/rpm
/tmp/mysql/rpm/mysql-community-client-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-common-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-server-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/config
/tmp/mysql1
[root@riyimei ~]#
tests
文件名
- -iname pattern 忽略大小写
- -name pattern 根据文件名查找
example:
查看/tmp目录下以mysql开头的文件 忽略大小写
[root@riyimei ~]# find /tmp/ -iname mysql*
/tmp/mysql
/tmp/mysql/rpm/mysql-community-client-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-common-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
/tmp/mysql/rpm/mysql-community-server-8.0.21-1.el7.x86_64.rpm
/tmp/mysql1
/tmp/MYSQL
[root@riyimei ~]#
正则表达式
[root@riyimei ~]# find /etc -regex .*ts
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/java/cacerts
/etc/pki/tls/certs
/etc/pki/CA/certs
/etc/pki/CA/newcerts
/etc/openldap/certs
/etc/exports
示例: