目录结构
| 命令 | 解释 |
|---|---|
| /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
更改文件的访问权限
Usage: chmod [OPTION]... MODE[,MODE]... FILE...or: chmod [OPTION]... OCTAL-MODE FILE...or: chmod [OPTION]... --reference=RFILE FILE...Change the mode of each FILE to MODE.With --reference, change the mode of each FILE to that of RFILE.-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--help display this help and exit--version output version information and exitEach MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.GNU coreutils online help: <https://www.gnu.org/software/coreutils/>Full documentation at: <https://www.gnu.org/software/coreutils/chmod>or available locally via: info '(coreutils) chmod invocation'
八进制模式
示例:设置文件的权限为所有者可读可写
touch 1.txtchmod 600 1.txt------------------------------------------------# root @ Tenyun in /home/hellochen [22:24:35]$ ll -atotal 8.0Kdrwxr-xr-x 2 root root 4.0K Dec 16 22:24 .drwxr-xr-x. 5 root root 4.0K Dec 16 14:33 ..-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 粘滞位 |
示例:对上述文件添加群组可读可写权限并删除所有者写入权限
chmod u-w,g=rw 1.txt-----------------------------------------------# root @ Tenyun in /home/hellochen [22:39:13]$ lltotal 0-r--rw---- 1 root root 0 Dec 16 22:24 1.txt
chown
更改文件的所有权限
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...or: chown [OPTION]... --reference=RFILE FILE...Change the owner and/or group of each FILE to OWNER and/or GROUP.With --reference, change the owner and group of each FILE to those of RFILE.-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 isthe 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 theownership of a symlink)--from=CURRENT_OWNER:CURRENT_GROUPchange the owner and/or group of each file only ifits current owner and/or group match those specifiedhere. Either may be omitted, in which case a matchis not required for the omitted attribute--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 thanspecifying OWNER:GROUP values-R, --recursive operate on files and directories recursivelyThe following options modify how a hierarchy is traversed when the -Roption is also specified. If more than one is specified, only the finalone takes effect.-H if a command line argument is a symbolic linkto a directory, traverse it-L traverse every symbolic link to a directoryencountered-P do not traverse any symbolic links (default)--help display this help and exit--version output version information and exitOwner is unchanged if missing. Group is unchanged if missing, but changedto login group if implied by a ':' following a symbolic OWNER.OWNER and GROUP may be numeric as well as symbolic.Examples:chown root /u Change the owner of /u to "root".chown root:staff /u Likewise, but also change its group to "staff".chown -hR root /u Change the owner of /u and subfiles to "root".GNU coreutils online help: <https://www.gnu.org/software/coreutils/>Full documentation at: <https://www.gnu.org/software/coreutils/chown>or available locally via: info '(coreutils) chown invocation'
示例:修改文件所有权为www,群组权限为www
chown www 1.js----------------------------------------------# root @ Tenyun in /home/hellochen [22:52:23]$ lltotal 4.0K-rw-r--r-- 1 www www 0 Dec 16 22:52 1.js-r--rw---- 1 root root 0 Dec 16 22:24 1.txt
目录管理
- man 帮助命令
查看所有函数以及命令的用法
- cd 切换目录
- ./ :当前目录
- ../ :上级目录
- / : 根目录
- ls (ll)列出目录
```shell
NAME
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.
Mandatory arguments to long options are mandatory for short options too.-a, --allall ,查看全部的文件,包括隐藏文件 .-A, --almost-alldo not list implied . and ..-l参数 列出所有的文件,包含文件的属性和权限,没有隐藏文件所有Linux可以组合使用!--authorwith -l, print the author of each file-b, --escapeprint C-style escapes for nongraphic characters
- pwd 显示当前用户所在目录- mkdir 创建目录- rmdir 删除目录注意:rmdir 仅能删除空的目录,如果下面存在文件,需要先删除文件,递归删除多个目录 -p 参数即可- cp 复制文件或目录cp 旧路径 --> 新路径- rm 删除文件或目录- -f 忽略不存在的文件,不会出现警告,强制删除!- -r 递归删除目录!- -i 互动,删除询问是否删除- `rm -rf /*` 递归删除所有文件即删除跑路操作!- mv 移动文件或目录 重命名文件<a name="4okod"></a>## 查看文件内容- cat 正序查看文件- tac 倒叙查看文件- nl 带行号查看- more 显示一页内容- more 一页一页的显示文件内容,带余下内容的(空格代表翻页,enter 代表向下看一行, :f 行<br />号)- less 显示一页内容less 与 more 类似,但是比 more 更好的是,他可以往前翻页! (空格下翻页,pageDown,pageUp键代表翻动页面!退出 q 命令,查 找字符串 /要查询的字符向下查询,向上查询使用?要查询的字符串,n 继续搜寻下一个,N 上寻找!)- head 只看前几行head 只看头几行 通过 -n 参数来控制显示几行- tail 只看后几行同上head<a name="97Tma"></a>## 文件/目录打包及解压缩常见压缩扩展名| *.Z compress | 程序压缩的文件; || --- | --- || *.zip zip | 程序压缩的文件; || *.gz gzip | 程序压缩的文件; || *.bz2 bzip2 | 程序压缩的文件; || *.xz xz | 程序压缩的文件; || *.tar tar | 程序打包的数据,并没有压缩过; || *.tar.gz tar | 程序打包的文件,其中并且经过 gzip 的压缩 || *.tar.bz2 tar | 程序打包的文件,其中并且经过 bzip2 的压缩 || *.tar.xz tar | 程序打包的文件,其中并且经过 xz 的压缩 |<a name="FfZrs"></a>### 文件压缩<a name="BOX5U"></a>#### gzip/gunzipgzip/gunzip是用来压缩和解压缩单个文件的工具,zcat/zmore/zless查看压缩后文件内容```shellUsage: gzip [OPTION]... [FILE]...Compress or uncompress FILEs (by default, compress FILES in-place).Mandatory arguments to long options are mandatory for short options too.-c, --stdout write on standard output, keep original files unchanged-d, --decompress decompress-f, --force force overwrite of output file and compress links-h, --help give this help-k, --keep keep (don't delete) input files-l, --list list compressed file contents-L, --license display software license-n, --no-name do not save or restore the original name and timestamp-N, --name save or restore the original name and timestamp-q, --quiet suppress all warnings-r, --recursive operate recursively on directories--rsyncable make rsync-friendly archive-S, --suffix=SUF use suffix SUF on compressed files--synchronous synchronous output (safer if system crashes, but slower)-t, --test test compressed file integrity-v, --verbose verbose mode-V, --version display version number-1, --fast compress faster-9, --best compress betterWith no FILE, or when FILE is -, read standard input.Report bugs to <bug-gzip@gnu.org>.-----------------------------------------------------------------------------------------Usage: /usr/bin/gunzip [OPTION]... [FILE]...Uncompress FILEs (by default, in-place).Mandatory arguments to long options are mandatory for short options too.-c, --stdout write on standard output, keep original files unchanged-f, --force force overwrite of output file and compress links-k, --keep keep (don't delete) input files-l, --list list compressed file contents-n, --no-name do not save or restore the original name and timestamp-N, --name save or restore the original name and timestamp-q, --quiet suppress all warnings-r, --recursive operate recursively on directories-S, --suffix=SUF use suffix SUF on compressed files--synchronous synchronous output (safer if system crashes, but slower)-t, --test test compressed file integrity-v, --verbose verbose mode--help display this help and exit--version display version information and exitWith no FILE, or when FILE is -, read standard input.Report bugs to <bug-gzip@gnu.org>.
示例:解压缩单个文件并查看
# root @ Tenyun in /home/hellochen [15:45:38]$ gzip -v 1.js1.js: 61.8% -- replaced with 1.js.gz$ lltotal 12K-rw-r--r-- 1 root root 192 Dec 17 15:45 1.js.gz-rw-r--r-- 1 root root 0 Dec 17 15:37 2.js-rw-r--r-- 1 root root 663 Dec 17 15:38 3.js-rw-r--r-- 1 root root 0 Dec 17 15:38 4.js---------------------------------------------------------$ zcat 1.js.gz*.Z compress 程序压缩的文件*.zip zip 程序压缩的文件*.gz gzip 程序压缩的文件*.bz2 bzip2 程序压缩的文件*.xz xz 程序压缩的文件*.tar tar 程序打包的数据并没有压缩过*.tar.gz tar 程序打包的文件其中并且经过 gzip 的压缩*.tar.bz2 tar 程序打包的文件其中并且经过 bzip2 的压缩*.tar.xz tar 程序打包的文件其中并且经过 xz 的压缩---------------------------------------------------------# root @ Tenyun in /home/hellochen [15:46:13]$ gunzip 1.js.gz$ ls1.js 2.js 3.js 4.js test1.zip
上面示例可知:gzip压缩单个文件时会生成.gz后缀,并且会删除源文件
bzip2/bunzip2
xz/xunzip
区别
| 时间 | 压缩率 |
|---|---|
| xz>bzip2>gzip | xz>bzip2>gzip |
文件打包
zip/unzip
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]The default action is to add or replace zipfile entries from list, whichcan include the special name - to compress standard input.If zipfile and list are omitted, zip compresses stdin to stdout.-f freshen: only changed files -u update: only changed or new files-d delete entries in zipfile -m move into zipfile (delete OS files)-r recurse into directories -j junk (don't record) directory names-0 store only -l convert LF to CR LF (-ll CR LF to LF)-1 compress faster -9 compress better-q quiet operation -v verbose operation/print version info-c add one-line comments -z add zipfile comment-@ read names from stdin -o make zipfile as old as latest entry-x exclude the following names -i include only the following names-F fix zipfile (-FF try harder) -D do not add directory entries-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)-T test zipfile integrity -X eXclude eXtra file attributes-y store symbolic links as the link instead of the referenced file-e encrypt -n don't compress these suffixes-h2 show more help----------------------------------------------------------------------------Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]Default action is to extract files in list, except those in xlist, to exdir;file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage).-p extract files to pipe, no messages -l list files (short format)-f freshen existing files, create none -t test compressed archive data-u update files, create if necessary -z display archive comment only-v list verbosely/show version info -T timestamp archive to latest-x exclude files that follow (in xlist) -d extract files into exdirmodifiers:-n never overwrite existing files -q quiet mode (-qq => quieter)-o overwrite files WITHOUT prompting -a auto-convert any text files-j junk paths (do not make directories) -aa treat ALL files as text-U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields-C match filenames case-insensitively -L make (some) names lowercase-X restore UID/GID info -V retain VMS version numbers-K keep setuid/setgid/tacky permissions -M pipe through "more" pager-O CHARSET specify a character encoding for DOS, Windows and OS/2 archives-I CHARSET specify a character encoding for UNIX and other archivesSee "unzip -hh" or unzip.txt for more help. Examples:unzip data1 -x joe => extract all files except joe from zipfile data1.zipunzip -p foo | more => send contents of foo.zip via pipe into program moreunzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
示例:将多个文件解压缩
# root @ Tenyun in /home/hellochen [16:14:48]$ zip test1 1.js 2.jsupdating: 1.js (deflated 62%)updating: 2.js (stored 0%)$ ls1.js 2.js 3.js 4.js test1.zip# root @ Tenyun in /home/hellochen [16:16:15]$ unzip test1.zipArchive: test1.zipinflating: 1.jsextracting: 2.js# root @ Tenyun in /home/hellochen [16:16:21]$ ls1.js 2.js test1.zip test2.zip------------------------------------------------------# root @ Tenyun in /home/hellochen [16:17:52]$ unzip test1.zipArchive: test1.zipreplace 1.js? [y]es, [n]o, [A]ll, [N]one, [r]ename: Ainflating: 1.jsextracting: 2.js
由上面的示例可知:zip将多个文件进行压缩并创建一个为.zip后缀名的压缩包,unzip解压时如果当前目录下有同名文件则提示是否替换
tar
Usage: tar [OPTION...] [FILE]...GNU 'tar' saves many files together into a single tape or disk archive, and canrestore individual files from the archive.Examples:tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.tar -tvf archive.tar # List all files in archive.tar verbosely.tar -xf archive.tar # Extract all files from archive.tar.----more view [man tar]选项与参数:-c :建立打包文件,可搭配 -v 来察看过程中被打包的档名(filename)-t :察看打包文件的内容含有哪些档名,重点在察看『档名』就是了;-x :解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开特别留意的是, -c, -t, -x 不可同时出现在一串指令列中。-z :透过 gzip 的支持进行压缩/解压缩:此时档名最好为 *.tar.gz-j :透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2-J :透过 xz 的支持进行压缩/解压缩:此时档名最好为 *.tar.xz特别留意, -z, -j, -J 不可以同时出现在一串指令列中-v :在压缩/解压缩的过程中,将正在处理的文件名显示出来!-f filename:-f 后面要立刻接要被处理的档名!建议 -f 单独写一个选项啰!(比较不会忘记)-C 目录 :这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。其他后续练习会使用到的选项介绍:-p(小写) :保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件-P(大写) :保留绝对路径,亦即允许备份数据中含有根目录存在之意;--exclude=FILE:在压缩的过程中,不要将 FILE 打包!
示例:
压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称查 询:tar -jtv -f filename.tar.bz2解压缩: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- - <@listfiles...> <path_to_extract\>
区别
- 都可以对目录进行打包,tar不支持压缩需要搭配gzip
- rar是商业软件,压缩比和安全算法较高
