上一篇教程写了 Linux压缩和解压zip ,因为内容太多,所以把 zip 和 rar 分开写,这里就来写 rar,rar 格式在 Windows 平台应该是用的最多的压缩格式了,国内的 Windows 电脑很多都在用 WinRAR。

RAR是一种专利文件格式,用于数据压缩与归档打包,开发者为尤金·罗谢尔(俄语:Евгений Лазаревич Рошал,拉丁转写:Yevgeny Lazarevich Roshal),RAR的全名是“Roshal ARchive”,即“罗谢尔的归档”之意。首个公开版本RAR 1.3发布于1993年。

我用来演示的系统版本是 CentOS7 64位,因为 rar 是收费软件,且不开源,所以一般操作系统都不会内置 rar 功能,目前 Linux 平台的 rar 分为 32位 和 64位 版本,我用的是 64位 的,所以只能下载 64位 版,而且 CentOS 的 yum 也没有 rar,所以只能手动下载安装。

下载 CentOS 64位rar:

wget http://rpmfind.net/linux/atrpms/f20-x86_64/atrpms/stable/rar-4.2.0-4.fc20.x86_64.rpm

如果要下载其他版本的 rpm 可以上 http://rpmfind.net/linux/rpm2html/search.php?query=rar ,选择对应的版本下载,

安装:

rpm -ivh rar-4.2.0-4.fc20.x86_64.rpm

如果要更详细的了解 rpm 的安装和管理可以看 Linux rpm包管理 ,下面是安装信息:

[root@localhost home]# rpm -ivh rar-4.2.0-4.fc20.x86_64.rpm
警告:rar-4.2.0-4.fc20.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 66534c2b: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:rar-4.2.0-4.fc20                 ################################# [100%]

安装完成后可以输入 rar,如果出现 rar 相关的帮助信息说明安装成功。

如果是 Ubuntu 这一类系统的话直接输入:

apt-get install rar

可以自动下载安装适合的版本。

无论是 CentOS 还是 Ubuntu 的 rar 使用命令都是一样的。

压缩

创建 rar 压缩: rar a 新文件名 要压缩的文件名 ,注意!这里的 a 前面没有 -,下面把 home 目录下的 1.txt 压缩位 1.rar

[root@localhost home]# rar a 1.rar 1.txt

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive 1.rar

Adding    1.txt                                                       OK 
Done

因为 rar 是收费软件,我这里使用的是试用版,所以会提示注册,但是是可以正常压缩的,所以在 Linux 平台如果有其他选择的话还是不推荐用 rar。

下面把 1.txtrar-4.2.0-4.fc20.x86_64.rpm 两个文件压缩为 1.rar

[root@localhost home]# rar a 1.rar 1.txt rar-4.2.0-4.fc20.x86_64.rpm

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive 1.rar

Adding    1.txt                                                       OK 
Adding    rar-4.2.0-4.fc20.x86_64.rpm                                 OK 
Done

下面把 dir 目录压缩为 dir.rar

[root@localhost home]# rar a dir.rar dir

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive dir.rar

Adding    dir/1.js                                                    OK 
Adding    dir/2.js                                                    OK 
Adding    dir                                                         OK 
Done

给rar包添加文件: rar u rar包文件名 要添加的文件名 ,下面把 2.js 添加到 1.rar 中:

[root@localhost home]# rar u 1.rar 2.js

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Updating archive 1.rar

Adding    2.js                                                        OK 
Done

加密压缩:rar a -p 新文件名 要压缩的文件名 ,执行命令后会提示输入密码,注意!输入密码的时候是看不到密码的,也看不到*,下面把 1.js 加密压缩为 1.rar

[root@localhost home]# rar a -p 1.rar 1.js

Enter password (will not be echoed): 

Reenter password: 


RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive 1.rar

Adding    1.js                                                        OK 
Done

删除rar包内的文件: rar d rar包文件名 要删除的文件名 ,下面删除 1.rar 中的 1.js 文件:

[root@localhost home]# rar d 1.rar 1.js

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Deleting from 1.rar
Deleting 1.js
Done

解压

把文件解压到当前目录: rar e rar包文件名 ,下面解压 1.rar

[root@localhost home]# rar e 1.rar

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help


Extracting from 1.rar

Extracting  2.js                                                      OK 
All OK

查看 rar 包内的文件信息: rar l rar包文件名 ,下面查看 1.rar 中的文件信息:

[root@localhost home]# rar l 1.rar

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help

Archive 1.rar

 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 2.js               58       67 115% 23-02-19 16:59 -rw-r--r-- F74C51F6 m3b 2.9
 1.js               91       14  15% 23-02-19 17:39 -rw-r--r-- 3C4274E3 m3b 2.9
-------------------------------------------------------------------------------
    2              149       81  54%

把文件解压到指定目录: rar x rar包文件名 目录名 ,下面解压 1.rarroot 目录:

[root@localhost home]# rar x 1.rar /root

RAR 4.20   Copyright (c) 1993-2012 Alexander Roshal   9 Jun 2012
Trial version             Type RAR -? for help


Extracting from 1.rar

Extracting  /root/2.js                                                OK 
Extracting  /root/1.js                                                OK 
All OK

下面是 rar 常用的一些参数:

参数说明
a压缩文件
c添加注释
d删除压缩包内的文件
e把文件解压到当前目录
k锁定压缩包
l查看压缩包内的文件信息
r修复压缩包
t测试压缩包
u更新压缩包内的文件
x把文件解压到指定目录

上面表格中只列出了一部分参数,rar 还有很多参数,因为 rar 是付费软件,所以有的功能需要付费才能使用,下面是完整的 rar 帮助说明:

<Commands>
  a             Add files to archive
  c             Add archive comment
  cf            Add files comment
  ch            Change archive parameters
  cw            Write archive comment to file
  d             Delete files from archive
  e             Extract files to current directory
  f             Freshen files in archive
  i[par]=<str>  Find string in archives
  k             Lock archive
  l[t,b]        List archive [technical, 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,b]        Verbosely list archive [technical,bare]
  x             Extract files with full path

<Switches>
  -             Stop switches scanning
  @[+]          Disable [enable] file lists
  ad            Append archive name to destination path
  ag[format]    Generate archive name using the current date
  ai            Ignore file attributes
  ap<path>      Set path inside archive
  as            Synchronize archive contents
  av            Put authenticity verification (registered versions only)
  av-           Disable authenticity verification check
  c-            Disable comments show
  cfg-          Disable read configuration
  cl            Convert names to lower case
  cu            Convert names to upper case
  df            Delete files after archiving
  dh            Open shared files
  ds            Disable name sort for solid archive
  dw            Wipe files after archiving
  e[+]<attr>    Set file exclude and include attributes
  ed            Do not add empty directories
  en            Do not put 'end of archive' block
  ep            Exclude paths from names
  ep1           Exclude base directory from names
  ep3           Expand paths to full including the drive letter
  f             Freshen files
  hp[password]  Encrypt both file data and headers
  id[c,d,p,q]   Disable messages
  ierr          Send all messages to stderr
  ilog[name]    Log errors to file (registered versions only)
  inul          Disable all messages
  isnd          Enable sound
  k             Lock archive
  kb            Keep broken extracted files
  log[f][=name] Write names to log file
  m<0..5>       Set compression level (0-store...3-default...5-maximal)
  mc<par>       Set advanced compression parameters
  md<size>      Dictionary size in KB (64,128,256,512,1024,2048,4096 or A-G)
  ms[ext;ext]   Specify file types to store
  mt<threads>   Set the number of threads
  n<file>       Include only specified file
  n@            Read file names to include from stdin
  n@<list>      Include files listed in specified list file
  o[+|-]        Set the overwrite mode
  ol            Save symbolic links as the link instead of the file
  or            Rename files automatically
  ow            Save or restore file owner and group
  p[password]   Set password
  p-            Do not query password
  r             Recurse subdirectories
  r-            Disable recursion
  r0            Recurse subdirectories for wildcard names only
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[<N>,v[-],e] Create solid archive
  s-            Disable solid archiving
  sc<chr>[obj]  Specify the character set
  sfx[name]     Create SFX archive
  si[name]      Read data from standard input (stdin)
  sl<size>      Process files with size less than specified
  sm<size>      Process files with size more than specified
  t             Test files after archiving
  ta<date>      Process files modified after <date> in YYYYMMDDHHMMSS format
  tb<date>      Process files modified before <date> in YYYYMMDDHHMMSS format
  tk            Keep original archive time
  tl            Set archive time to latest file
  tn<time>      Process files newer than <time>
  to<time>      Process files older than <time>
  ts<m,c,a>[N]  Save or restore file time (modification, creation, access)
  u             Update files
  v             Create volumes with size autodetection or list all volumes
  v<size>[k,b]  Create volumes with size=<size>*1000 [*1024, *1]
  ver[n]        File version control
  vn            Use the old style volume naming scheme
  vp            Pause before each volume
  w<path>       Assign work directory
  x<file>       Exclude specified file
  x@            Read file names to exclude from stdin
  x@<list>      Exclude files listed in specified list file
  y             Assume Yes on all queries
  z[file]       Read archive comment from file

相关文章:

版权声明:本文为原创文章,版权归 Mr. Ma's Blog 所有,转载请联系博主获得授权!
本文地址:https://www.misterma.com/archives/363/
如果您对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。
最后编辑:2019年12月15日 ©著作权归作者所有

发表评论

正在加载 Emoji
×