书写格式
命令
用法:
常用参数:
示例:

目录管理命令:mkdir、rmdir

mkdir

用法:创建目录
常用参数:
-p 递归创建目录
-m 设置文件权限 类似 chmod
-v 显示创建过程

  1. -m, --mode=MODE
  2. set file mode (as in chmod), not a=rwx - umask
  3. -p, --parents
  4. no error if existing, make parent directories as needed
  5. -v, --verbose
  6. print a message for each created directory

示例:
1:创建一个目录MySQL

  1. [root@riyimei tmp]# mkdir mysql
  2. [root@riyimei tmp]#
  3. [root@riyimei tmp]#
  4. [root@riyimei tmp]# mkdir -v mysql1
  5. mkdir: created directory mysql1
  6. [root@riyimei tmp]#

2:在MySQL底下再创建一个子目录rpm

  1. [root@riyimei tmp]# mkdir -pv /tmp/mysql/rpm
  2. mkdir: created directory ‘/tmp/mysql/rpm
  3. [root@riyimei tmp]# ls -l /tmp/mysql
  4. total 0
  5. drwxr-x--- 2 root root 279 Jul 25 19:19 rpm
  6. [root@riyimei tmp]#

3:创建一个目录权限为644的目录config

  1. [root@riyimei tmp]# mkdir -pv -m 644 /tmp/mysql/config
  2. mkdir: created directory ‘/tmp/mysql/config
  3. [root@riyimei tmp]# ls -l /tmp/mysql/config/
  4. total 0
  5. drw-r--r-- 2 root root 6 Jul 25 19:26 config
  6. drwxr-x--- 2 root root 279 Jul 25 19:19 rpm
  7. [root@riyimei tmp]#

rmdir

用法:只能删除空目录
常用参数:
-p 递归删除
-v 显示删除过程
示例:
1:删除一个空目录

  1. [root@riyimei tmp]# rmdir test/
  2. rmdir: failed to remove test/’: Directory not empty
  3. [root@riyimei tmp]#
  4. [root@riyimei tmp]#
  5. [root@riyimei tmp]# rm -rf /tmp/test/liweiming
  6. [root@riyimei tmp]#
  7. [root@riyimei tmp]# rmdir -v test
  8. rmdir: removing directory, test
  9. [root@riyimei tmp]#

文件管理命令:cp、mv、rm

cp

用法:复制文件或者目录

当目的地为目录里时,将文件复制到目录里面 当目的地为文件时: 1、文件不能为目录 2、只能复制单个文件 文件复制到文件的时候,即是覆盖文件(内容不变)

常用参数:
-p 复制链接文件时 不复制链接文件的源文件
-a 用于归档文件
-L 复制链接文件时,直接复制其源文件,名称为链接名称
-r,R 递归复制
-i 交互式复制
-f
-n

  1. -a, --archive
  2. same as -dR --preserve=all
  3. --attributes-only
  4. don't copy the file data, just the attributes
  5. --backup[=CONTROL]
  6. make a backup of each existing destination file
  7. -b like --backup but does not accept an argument
  8. --copy-contents
  9. copy contents of special files when recursive
  10. -d same as --no-dereference --preserve=links
  11. -f, --force
  12. if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option
  13. is also used)
  14. -i, --interactive
  15. prompt before overwrite (overrides a previous -n option)
  16. -H follow command-line symbolic links in SOURCE
  17. -l, --link
  18. hard link files instead of copying
  19. -L, --dereference
  20. always follow symbolic links in SOURCE
  21. -n, --no-clobber
  22. do not overwrite an existing file (overrides a previous -i option)
  23. -P, --no-dereference
  24. never follow symbolic links in SOURCE
  25. -p same as --preserve=mode,ownership,timestamps
  26. --preserve[=ATTR_LIST]
  27. preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context,
  28. links, xattr, all
  29. -c deprecated, same as --preserve=context
  30. --no-preserve=ATTR_LIST
  31. don't preserve the specified attributes
  32. --parents
  33. use full source file name under DIRECTORY
  34. -R, -r, --recursive
  35. copy directories recursively
  36. --reflink[=WHEN]
  37. control clone/CoW copies. See below
  38. --remove-destination
  39. remove each existing destination file before attempting to open it (contrast with --force)
  40. --sparse=WHEN
  41. control creation of sparse files. See below
  42. --strip-trailing-slashes
  43. remove any trailing slashes from each SOURCE argument
  44. -s, --symbolic-link
  45. make symbolic links instead of copying
  46. -S, --suffix=SUFFIX
  47. override the usual backup suffix
  48. -t, --target-directory=DIRECTORY
  49. copy all SOURCE arguments into DIRECTORY
  50. -T, --no-target-directory
  51. treat DEST as a normal file
  52. -u, --update
  53. copy only when the SOURCE file is newer than the destination file or when the destination file is missing
  54. -v, --verbose
  55. explain what is being done
  56. -x, --one-file-system
  57. stay on this file system
  58. -Z set SELinux security context of destination file to default type
  59. --context[=CTX]
  60. like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
  61. --help display this help and exit
  62. --version
  63. 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目录下的一级子文件

  1. [root@riyimei ~]# find /tmp/ -maxdepth 1
  2. /tmp/
  3. /tmp/.XIM-unix
  4. /tmp/.ICE-unix
  5. /tmp/.Test-unix
  6. /tmp/.X11-unix
  7. /tmp/.font-unix
  8. /tmp/mysql
  9. /tmp/mysql1
  10. [root@riyimei ~]#

显示最大级

  1. [root@riyimei ~]# find /tmp/ -maxdepth 4
  2. /tmp/
  3. /tmp/.XIM-unix
  4. /tmp/.ICE-unix
  5. /tmp/.Test-unix
  6. /tmp/.X11-unix
  7. /tmp/.font-unix
  8. /tmp/mysql
  9. /tmp/mysql/rpm
  10. /tmp/mysql/rpm/mysql-community-client-8.0.21-1.el7.x86_64.rpm
  11. /tmp/mysql/rpm/mysql-community-common-8.0.21-1.el7.x86_64.rpm
  12. /tmp/mysql/rpm/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
  13. /tmp/mysql/rpm/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
  14. /tmp/mysql/rpm/mysql-community-server-8.0.21-1.el7.x86_64.rpm
  15. /tmp/mysql/config
  16. /tmp/mysql1
  17. [root@riyimei ~]#

tests
文件名

  • -iname pattern 忽略大小写
  • -name pattern 根据文件名查找

example:
查看/tmp目录下以mysql开头的文件 忽略大小写

  1. [root@riyimei ~]# find /tmp/ -iname mysql*
  2. /tmp/mysql
  3. /tmp/mysql/rpm/mysql-community-client-8.0.21-1.el7.x86_64.rpm
  4. /tmp/mysql/rpm/mysql-community-common-8.0.21-1.el7.x86_64.rpm
  5. /tmp/mysql/rpm/mysql-community-libs-8.0.21-1.el7.x86_64.rpm
  6. /tmp/mysql/rpm/mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
  7. /tmp/mysql/rpm/mysql-community-server-8.0.21-1.el7.x86_64.rpm
  8. /tmp/mysql1
  9. /tmp/MYSQL
  10. [root@riyimei ~]#

正则表达式

  1. [root@riyimei ~]# find /etc -regex .*ts
  2. /etc/pki/ca-trust/extracted/java/cacerts
  3. /etc/pki/java/cacerts
  4. /etc/pki/tls/certs
  5. /etc/pki/CA/certs
  6. /etc/pki/CA/newcerts
  7. /etc/openldap/certs
  8. /etc/exports

示例: