文件操作和权限
文件操作和权限
在Linux中一切皆文件,包括设备也被抽象为文件,对应的设备文件在/dev目录中。目录也是文件,文件内容是文件名和文件的inode号。
在文件系统中,一个文件由一个inode和data block组成,inode存放文件的权限与属性,data block存放实际数据。目录的data block存放文件名和文件的inode号。
文件权限、属性和文件类型
lrl@DESKTOP-LRL:~$ ls -al
total 12
0 1 2 3 4 5
drwxr-xr-x 1 lrl lrl 4096 Aug 10 22:41 .
drwxr-xr-x 1 root root 4096 Aug 9 15:05 ..
-rw------- 1 lrl lrl 741 Aug 10 23:35 .bash_history
-rw-r--r-- 1 lrl lrl 220 Aug 9 15:05 .bash_logout
-rw-r--r-- 1 lrl lrl 3771 Aug 9 15:05 .bashrc
drwxr-xr-x 1 lrl lrl 4096 Aug 9 15:05 .landscape
-rw------- 1 lrl lrl 68 Aug 10 22:41 .lesshst
drwxr-xr-x 1 lrl lrl 4096 Aug 10 22:07 .local
-rw-r--r-- 1 lrl lrl 0 Aug 11 10:44 .motd_shown
-rw-r--r-- 1 lrl lrl 807 Aug 9 15:05 .profile
-rw-r--r-- 1 lrl lrl 0 Aug 10 22:13 .sudo_as_admin_successful

属性有
0:链接到此inode节点数量 1:owner 2:group 3:文件大小(默认以字节为单位) 4:文件修改时间 5:文件名
由上述可知:权限分为三组,分别为owner、group、other,每组权限都分为rwx(read,write,execute)。
权限的意义
-
对目录
- r:读取目录结构
- w:修改目录结构:
- 删除创建文件与目录
- 重命名
- 移动
- x:可切换到该目录作为工作名录执行命令
lrl@DESKTOP-LRL:~$ ls t t.txt lrl@DESKTOP-LRL:~$ chmod u-x t lrl@DESKTOP-LRL:~$ ls t ls: cannot access 't/t.txt': Permission denied t.txt lrl@DESKTOP-LRL:~$ ls -l t ls: cannot access 't/t.txt': Permission denied total 0 -????????? ? ? ? ? ?当没有x权限时,有r权限时虽然能读出目录内容,但是会显示无权限,查看文件属性时会有一串?
lrl@DESKTOP-LRL:~$ chmod u=x t lrl@DESKTOP-LRL:~$ vim t/t.txt lrl@DESKTOP-LRL:~$ chmod u=rwx t lrl@DESKTOP-LRL:~$ cd t lrl@DESKTOP-LRL:~/t$ ls t.txt lrl@DESKTOP-LRL:~/t$ cat t.txt jojoij当只具备x权限时,若知道目录中文件名字且对文件具有对应权限,则依然可以对文件进行操作。
-
对文件
- r:可读
- w:可编辑
- x:可执行
文件能否被执行,取决于是否拥有x权限,与文件名没有绝对关系。
ls - list directory contents
ls [OPTION]... [FILE]...
OPTIONS is folloing:
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
-c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest first
-f do not sort, enable -aU, disable -ls --color
-F, --classify
append indicator (one of */=>@|) to entries,/:directory,*:exectuable file,=:socket,|:FIFO
--color[=WHEN]
colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below
--full-time
like -l --time-style=full-iso
-h, --human-readable
with -l and -s, print sizes like 1K 234M 2G etc.
-i, --inode
print the index number of each file
-l use a long listing format(full information)
-r, --reverse
reverse order while sorting
-R, --recursive
list subdirectories recursively
--sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
--time=WORD
with -l, show time as WORD instead of default modification time: atime or access or use (-u); ctime or status (-c);also use specified time as sort key if --sort=time (newest first)
-t sort by modification time, newest first
-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access
time, newest first
-U do not sort; list entries in directory order
touch
file - determine file type
file filename...
#示例
lrl@DESKTOP-LRL:~$ file .bash_history
.bash_history: ASCII text
chown - change file owner and group
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chgrp - change group ownership
chgrp [OPTION]... GROUP FILE...
chmod - change file mode bits
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
OPTIONS:
-R 递归修改
MODE的表示方式有两种:
-
数字类型
#rwx分别用数字421代替,就是二进制111 #每组权限都是rwx的数字形式累加和,三组权限用xyz表示,则使用如下所示 chmod xyz file -
符号类型
| 符号 | 操作 | 权限 |
|---|---|---|
| u(user,也就是owner) g(group) o(other) a(all,也就是ugo) |
+(添加) -(移除) =(设置) |
r w x |
lrl@DESKTOP-LRL:~$ chmod u=rwx,go=rx .bashrc #设置权限
lrl@DESKTOP-LRL:~$ chmod a+w .bashrc #为所有身份添加权限
lrl@DESKTOP-LRL:~$ ll
total 12
drwxr-xr-x 1 lrl lrl 4096 Aug 11 15:25 ./
drwxr-xr-x 1 root root 4096 Aug 9 15:05 ../
-rw------- 1 lrl lrl 741 Aug 10 23:35 .bash_history
-rw-r--r-- 1 lrl lrl 220 Aug 9 15:05 .bash_logout
-rwxrwxrwx 1 lrl lrl 3771 Aug 9 15:05 .bashrc*
drwxr-xr-x 1 lrl lrl 4096 Aug 9 15:05 .landscape/
-rw------- 1 lrl lrl 114 Aug 11 15:25 .lesshst
drwxr-xr-x 1 lrl lrl 4096 Aug 10 22:07 .local/
-rw-r--r-- 1 lrl lrl 0 Aug 11 10:44 .motd_shown
-rw-r--r-- 1 lrl lrl 807 Aug 9 15:05 .profile
-rw-r--r-- 1 lrl lrl 0 Aug 10 22:13 .sudo_as_admin_successful
特殊权限
特殊权限为SUID、SGID、SBIT
SUID:Set UID
- 仅对二进制可执行程序有效
- 执行者对该程序具有x权限
- 在程序执行过程中具有程序拥有者owner的权限
lrl@DESKTOP-LRL:~$ ls -l /bin/passwd
-rwsr-xr-x 1 root root 68208 May 28 2020 /bin/passwd
lrl@DESKTOP-LRL:~$ ll /etc/shadow
-rw-r----- 1 root shadow 972 Aug 9 15:05 /etc/shadow
passwd的owner权限的x变成了s,这就是特殊权限SUID。/etc/shadow保存了用户密码,一般用户无法修改。但是我们可以通过执行passwd临时获得passwd拥有者root的权限来修改密码。
SGID:Set GID
-
对二进制程序
- 执行者对该程序具有x权限。
- 程序执行过程中获得该程序用户组支持——即执行者的有效用户组临时变成该程序用户组。
-
对目录
- 用户对目录具有rx权限时,用户在此目录下的有效用户组变为该目录的用户组。
- 在上一个条件下,若同时具有w权限,则用户建立的新文件的用户组与目录用户组相同。
SBIT:Stick Bit
用户对于目录具有wx权限时,用户再次目录下建立的文件只有自己与root才可删除。
特殊权限设置
除了三组基本权限外,特殊权限又是一组,SUID,SGID,SBIT分别对应着数字类型中的4,2,1,也对应着三组基本权限的x位,若设置了特殊权限,则对应的x位为s,s,t,若无x权限,则对应的x为S,S,T。
-
数字类型
chmod sxyz FILE... #s即为特殊权限的累加和 -
符号类型
相比基本权限的符号外,多出了一个s,一个t。
chmod u+s FILE... #s即为特殊权限 chmod g+s FILE... chmod o+t FILE...
默认权限与umask
umask的使用
mask,面具,umask即为设置权限掩码,就是从权限中移除umask所指定的权限。
umask [-p] [-S] [mode]
-S #符号类型
-p #暂时不知道
lrl@DESKTOP-LRL:~/t$ umask #不加选项参数,查看掩码
0022
lrl@DESKTOP-LRL:~/t$ umask -S #以符号类型查看默认权限
u=rwx,g=rx,o=rx
lrl@DESKTOP-LRL:~/t$ umask 0000 #以数字类型设置掩码
lrl@DESKTOP-LRL:~/t$ umask
0000
lrl@DESKTOP-LRL:~/t$ umask 0022 #恢复权限
lrl@DESKTOP-LRL:~/t$ umask
0022
lrl@DESKTOP-LRL:~/t$ umask -S a=rwx #以符号类型设置权限
u=rwx,g=rwx,o=rwx
lrl@DESKTOP-LRL:~/t$ umask
0000
- 在没有umask的情况下
- 建立文件时,默认没有x权限,只有rw,即权限是666,
-rw-rw-rw- - 建立目录时,x对于目录特别重要,所以默认是777,
drwxrwxrwx
- 有umask(上述例子中为0022,-----w--w-),在1的情况下移除umask所指定的权限
- 对文件 则为
-rw-r--r--,即644 - 对目录 则为
drwxr-xr-x,即755
FHS与目录树
相对路径与绝对路径
- 绝对路径:从根目录/写起
- 相对路径:相对于当前目录的路径,就是非/开头

文件操作命令
目录操作
cd - change directory
特殊符号
~ #home目录
~account #account home目录
- #上一次所在的目录
. #当前目录
.. #上一级目录
cd .. #切换到上一级目录
cd ~ #切换到home目录
cd /home/lrl #使用绝对路径切换到home目录lrl
pwd - print name of current/working directory
lrl@DESKTOP-LRL:~$ pwd
/home/lrl
mkdir - make directories
mkdir [OPTION]... DIRECTORY...
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
lrl@DESKTOP-LRL:~$ mkdir -m 711 test
lrl@DESKTOP-LRL:~$ mkdir --mode=711 test2
lrl@DESKTOP-LRL:~$ mkdir -p test3/test4
lrl@DESKTOP-LRL:~$ ls -ld test*
drwx--x--x 1 lrl lrl 4096 Aug 11 22:27 test
drwx--x--x 1 lrl lrl 4096 Aug 11 22:28 test2
drwxr-xr-x 1 lrl lrl 4096 Aug 11 22:28 test3
rmdir - remove empty directories
rmdir [OPTION]... DIRECTORY...
-p, --parents
remove DIRECTORY and its ancestors;
e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'
lrl@DESKTOP-LRL:~$ rmdir test test2 #rmdir只能删除空目录
lrl@DESKTOP-LRL:~$ rmdir -p test3/test4/ #将目录连同父目录一起删除
lrl@DESKTOP-LRL:~$ ls
lrl@DESKTOP-LRL:~$ #已全部删除,无输出结果
dirname和basename
每个文件的全名是从根目录到文件的一个完整绝对路径
dirname NAME #输出目录名
basename NAME #输出文件名
文件操作
移动、删除、复制:mv、rm、cp
mv - move (rename) files
mv [OPTION]... [-T] SOURCE DEST #Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
-i, --interactive
prompt before overwrite
-f, --force
do not prompt before overwriting
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing
-t, --target-directory=DIRECTORY #--target-directory=DIRECTORY ==> -t DIRECTORY
move all SOURCE arguments into DIRECTORY
#-短选项 参数 ==> --长选项=参数
rm - remove files or directories
rm [OPTION]... [FILE]...
-f, --force
ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
-interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
-r, -R, --recursive
remove directories and their contents recursively
-d, --dir
remove empty directories
By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a '-', for example '-foo', use one of these commands:
rm -- -foo
rm ./-foo
cp - copy files and directories
#Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE..
-a, --archive
same as -dR --preserve=all #用作备份,保留源文件全部权限属性,
-d same as --no-dereference --preserve=links #复制链接文件,不是源文件,默认复制源文件
-R, -r, --recursive
copy directories recursively
-P, --no-dereference
never follow symbolic links in SOURCE
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
-p same as --preserve=mode,ownership,timestamps
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
--parents
use full source file name under DIRECTORY
-l, --link #硬链接
hard link files instead of copying
-s, --symbolic-link #符号链接 符号链接的实际内容指向源文件的路径
make symbolic links instead of copying
默认情况下,cp 源文件与目标文件的权限是不同,目标文件的owner时命令执行者。因此使用 -a或-p选项完整复制文件权限属性。
**-短选项 参数 ==> --长选项=参数 **
查看文件内容
cat
由文件第一行开始输出
#concatenate files and print on the standard output
# With no FILE, or when FILE is -, read standard input.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
-
tac 逆序从最后一行开始输出,与cat相反,同时有自己特殊选项
-
nl 添加行号输出,输出的行号格式通过选项设置
# nl - number lines of files # Write each FILE to standard output, with line numbers added. # With no FILE, or when FILE is -, read standard input. -
more,只能下翻,不能上翻
-
less,more的升级版
| 按键 | 作用 |
|---|---|
| Space/PageDown/Ctrl+F | 下翻一页 |
| PageUp/Ctrl+B | 上翻一页 |
| /STRING | 向下查找 |
| ?STRING | 向上查找 |
| :f | 显示文件名及目前显示行数 |
| n | 重复上一次查找 |
| N | 反向重复上一次查找 |
| g | 到第一行 |
| G | 到最后一行 |
| q | 退出 |
man page页面就是使用less来输出。
-
head 不带选项默认输出前十行
# head - output the first part of files -n, --lines=[-]NUM print the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file -
tail 不带选项默认输出后十行
-n, --lines=[+]NUM output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM -
od 以指定TYPE读取文件
od [-t TYPE] FILE
查找文件
which
which - locate a command
依据PATH环境变量查找能在当前环境下执行的文件,命令就是可执行文件
which [-a] filename ...
-a print all matching pathnames of each argument
# which returns the pathnames of the files (or links) which would be executed in the current environment.
# It does this by searching the PATH for executable files matching the names of the arguments.
lrl@DESKTOP-LRL:~/t$ which -a ls
/usr/bin/ls
/bin/ls
type
查询命令是否为Shell内置命令
type [-tpa] command
lrl@DESKTOP-LRL:~/t$ which type
lrl@DESKTOP-LRL:~/t$ which history
lrl@DESKTOP-LRL:~/t$ type ls
ls is aliased to `ls --color=auto'
lrl@DESKTOP-LRL:~/t$ type history
history is a shell builtin
lrl@DESKTOP-LRL:~/t$ which alias
lrl@DESKTOP-LRL:~/t$ type which
which is hashed (/usr/bin/which)
lrl@DESKTOP-LRL:~/t$ type alias
alias is a shell builtin
Shell内置命令有cd、type、history、alias、umask,有个共同点就是没有man page,这些命令的使用方法通过man bash中,which也无法找到这些命令对应的文件。
-t #以如下NAME显示命令类型:
1.file:外部命令
2.alias:为alias命令设置的别名
3.builtin:Shell内置命令
-p #command为外部命令时显示完整路径
-a #将PATH变量中所有含command的命令都显示出来
lrl@DESKTOP-LRL:~/t$ type -t ls
alias
lrl@DESKTOP-LRL:~/t$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
whereis
whereis - locate the binary, source, and manual page files for a command
whereis then attempts to locate the desired program in the standard Linux places, and in the places specified by $PATH and $MANPATH.
whereis [options] [-BMS directory... -f] name...
The search restrictions (options -b, -m and -s) are cumulative and apply to the subsequent name patterns on the command line. Any new search restriction resets the search mask. For example,
whereis -bm ls tr -m gcc
searches for "ls" and "tr" binaries and man pages, and for "gcc" man pages only.
lrl@DESKTOP-LRL:~$ whereis -bm ls tr -m gcc
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
tr: /usr/bin/tr /usr/share/man/man1/tr.1.gz
gcc: #没有安装gcc,此处为空
The options -B, -M and -S reset search paths for the subsequent name patterns. For example,
whereis -m ls -M /usr/share/man/man1 -f cal
searches for "ls" man pages in all default paths, but for "cal" in the /usr/share/man/man1 directory only.
lrl@DESKTOP-LRL:~$ whereis -m ls -M /usr/share/man/man1 -f cal
ls: /usr/share/man/man1/ls.1.gz
cal: /usr/share/man/man1/cal.1.gz
# The search restrictions (options -b, -m and -s)
-b Search for binaries.
-m Search for manuals.
-s Search for sources.
-u Only show the command names that have unusual entries. A command is said to be unusual if it does not have just one entry of each explicitly requested type. Thus 'whereis -m -u *' asks for those files in the current directory which have no documentation file, or more than one.
#The options -B, -M and -S reset search paths
-B 'directories list'
Limit the places where whereis searches for binaries, by a whitespace-separated list of directories.
-M 'directories list'
Limit the places where whereis searches for manuals and documentation in Info format, by a whitespace-
separated list of directories.
-S 'directories list'
Limit the places where whereis searches for sources, by a whitespace-separated list of directories.
-l Output the list of effective lookup paths that whereis is using. When none of -B, -M, or -S is specified, the option will output the hard-coded paths that the command was able to find on the system.
locate
locate - find files by name
locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.(通过updatedb建立数据库,locate通过查询数据库查找文件)
locate can never report files created after the most recent update of the relevant database.(新创建文件在数据库计划更新时间之后,数据库并没有将其加入,所以locate找不到该文件)
locate [OPTION]... PATTERN...
-c, --count
Instead of writing file names on standard output, write the number of matching entries only.
-i, --ignore-case
Ignore case distinctions when matching patterns.
-l, --limit, -n LIMIT
Exit successfully after finding LIMIT entries. If the --count option is specified, the resulting count
is also limited to LIMIT.
-S, --statistics
Write statistics about each read database to standard output instead of searching for files and exit
successfully.
-r, --regexp REGEXP
Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.
--regex
Interpret all PATTERNs as extended regexps.
lrl@DESKTOP-LRL:~/t$ locate -S
Database /var/lib/mlocate/mlocate.db:
24318 directories
158575 files
13057716 bytes in file names
5738798 bytes used to store database
lrl@DESKTOP-LRL:~/t$ locate -S passwd
locate: non-option arguments are not allowed with --statistics
lrl@DESKTOP-LRL:~/t$ locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd
选项后面接参数,就是选项参数
find
find - search for files in a directory hierarchy
详情看man page以即csdn博客参考链接
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
#The -H, -L and -P options control the treatment of symbolic links.
# -Olevel Enables query optimisation.
#具体参考man page
#starting-point 搜索起始点
#expression a kind of query specification describing how we match files and what we do with the files that were matched,is composed of a sequence of things:Tests、 Actions、Operators、 Positional options、 Global options
打包压缩
压缩
尽管Linux后缀名并不像Windows那样,但是特定后缀名能起到提示作用,约定压缩文件与压缩文件后缀名对应关系如下。
| 后缀名 | 压缩程序 | 读取压缩文件程序 |
|---|---|---|
| .Z | compress | |
| .gz | gzip | zcat/zgrep/zmore/zless |
| .bz2 | bzip2 | bzcat/bzgrep/bzmore/bzless |
| .xz | xz | xzcat/xzgrep/xzmore/xzless |
| .tar.gz | tar打包,gzip压缩 | |
| .tar.bz2 | tar打包,bzip2压缩 | |
| .tar.xz | tar打包,xz压缩 |
以下压缩软件压缩比按顺序越来越好,时间夜宵好得多,功能也越高级
gzip
gzip, gunzip, zcat - compress or expand files
gzip [ -acdkltv#] [-S suffix] [ name ... ]
gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ... ]
zcat [ -fhLV ] [ name ... ]
-c --stdout --to-stdout
Write output on standard output; keep original files unchanged.
-d --decompress --uncompress
Decompress.
-v --verbose
Verbose. Display the name and percentage reduction for each file compressed or decompressed.
-t --test
Test. Check the compressed file integrity, but don't decompress them.
-k --keep
Keep (don't delete) input files during compression or decompression.
#也就是说默认会删除源文件
-f --force overwrite existing output files
-# --fast --best
Regulate the speed of compression using the specified digit #, where -1 or --fast indicates the fastest compression method (less compression) and -9 or --best indicates the slowest compression method (best compression). The default compression level is -6 (that is, biased towards high compression at expense of speed).
-l --list
For each compressed file, list the following fields:
compressed size: size of the compressed file
uncompressed size: size of the uncompressed file
ratio: compression ratio (0.0% if unknown)
-v --verbose
Verbose. Display the name and percentage reduction for each file compressed or decompressed. uncompressed_name: name of the uncompressed file
bzip2
bzip2, bunzip2 - a block-sorting file compressor, v1.0.8
bzcat - decompresses files to stdout
bzip2 [ -cdfkqtvVL# ] [ filenames ... ]
#上面选项意义全部与gizp相同
-z --compress Compress. This is the default operation mode when no operation mode option is specified
-s --small use less memory (at most 2500k)
xz
xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files
xz [-zdtclkf#] [file...]
unxz is equivalent to xz --decompress.
xzcat is equivalent to xz --decompress --stdout.
lzma is equivalent to xz --format=lzma.
unlzma is equivalent to xz --format=lzma --decompress.
zcat is equivalent to xz --format=lzma --decompress --stdout.
#上述选项全部与gzip意义相同
-z --compress #与bzip2相同
打包
tar
tar - an archiving utility
tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tape archiver), which can be located either on the local or on a remote machine.
tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG...]
tar -A [OPTIONS] ARCHIVE ARCHIVE
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
tar -d [-f ARCHIVE] [OPTIONS] [FILE...]
tar -t [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar -r [-f ARCHIVE] [OPTIONS] [FILE...]
tar -u [-f ARCHIVE] [OPTIONS] [FILE...]
tar -x [-f ARCHIVE] [OPTIONS] [MEMBER...] #提取特定文件MEMBER(可首先用-t查看文件名MEMBER)
opteration mode
-c, --create
Create a new archive. Arguments supply the names of the files to be archived. Directories are archived recur‐
sively, unless the --no-recursion option is given.
-t, --list
List the contents of an archive. Arguments are optional. When given, they specify the names of the members to
list.
-x, --extract, --get
Extract files from an archive. Arguments are optional. When given, they specify names of the archive members to
be extracted.
-Adru #参看man page
-f, --file=ARCHIVE
Use archive file or device ARCHIVE.
-p, --preserve-permissions, --same-permissions
extract information about file permissions (default for superuser)
-P, --absolute-names
Don't strip leading slashes from file names when creating archives.
-j, --bzip2
Filter the archive through bzip2(1).
-J, --xz
Filter the archive through xz(1).
-Z, --compress, --uncompress
Filter the archive through compress(1).
-v, --verbose
Verbosely list files processed
-t, --list
List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.
-C, --directory=DIR #用于解包到指定目录DIR
Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow.
--exclude=PATTERN #排除符合PATTERN的文件,不打包
Exclude files matching PATTERN, a glob(3)-style wildcard pattern.
-N, --newer=DATE, --after-date=DATE
Don\'t replace existing files that are newer than their archive copies.
--newer-mtime=DATE
Work on files whose data changed after the DATE. If DATE starts with / or . it is taken to be a file name; the mtime of that file is used as the date.

浙公网安备 33010602011771号