Linuxでrarファイルを圧縮・解凍する方法(CentOS)

rarファイルは、zipよりも圧縮率の高いデータ圧縮のフォーマットで、複数のファイルに分割して圧縮できたり、圧縮ファイルが破損していても少しであれば修復できるのが特徴です。

RARLABによって開発されていて、解凍はフリーウェアです。
圧縮するのは無料のトライアルライセンスのツールで可能です。

圧縮するツールは有料、解凍するツールは無料という収益モデルのようです。

圧縮するためのツールはWinRARで、名前の通りWindowsがメインのソフトウェアです。
その他のOSはコマンドライン版があります。

Linuxでrarを圧縮するには『rar』、解凍するには『unrar』があります。

rarlinuxのインストール
圧縮するコマンド
解凍するコマンド
Cron実行時の注意点
rarコマンドのヘルプ
rarコマンドのヘルプ(日本語訳)
unrarコマンドのヘルプ
unrarコマンドのヘルプ(日本語訳)
unrarコマンドのエラーコード

rarlinuxのインストール

ファイル取得、インストールのためのコマンドをインストールします。

dnf -y install wget tar make

RARLABのサイトから最新版のURLを調べます。
RARLABダウンロード

RAR x.xx for Linux x64をダウンロードします。

wget https://www.rarlab.com/rar/rarlinux-x64-6.0.1.tar.gz

ダウンロードしたファイルを解凍します。

tar xzfv rarlinux-x64-6.0.1.tar.gz -C /usr/local/src

makeしてunrarをインストールします。

cd /usr/local/src/rar/
make
make install

makeもmake installも動作は同じようです。

# make
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib
cp rar unrar /usr/local/bin
cp rarfiles.lst /etc
cp default.sfx /usr/local/lib

# make install
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib
cp rar unrar /usr/local/bin
cp rarfiles.lst /etc
cp default.sfx /usr/local/lib

圧縮するコマンド

$ rar a 【出力先ファイル】 【圧縮するファイル】

$ rar a hoge.rar hoge.txt

解凍するコマンド

$ unrar x 【解凍するファイル】 【出力先フォルダ】

$ unrar x hoge.rar /home/user/hoge/

出力先にファイルが存在した場合に上書きする場合

$ unrar -o+ x hoge.rar /home/user/hoge/

出力先にファイルが存在した場合に上書きしない場合

$ unrar -o- x hoge.rar /home/user/hoge/

Cron実行時の注意点

コマンドの実行パスは
/usr/local/bin/rar
/usr/local/bin/unrar
になります。

/usr/local/binにパスが通っていない(PATH変数が設定されていない)と、cron実行時にコマンドを使うことができません。

cronでコマンドを使う時は、crontabの先頭に

PATH=/usr/local/bin:$PATH

のように書くか、コマンドを絶対パスで記述するようにします。

コマンドを絶対パスで記述すると、環境を移行したときにコマンドの絶対パスが変わってシステムが動作しなくなることがあるのであまりやらないほうがよいです。

シンボリックリンクを設置するとPATHの変更をする必要がなく、影響範囲が小さくなります。

# ln -s /usr/local/bin/rar /usr/bin/rar
# ln -s /usr/local/bin/unrar /usr/bin/unrar

rarコマンドのヘルプ

$ rar

RAR 6.01   Copyright (c) 1993-2021 Alexander Roshal   7 Apr 2021
Trial version             Type 'rar -?' for help

Usage:     rar <command> -<switch 1> -<switch N> <archive> <files...>
               <@listfiles...> <path_to_extract\>

<Commands>
  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]=<str>  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

<Switches>
  -             Stop switches scanning
  @[+]          Disable [enable] file lists
  ad[1,2]       Alternate 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
  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
  ht[b|c]       Select hash type [BLAKE2,CRC32] for file checksum
  id[c,d,n,p,q] Display or disable messages
  ierr          Send all messages to stderr
  ilog[name]    Log errors to file
  inul          Disable all messages
  isnd[-]       Control notification sounds
  iver          Display the version number
  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)
  ma[4|5]       Specify a version of archiving format
  mc<par>       Set advanced compression parameters
  md<n>[k,m,g]  Dictionary size in KB, MB or GB
  ms[ext;ext]   Specify file types to store
  mt<threads>   Set the number of threads
  n<file>       Additionally filter included files
  n@            Read additional filter masks from stdin
  n@<list>      Read additional filter masks from list file
  o[+|-]        Set the overwrite mode
  oh            Save hard links as the link instead of the file
  oi[0-4][:min] Save identical files as references
  ol[a]         Process symbolic links as the link [absolute paths]
  or            Rename files automatically
  ow            Save or restore file owner and group
  p[password]   Set password
  p-            Do not query password
  qo[-|+]       Add quick open information [none|force]
  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[mcao]<d>   Process files modified after <d> YYYYMMDDHHMMSS date
  tb[mcao]<d>   Process files modified before <d> YYYYMMDDHHMMSS date
  tk            Keep original archive time
  tl            Set archive time to latest file
  tn[mcao]<t>   Process files newer than <t> time
  to[mcao]<t>   Process files older than <t> time
  ts[m,c,a,p]   Save or restore time (modification, creation, access, preserve)
  u             Update files
  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

rarコマンドのヘルプ(日本語訳)

<コマンド>
  aアーカイブするファイルを追加する
  cアーカイブコメントを追加する
  chアーカイブパラメータを変更する
  cwアーカイブコメントをファイルに書き込む
  dアーカイブからファイルを削除する
  eアーカイブされたパスなしでファイルを抽出する
  fアーカイブ内のファイルを更新する
  i [par] = <str>アーカイブで文字列を検索する
  kアーカイブをロックする
  l [t [a]、b]アーカイブの内容を一覧表示する[technical [all]、bare]
  m [f]アーカイブに移動[ファイルのみ]
  pファイルをstdoutに出力します
  rアーカイブの修復
  rc不足しているボリュームを再構築します
  rnアーカイブファイルの名前を変更する
  rr [N]データ回復レコードを追加します
  rv [N]リカバリボリュームを作成します
  s [name |-]アーカイブをSFXとの間で変換する
  tアーカイブファイルをテストします
  uアーカイブ内のファイルを更新します
  v [t [a]、b]アーカイブの内容を詳細に一覧表示する[technical [all]、bare]
  xフルパスでファイルを抽出する

<スイッチ>
  -スイッチのスキャンを停止します
  @ [+]ファイルリストを無効にする[有効にする]
  ad [1,2]代替宛先パス
  ag [format]現在の日付を使用してアーカイブ名を生成します
  aiファイル属性を無視する
  ap <path>アーカイブ内のパスを設定します
  アーカイブコンテンツの同期として
  c-コメント表示を無効にする
  cfg-読み取り構成を無効にする
  cl名前を小文字に変換する
  cu名前を大文字に変換する
  dfアーカイブ後にファイルを削除する
  dh共有ファイルを開く
  dsソリッドアーカイブの名前の並べ替えを無効にする
  dwアーカイブ後にファイルをワイプする
  e [+] <attr>ファイルの除外属性と包含属性を設定します
  ed空のディレクトリを追加しないでください
  ja「アーカイブの終わり」ブロックを配置しないでください
  ep名前からパスを除外する
  ep1名前からベースディレクトリを除外する
  ep3ドライブ文字を含むパスを完全に展開します
  fファイルを更新する
  hp [password]ファイルデータとヘッダーの両方を暗号化する
  ht [b | c]ファイルチェックサムのハッシュタイプ[BLAKE2、CRC32]を選択します
  id [c、d、n、p、q]メッセージを表示または無効にする
  ierrすべてのメッセージをstderrに送信します
  ilog [name]エラーをファイルに記録します
  inulすべてのメッセージを無効にする
  isnd [-]制御通知音
  iverバージョン番号を表示します
  kアーカイブをロックする
  kb壊れた抽出ファイルを保持する
  log [f] [= name]ログファイルに名前を書き込む
  m <0..5>圧縮レベルを設定します(0-ストア... 3-デフォルト... 5-最大)
  ma [4 | 5]アーカイブ形式のバージョンを指定します
  mc <par>高度な圧縮パラメータを設定します
  md <n> [k、m、g]辞書のサイズ(KB、MB、またはGB)
  ms [ext; ext]保存するファイルの種類を指定します
  mt <threads>スレッド数を設定します
  n <file>さらに含まれるファイルをフィルタリングする
  o [+ |-]上書きモードを設定します
  oh ハードリンクをファイルではなくリンクとして保存する
  oi [0-4] [:min]同じファイルを参照として保存します
  ol [a]シンボリックリンクをリンクとして処理する[絶対パス]
  またはファイルの名前を自動的に変更します
  owファイルの所有者とグループを保存または復元します
  p [パスワード]パスワードを設定する
  p-パスワードを照会しない
  qo [-| +]クイックオープン情報を追加[なし|強制]
  rサブディレクトリを再帰します
  r-再帰を無効にする
  r0ワイルドカード名のみの再帰サブディレクトリ
  rr [N]データ回復レコードを追加します
  rv [N]リカバリボリュームを作成します
  s [<N>、v [-]、e]堅実なアーカイブを作成する
  s-ソリッドアーカイブを無効にする
  sc <chr> [obj]文字セットを指定します
  sfx [name] SFXアーカイブを作成します
  si [name]標準入力(stdin)からデータを読み取ります
  sl <size>指定されたサイズよりも小さいサイズのファイルを処理します
  sm <size>指定されたサイズを超えるサイズのファイルを処理します
  tアーカイブ後にファイルをテストする
  ta [mcao] <d> <d> YYYYMMDDHHMMSSの日付以降に変更されたプロセスファイル
  tb [mcao] <d> <d> YYYYMMDDHHMMSS日付より前に変更されたプロセスファイル
  tk元のアーカイブ時間を保持する
  tlアーカイブ時間を最新のファイルに設定します
  tn [mcao] <t> <t>時間より新しいファイルを処理します
  to [mcao] <t> <t>時間より古いファイルを処理します
  ts [m、c、a、p]時間を節約または復元する(変更、作成、アクセス、保存)
  uファイルを更新します
  v <size> [k、b] size = <size> * 1000 [* 1024、* 1]でボリュームを作成します
  ver [n]ファイルバージョン管理
  vn古いスタイルのボリューム命名スキームを使用する
  vp各ボリュームの前で一時停止
  w <path>作業ディレクトリを割り当てます
  x <file>指定されたファイルを除外します
  x @ファイル名を読み取ってstdinから除外する
  x @ <list>指定されたリストファイルにリストされているファイルを除外します
  yすべてのクエリで「はい」と見なします
  z [ファイル]ファイルからアーカイブコメントを読み取る

unrarコマンドのヘルプ

$ unrar

UNRAR 6.01 freeware      Copyright (c) 1993-2021 Alexander Roshal

Usage:     unrar <command> -<switch 1> -<switch N> <archive> <files...>
               <@listfiles...> <path_to_extract\>

<Commands>
  e             Extract files without archived paths
  l[t[a],b]     List archive contents [technical[all], bare]
  p             Print file to stdout
  t             Test archive files
  v[t[a],b]     Verbosely list archive contents [technical[all],bare]
  x             Extract files with full path

<Switches>
  -             Stop switches scanning
  @[+]          Disable [enable] file lists
  ad[1,2]       Alternate destination path
  ag[format]    Generate archive name using the current date
  ai            Ignore file attributes
  ap<path>      Set path inside archive
  c-            Disable comments show
  cfg-          Disable read configuration
  cl            Convert names to lower case
  cu            Convert names to upper case
  dh            Open shared files
  ep            Exclude paths from names
  ep3           Expand paths to full including the drive letter
  f             Freshen files
  id[c,d,n,p,q] Display or disable messages
  ierr          Send all messages to stderr
  inul          Disable all messages
  kb            Keep broken extracted files
  n<file>       Additionally filter included files
  n@            Read additional filter masks from stdin
  n@<list>      Read additional filter masks from list file
  o[+|-]        Set the overwrite mode
  ol[a]         Process symbolic links as the link [absolute paths]
  or            Rename files automatically
  ow            Save or restore file owner and group
  p[password]   Set password
  p-            Do not query password
  r             Recurse subdirectories
  sc<chr>[obj]  Specify the character set
  sl<size>      Process files with size less than specified
  sm<size>      Process files with size more than specified
  ta[mcao]<d>   Process files modified after <d> YYYYMMDDHHMMSS date
  tb[mcao]<d>   Process files modified before <d> YYYYMMDDHHMMSS date
  tn[mcao]<t>   Process files newer than <t> time
  to[mcao]<t>   Process files older than <t> time
  ts[m,c,a,p]   Save or restore time (modification, creation, access, preserve)
  u             Update files
  v             List all volumes
  ver[n]        File version control
  vp            Pause before each volume
  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

unrarコマンドのヘルプ(日本語訳)

使用法:unrar <コマンド>-<スイッチ1>-<スイッチN> <アーカイブ> <ファイル...>
               <@listfiles ...> <path_to_extract \>

<コマンド>
  eアーカイブされたパスなしでファイルを抽出する
  l [t [a]、b]アーカイブの内容を一覧表示する[technical [all]、bare]
  pファイルをstdoutに出力します
  tアーカイブファイルをテストします
  v [t [a]、b]アーカイブの内容を詳細に一覧表示する[technical [all]、bare]
  xフルパスでファイルを抽出する

<スイッチ>
  -スイッチのスキャンを停止します
  @ [+]ファイルリストを無効にする[有効にする]
  ad [1,2]代替宛先パス
  ag [format]現在の日付を使用してアーカイブ名を生成します
  aiファイル属性を無視する
  ap <path>アーカイブ内のパスを設定します
  c-コメント表示を無効にする
  cfg-読み取り構成を無効にする
  cl名前を小文字に変換する
  cu名前を大文字に変換する
  dh共有ファイルを開く
  ep名前からパスを除外する
  ep3ドライブ文字を含むパスを完全に展開します
  fファイルを更新する
  id [c、d、n、p、q]メッセージを表示または無効にする
  ierrすべてのメッセージをstderrに送信します
  inulすべてのメッセージを無効にする
  kb壊れた抽出ファイルを保持する
  n <file>さらに含まれるファイルをフィルタリングする
  n @stdinから追加のフィルターマスクを読み取ります
  n @ <list>リストファイルから追加のフィルターマスクを読み取ります
  o [+ |-]上書きモードを設定します
  ol [a]シンボリックリンクをリンクとして処理する[絶対パス]
  またはファイルの名前を自動的に変更します
  owファイルの所有者とグループを保存または復元します
  p [パスワード]パスワードを設定する
  p-パスワードを照会しない
  rサブディレクトリを再帰します
  sc <chr> [obj]文字セットを指定します
  sl <size>指定されたサイズよりも小さいサイズのファイルを処理します
  sm <size>指定されたサイズを超えるサイズのファイルを処理します
  ta [mcao] <d> <d> YYYYMMDDHHMMSSの日付以降に変更されたプロセスファイル
  tb [mcao] <d> <d> YYYYMMDDHHMMSS日付より前に変更されたプロセスファイル
  tn [mcao] <t> <t>時間より新しいファイルを処理します
  to [mcao] <t> <t>時間より古いファイルを処理します
  ts [m、c、a、p]時間を節約または復元する(変更、作成、アクセス、保存)
  uファイルを更新します
  vすべてのボリュームをリストします
  ver [n]ファイルバージョン管理
  vp各ボリュームの前で一時停止
  x <file>指定されたファイルを除外します
  x @ファイル名を読み取ってstdinから除外する
  x @ <list>指定されたリストファイルにリストされているファイルを除外します
  yすべてのクエリで「はい」と見なします

unrarコマンドのエラーコード

エラーコード 内容
0 normal; no errors or warnings detected.
正常;エラーや警告は検出されませんでした。
1 one or more warning errors were encountered, but processing completed anyway.
1つ以上の警告エラーが発生しましたが、処理はとにかく完了しました。
2 fatal error encountered during rar archive processing.
rarアーカイブ処理中に致命的なエラーが発生しました。
3 CRC error. Internal archive checksum does not match the data.
CRCエラー。内部アーカイブチェックサムがデータと一致しません。
4 Locking issue. Not used in unrar.
ロックの問題。 unrarでは使用されません。
5 unrar was unable to write data to disk.
unrarはデータをディスクに書き込むことができませんでした。
6 unrar was unable to open a file
unrarはファイルを開くことができませんでした
7 User error (like wrong switch, etc.)
ユーザーエラー(スイッチの間違いなど)
8 unrar was unable to allocate memory
unrarはメモリを割り当てることができませんでした
9 unrar was unable to create file
unrarはファイルを作成できませんでした
255 User terminated the process by using Ctrl+C
ユーザーがCtrl + Cを使用してプロセスを終了しました

関連記事

スポンサーリンク

RANDOM関数 乱数を求める

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る