目录结构

命令 解释
/bin: bin是Binary的缩写, 这个目录存放着最经常使用的命令。
/boot: 这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件。(不要动)
/dev: dev是Device(设备)的缩写, 存放的是Linux的外部设备,在Linux中访问设备的方式和访问文件的方式是相同的。
/etc: 这个目录用来存放所有的系统管理所需要的配置文件和子目录。
/home: 用户的主目录,在Linux中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的。
/lib: 这个目录里存放着系统最基本的动态连接共享库,其作用类似于Windows里的DLL文件。(不要动)
/lost+found: 这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件。(存放突然关机的一些文件)
/media: linux系统会自动识别一些设备,例如U盘、光驱等等,当识别后,linux会把识别的设备挂载到这个目录下。
/mnt :系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在/mnt/上,然后进入该目录就可以查看光盘
/opt: 这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。
/proc: 这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。(不用管)
/root: 该目录为系统管理员,也称作超级权限者的用户主目录。
/sbin: s就是Super User的意思,这里存放的是系统管理员使用的系统管理程序。
/srv: 该目录存放一些服务启动之后需要提取的数据。
/sys: 这是linux2.6内核的一个很大的变化。该目录下安装了2.6内核中新出现的一个文件系统sysfs 。
/tmp: 这个目录是用来存放一些临时文件的。用完即丢的文件,可以放在这个目录下,安装包!
ls //usr:这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似于windows下的program files目录
/usr/bin: 系统用户使用的应用程序。
/usr/sbin: 超级用户使用的比较高级的管理程序和系统守护程序。Super
/usr/src: 内核源代码默认的放置目录。
/var: 这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。
/run: 是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。
/www: 存放服务器网站相关的资源,环境,网站的项目

文件/目录权限

文件和目录权限
1 2 3 4 5 6 7 8 9 10
- r/- w/- x/- r/- w/- x/- r/- w/- x/-
说明 文件类型 属主权限(u/user) 群组权限(g/group) 其他用户权限(0/other)

文件类型

  • [-] 为目录
  • [d] 为文件
  • [l] 为链接
  • [b] 为可存储接口设备
  • [c] 为串行端口设备 | 权限的字符表示法与数字表示法对应关系 | | | | | | | —- | —- | —- | —- | —- | —- | | 字符表示 | 二进制表示 | 八进制表示 | 字符表示 | 二进制表示 | 八进制表示 | | —- | 000 | 0 | —x | 001 | 1 | | -w- | 010 | 2 | -wx | 011 | 3 | | r— | 100 | 4 | r-x | 101 | 5 | | rw- | 110 | 6 | rwx | 111 | 7 | | 字符表示 | | 二进制表示 | | 八进制表示 | | | rwxrwxrwx | 所有权限 | 111111111 | | 777 | | | rw-r——- | 用户可读可写,组可读 | 110100000 | | 640 | | | rw-r—rw- | 用户可读可写,组可读,其他用户可读可写 | 110100110 | | 646 | | | rw-r—r— | 用户可读可写,组和其他用户可读 | 110100100 | | 644 | |

更改文件/目录权限命令

chmod

更改文件的访问权限

  1. Usage: chmod [OPTION]... MODE[,MODE]... FILE...
  2. or: chmod [OPTION]... OCTAL-MODE FILE...
  3. or: chmod [OPTION]... --reference=RFILE FILE...
  4. Change the mode of each FILE to MODE.
  5. With --reference, change the mode of each FILE to that of RFILE.
  6. -c, --changes like verbose but report only when a change is made
  7. -f, --silent, --quiet suppress most error messages
  8. -v, --verbose output a diagnostic for every file processed
  9. --no-preserve-root do not treat '/' specially (the default)
  10. --preserve-root fail to operate recursively on '/'
  11. --reference=RFILE use RFILE's mode instead of MODE values
  12. -R, --recursive change files and directories recursively
  13. --help display this help and exit
  14. --version output version information and exit
  15. Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.
  16. GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
  17. Full documentation at: <https://www.gnu.org/software/coreutils/chmod>
  18. or available locally via: info '(coreutils) chmod invocation'

八进制模式

示例:设置文件的权限为所有者可读可写

  1. touch 1.txt
  2. chmod 600 1.txt
  3. ------------------------------------------------
  4. # root @ Tenyun in /home/hellochen [22:24:35]
  5. $ ll -a
  6. total 8.0K
  7. drwxr-xr-x 2 root root 4.0K Dec 16 22:24 .
  8. drwxr-xr-x. 5 root root 4.0K Dec 16 14:33 ..
  9. -rw------- 1 root root 0 Dec 16 22:24 1.txt

字符模式

修改权限可选用的字符选项
用户对象表 修改操作 权限表示
u 文件所有者
g 同群组用户
o 其他用户
a 所有用户
[+] 赋予权限
[-] 删除权限
[=] 设置权限
r 读权限
w 写权限
x 执行权限-
- 无权限
s、S 设置set-UID 和 set-GID
t、T 粘滞位

示例:对上述文件添加群组可读可写权限并删除所有者写入权限

  1. chmod u-w,g=rw 1.txt
  2. -----------------------------------------------
  3. # root @ Tenyun in /home/hellochen [22:39:13]
  4. $ ll
  5. total 0
  6. -r--rw---- 1 root root 0 Dec 16 22:24 1.txt

chown

更改文件的所有权限

  1. Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
  2. or: chown [OPTION]... --reference=RFILE FILE...
  3. Change the owner and/or group of each FILE to OWNER and/or GROUP.
  4. With --reference, change the owner and group of each FILE to those of RFILE.
  5. -c, --changes like verbose but report only when a change is made
  6. -f, --silent, --quiet suppress most error messages
  7. -v, --verbose output a diagnostic for every file processed
  8. --dereference affect the referent of each symbolic link (this is
  9. the default), rather than the symbolic link itself
  10. -h, --no-dereference affect symbolic links instead of any referenced file
  11. (useful only on systems that can change the
  12. ownership of a symlink)
  13. --from=CURRENT_OWNER:CURRENT_GROUP
  14. change the owner and/or group of each file only if
  15. its current owner and/or group match those specified
  16. here. Either may be omitted, in which case a match
  17. is not required for the omitted attribute
  18. --no-preserve-root do not treat '/' specially (the default)
  19. --preserve-root fail to operate recursively on '/'
  20. --reference=RFILE use RFILE's owner and group rather than
  21. specifying OWNER:GROUP values
  22. -R, --recursive operate on files and directories recursively
  23. The following options modify how a hierarchy is traversed when the -R
  24. option is also specified. If more than one is specified, only the final
  25. one takes effect.
  26. -H if a command line argument is a symbolic link
  27. to a directory, traverse it
  28. -L traverse every symbolic link to a directory
  29. encountered
  30. -P do not traverse any symbolic links (default)
  31. --help display this help and exit
  32. --version output version information and exit
  33. Owner is unchanged if missing. Group is unchanged if missing, but changed
  34. to login group if implied by a ':' following a symbolic OWNER.
  35. OWNER and GROUP may be numeric as well as symbolic.
  36. Examples:
  37. chown root /u Change the owner of /u to "root".
  38. chown root:staff /u Likewise, but also change its group to "staff".
  39. chown -hR root /u Change the owner of /u and subfiles to "root".
  40. GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
  41. Full documentation at: <https://www.gnu.org/software/coreutils/chown>
  42. or available locally via: info '(coreutils) chown invocation'

示例:修改文件所有权为www,群组权限为www

  1. chown www 1.js
  2. ----------------------------------------------
  3. # root @ Tenyun in /home/hellochen [22:52:23]
  4. $ ll
  5. total 4.0K
  6. -rw-r--r-- 1 www www 0 Dec 16 22:52 1.js
  7. -r--rw---- 1 root root 0 Dec 16 22:24 1.txt

目录管理

  • man 帮助命令

查看所有函数以及命令的用法

  • cd 切换目录
    • ./ :当前目录
    • ../ :上级目录
    • / : 根目录
  • ls (ll)列出目录 ```shell NAME
    1. ls - list directory contents

SYNOPSIS ls [OPTION]… [FILE]…

DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alpha‐ betically if none of -cftuvSUX nor —sort is specified.

  1. Mandatory arguments to long options are mandatory for short options too.
  2. -a, --all
  3. all ,查看全部的文件,包括隐藏文件 .
  4. -A, --almost-all
  5. do not list implied . and ..
  6. -l
  7. 参数 列出所有的文件,包含文件的属性和权限,没有隐藏文件所有Linux可以组合使用!
  8. --author
  9. with -l, print the author of each file
  10. -b, --escape
  11. print C-style escapes for nongraphic characters
  1. - pwd 显示当前用户所在目录
  2. - mkdir 创建目录
  3. - rmdir 删除目录
  4. 注意:rmdir 仅能删除空的目录,如果下面存在文件,需要先删除文件,递归删除多个目录 -p 参数即可
  5. - cp 复制文件或目录
  6. cp 旧路径 --> 新路径
  7. - rm 删除文件或目录
  8. - -f 忽略不存在的文件,不会出现警告,强制删除!
  9. - -r 递归删除目录!
  10. - -i 互动,删除询问是否删除
  11. - `rm -rf /*` 递归删除所有文件即删除跑路操作!
  12. - mv 移动文件或目录 重命名文件
  13. <a name="4okod"></a>
  14. ## 查看文件内容
  15. - cat 正序查看文件
  16. - tac 倒叙查看文件
  17. - nl 带行号查看
  18. - more 显示一页内容
  19. - more 一页一页的显示文件内容,带余下内容的(空格代表翻页,enter 代表向下看一行, :f 行<br />号)
  20. - less 显示一页内容
  21. less more 类似,但是比 more 更好的是,他可以往前翻页! (空格下翻页,pageDownpageUp键代表翻动页面!退出 q 命令,查 找字符串 /要查询的字符向下查询,向上查询使用?要查询的字符串,n 继续搜寻下一个,N 上寻找!)
  22. - head 只看前几行
  23. head 只看头几行 通过 -n 参数来控制显示几行
  24. - tail 只看后几行
  25. 同上head
  26. <a name="97Tma"></a>
  27. ## 文件/目录打包及解压缩
  28. 常见压缩扩展名
  29. | *.Z compress | 程序压缩的文件; |
  30. | --- | --- |
  31. | *.zip zip | 程序压缩的文件; |
  32. | *.gz gzip | 程序压缩的文件; |
  33. | *.bz2 bzip2 | 程序压缩的文件; |
  34. | *.xz xz | 程序压缩的文件; |
  35. | *.tar tar | 程序打包的数据,并没有压缩过; |
  36. | *.tar.gz tar | 程序打包的文件,其中并且经过 gzip 的压缩 |
  37. | *.tar.bz2 tar | 程序打包的文件,其中并且经过 bzip2 的压缩 |
  38. | *.tar.xz tar | 程序打包的文件,其中并且经过 xz 的压缩 |
  39. <a name="FfZrs"></a>
  40. ### 文件压缩
  41. <a name="BOX5U"></a>
  42. #### gzip/gunzip
  43. gzip/gunzip是用来压缩和解压缩单个文件的工具,zcat/zmore/zless查看压缩后文件内容
  44. ```shell
  45. Usage: gzip [OPTION]... [FILE]...
  46. Compress or uncompress FILEs (by default, compress FILES in-place).
  47. Mandatory arguments to long options are mandatory for short options too.
  48. -c, --stdout write on standard output, keep original files unchanged
  49. -d, --decompress decompress
  50. -f, --force force overwrite of output file and compress links
  51. -h, --help give this help
  52. -k, --keep keep (don't delete) input files
  53. -l, --list list compressed file contents
  54. -L, --license display software license
  55. -n, --no-name do not save or restore the original name and timestamp
  56. -N, --name save or restore the original name and timestamp
  57. -q, --quiet suppress all warnings
  58. -r, --recursive operate recursively on directories
  59. --rsyncable make rsync-friendly archive
  60. -S, --suffix=SUF use suffix SUF on compressed files
  61. --synchronous synchronous output (safer if system crashes, but slower)
  62. -t, --test test compressed file integrity
  63. -v, --verbose verbose mode
  64. -V, --version display version number
  65. -1, --fast compress faster
  66. -9, --best compress better
  67. With no FILE, or when FILE is -, read standard input.
  68. Report bugs to <bug-gzip@gnu.org>.
  69. -----------------------------------------------------------------------------------------
  70. Usage: /usr/bin/gunzip [OPTION]... [FILE]...
  71. Uncompress FILEs (by default, in-place).
  72. Mandatory arguments to long options are mandatory for short options too.
  73. -c, --stdout write on standard output, keep original files unchanged
  74. -f, --force force overwrite of output file and compress links
  75. -k, --keep keep (don't delete) input files
  76. -l, --list list compressed file contents
  77. -n, --no-name do not save or restore the original name and timestamp
  78. -N, --name save or restore the original name and timestamp
  79. -q, --quiet suppress all warnings
  80. -r, --recursive operate recursively on directories
  81. -S, --suffix=SUF use suffix SUF on compressed files
  82. --synchronous synchronous output (safer if system crashes, but slower)
  83. -t, --test test compressed file integrity
  84. -v, --verbose verbose mode
  85. --help display this help and exit
  86. --version display version information and exit
  87. With no FILE, or when FILE is -, read standard input.
  88. Report bugs to <bug-gzip@gnu.org>.

示例:解压缩单个文件并查看

  1. # root @ Tenyun in /home/hellochen [15:45:38]
  2. $ gzip -v 1.js
  3. 1.js: 61.8% -- replaced with 1.js.gz
  4. $ ll
  5. total 12K
  6. -rw-r--r-- 1 root root 192 Dec 17 15:45 1.js.gz
  7. -rw-r--r-- 1 root root 0 Dec 17 15:37 2.js
  8. -rw-r--r-- 1 root root 663 Dec 17 15:38 3.js
  9. -rw-r--r-- 1 root root 0 Dec 17 15:38 4.js
  10. ---------------------------------------------------------
  11. $ zcat 1.js.gz
  12. *.Z compress 程序压缩的文件
  13. *.zip zip 程序压缩的文件
  14. *.gz gzip 程序压缩的文件
  15. *.bz2 bzip2 程序压缩的文件
  16. *.xz xz 程序压缩的文件
  17. *.tar tar 程序打包的数据并没有压缩过
  18. *.tar.gz tar 程序打包的文件其中并且经过 gzip 的压缩
  19. *.tar.bz2 tar 程序打包的文件其中并且经过 bzip2 的压缩
  20. *.tar.xz tar 程序打包的文件其中并且经过 xz 的压缩
  21. ---------------------------------------------------------
  22. # root @ Tenyun in /home/hellochen [15:46:13]
  23. $ gunzip 1.js.gz
  24. $ ls
  25. 1.js 2.js 3.js 4.js test1.zip

上面示例可知:gzip压缩单个文件时会生成.gz后缀,并且会删除源文件

bzip2/bunzip2

使用方式同gzip

xz/xunzip

同上

区别

时间 压缩率
xz>bzip2>gzip xz>bzip2>gzip

文件打包

zip/unzip

  1. zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  2. The default action is to add or replace zipfile entries from list, which
  3. can include the special name - to compress standard input.
  4. If zipfile and list are omitted, zip compresses stdin to stdout.
  5. -f freshen: only changed files -u update: only changed or new files
  6. -d delete entries in zipfile -m move into zipfile (delete OS files)
  7. -r recurse into directories -j junk (don't record) directory names
  8. -0 store only -l convert LF to CR LF (-ll CR LF to LF)
  9. -1 compress faster -9 compress better
  10. -q quiet operation -v verbose operation/print version info
  11. -c add one-line comments -z add zipfile comment
  12. -@ read names from stdin -o make zipfile as old as latest entry
  13. -x exclude the following names -i include only the following names
  14. -F fix zipfile (-FF try harder) -D do not add directory entries
  15. -A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
  16. -T test zipfile integrity -X eXclude eXtra file attributes
  17. -y store symbolic links as the link instead of the referenced file
  18. -e encrypt -n don't compress these suffixes
  19. -h2 show more help
  20. ----------------------------------------------------------------------------
  21. Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  22. Default action is to extract files in list, except those in xlist, to exdir;
  23. file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage).
  24. -p extract files to pipe, no messages -l list files (short format)
  25. -f freshen existing files, create none -t test compressed archive data
  26. -u update files, create if necessary -z display archive comment only
  27. -v list verbosely/show version info -T timestamp archive to latest
  28. -x exclude files that follow (in xlist) -d extract files into exdir
  29. modifiers:
  30. -n never overwrite existing files -q quiet mode (-qq => quieter)
  31. -o overwrite files WITHOUT prompting -a auto-convert any text files
  32. -j junk paths (do not make directories) -aa treat ALL files as text
  33. -U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields
  34. -C match filenames case-insensitively -L make (some) names lowercase
  35. -X restore UID/GID info -V retain VMS version numbers
  36. -K keep setuid/setgid/tacky permissions -M pipe through "more" pager
  37. -O CHARSET specify a character encoding for DOS, Windows and OS/2 archives
  38. -I CHARSET specify a character encoding for UNIX and other archives
  39. See "unzip -hh" or unzip.txt for more help. Examples:
  40. unzip data1 -x joe => extract all files except joe from zipfile data1.zip
  41. unzip -p foo | more => send contents of foo.zip via pipe into program more
  42. unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

示例:将多个文件解压缩

  1. # root @ Tenyun in /home/hellochen [16:14:48]
  2. $ zip test1 1.js 2.js
  3. updating: 1.js (deflated 62%)
  4. updating: 2.js (stored 0%)
  5. $ ls
  6. 1.js 2.js 3.js 4.js test1.zip
  7. # root @ Tenyun in /home/hellochen [16:16:15]
  8. $ unzip test1.zip
  9. Archive: test1.zip
  10. inflating: 1.js
  11. extracting: 2.js
  12. # root @ Tenyun in /home/hellochen [16:16:21]
  13. $ ls
  14. 1.js 2.js test1.zip test2.zip
  15. ------------------------------------------------------
  16. # root @ Tenyun in /home/hellochen [16:17:52]
  17. $ unzip test1.zip
  18. Archive: test1.zip
  19. replace 1.js? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  20. inflating: 1.js
  21. extracting: 2.js

由上面的示例可知:zip将多个文件进行压缩并创建一个为.zip后缀名的压缩包,unzip解压时如果当前目录下有同名文件则提示是否替换

tar

  1. Usage: tar [OPTION...] [FILE]...
  2. GNU 'tar' saves many files together into a single tape or disk archive, and can
  3. restore individual files from the archive.
  4. Examples:
  5. tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.
  6. tar -tvf archive.tar # List all files in archive.tar verbosely.
  7. tar -xf archive.tar # Extract all files from archive.tar.
  8. ----more view [man tar]
  9. 选项与参数:
  10. -c :建立打包文件,可搭配 -v 来察看过程中被打包的档名(filename)
  11. -t :察看打包文件的内容含有哪些档名,重点在察看『档名』就是了;
  12. -x :解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开
  13. 特别留意的是, -c, -t, -x 不可同时出现在一串指令列中。
  14. -z :透过 gzip 的支持进行压缩/解压缩:此时档名最好为 *.tar.gz
  15. -j :透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2
  16. -J :透过 xz 的支持进行压缩/解压缩:此时档名最好为 *.tar.xz
  17. 特别留意, -z, -j, -J 不可以同时出现在一串指令列中
  18. -v :在压缩/解压缩的过程中,将正在处理的文件名显示出来!
  19. -f filename:-f 后面要立刻接要被处理的档名!建议 -f 单独写一个选项啰!(比较不会忘记)
  20. -C 目录 :这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。
  21. 其他后续练习会使用到的选项介绍:
  22. -p(小写) :保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件
  23. -P(大写) :保留绝对路径,亦即允许备份数据中含有根目录存在之意;
  24. --exclude=FILE:在压缩的过程中,不要将 FILE 打包!

示例:

  1. 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称
  2. 询:tar -jtv -f filename.tar.bz2
  3. 解压缩:tar -jxv -f filename.tar.bz2 -C 欲解压缩的目录

rar

  • 下载:wget https://www.rarlab.com/rar/rarlinux-x64-6.0.0.tar.gz
  • 解压:tar -zxvf rarlinux-x64-6.0.0.tar.gz
  • 编译:make
  • 安装:make install ```shell Usage: rar - -
    1. <@listfiles...> <path_to_extract\>

a Add files to archive c Add archive comment ch Change archive parameters cw Write archive comment to file d Delete files from archive e Extract files without archived paths f Freshen files in archive i[par]= Find string in archives k Lock archive l[t[a],b] List archive contents [technical[all], bare] m[f] Move to archive [files only] p Print file to stdout r Repair archive rc Reconstruct missing volumes rn Rename archived files rr[N] Add data recovery record rv[N] Create recovery volumes s[name|-] Convert archive to or from SFX t Test archive files u Update files in archive v[t[a],b] Verbosely list archive contents [technical[all],bare] x Extract files with full path ——more view [rar —help] ```

区别

  1. 都可以对目录进行打包,tar不支持压缩需要搭配gzip
  2. rar是商业软件,压缩比和安全算法较高