LoginSignup
1
0

データ管理をもっと簡単に:splitでファイルを効率的に分割する

Last updated at Posted at 2024-03-20

データのサイズが大きくなるにつれて、その管理は複雑になりがちです。特に、大量の情報を含むファイルを扱う際、サイズの大きさが作業を妨げる原因となることがあります。

split コマンドは、特に大きなファイルを小さなファイルに分割する際に役立つUNIX系オペレーティングシステム(LinuxやmacOSなど)の標準ツールです。これは、ファイルのサイズが大きすぎて扱いにくい、または特定の制限(例えばメールでの送信制限)に適合させる必要がある場合に便利です。

なぜファイルを分割するのか

ファイルを分割する理由は多岐にわたりますが、主なものは以下の通りです:

  • 転送の容易さ:大きなファイルはメールやオンラインストレージサービスを介して共有する際に問題となることがあります。分割すれば、これらの制限を回避できます。
  • バックアップの簡易化:小さなファイルは、バックアップが容易で、特定のデータの復元も迅速に行えます。
  • パフォーマンスの向上:特にテキストエディタやデータベースでの処理時、小さなファイルの方が読み込みや書き込みが速くなります。

split基本的な使い方

基本的なsplit コマンドの形式は以下のとおりです。

split [オプション] [入力ファイル] [出力ファイルの接頭辞]
  • [入力ファイル]は分割したい元のファイルの名前です。
  • [出力ファイルの接頭辞]は、分割されたファイルの名前の始まりの部分で、指定しない場合はデフォルトでxが使用されます。

サイズ指定による分割

ァイルを特定のサイズごとに分割するには、-b(または--bytes)オプションに続けてサイズを指定します。サイズの指定には、K(キロバイト)、M(メガバイト)、G(ギガバイト)などの単位を使用できます。

例えば、file.datを10MBごとに分割し、出力ファイルの接頭辞としてpart_を使用するには、次のコマンドを実行します。

split -b 10M file.dat part_

このコマンドは、file.datを10MBのファイルに分割し、part_aa、part_ab、part_ac…のように名付けられたファイルを作成します。

行数指定による分割

ファイルを特定の行数ごとに分割する場合は、-l(または--lines)オプションを使用します。たとえば、ファイルを1000行ごとに分割するには、以下のコマンドを使用します。

split -l 1000 file.dat part_

このコマンドは、file.datを1000行ごとに分割し、それぞれpart_aa、part_ab、part_ac…と名付けられたファイルを生成します。

MySQLのquery.logファイル(6G)を1GBごとに分割する

[root@xxxxx test]# split -b 1G query.log query_ --verbose
creating file ‘query_aa’
creating file ‘query_ab’
creating file ‘query_ac’
creating file ‘query_ad’
creating file ‘query_ae’
creating file ‘query_af’
creating file ‘query_ag’
[root@xxxxx test]# ls -alh
total 13G
drwxr-xr-x   2 root root  135 Mar 20 13:27 .
drwxrwxrwt. 11 root root 284K Mar 20 13:28 ..
-rw-r--r--   1 root root 1.0G Mar 20 13:25 query_aa
-rw-r--r--   1 root root 1.0G Mar 20 13:25 query_ab
-rw-r--r--   1 root root 1.0G Mar 20 13:26 query_ac
-rw-r--r--   1 root root 1.0G Mar 20 13:26 query_ad
-rw-r--r--   1 root root 1.0G Mar 20 13:26 query_ae
-rw-r--r--   1 root root 1.0G Mar 20 13:27 query_af
-rw-r--r--   1 root root 235M Mar 20 13:27 query_ag
-rw-r--r--   1 root root 6.3G Mar 19 18:43 query.log

split 詳細確認

split --help
Usage: split [OPTION]... [FILE [PREFIX]]
Output pieces of FILE to PREFIXaa, PREFIXab, ...;
default size is 1000 lines, and default PREFIX is 'x'.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   generate suffixes of length N (default 2)
      --additional-suffix=SUFFIX  append an additional SUFFIX to file names
  -b, --bytes=SIZE        put SIZE bytes per output file
  -C, --line-bytes=SIZE   put at most SIZE bytes of records per output file
  -d                      use numeric suffixes starting at 0, not alphabetic
      --numeric-suffixes[=FROM]  same as -d, but allow setting the start value
  -x                      use hex suffixes starting at 0, not alphabetic
      --hex-suffixes[=FROM]  same as -x, but allow setting the start value
  -e, --elide-empty-files  do not generate empty output files with '-n'
      --filter=COMMAND    write to shell COMMAND; file name is $FILE
  -l, --lines=NUMBER      put NUMBER lines/records per output file
  -n, --number=CHUNKS     generate CHUNKS output files; see explanation below
  -t, --separator=SEP     use SEP instead of newline as the record separator;
                            '\0' (zero) specifies the NUL character
  -u, --unbuffered        immediately copy input to output with '-n r/...'
      --verbose           print a diagnostic just before each
                            output file is opened
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

CHUNKS may be:
  N       split into N files based on size of input
  K/N     output Kth of N to stdout
  l/N     split into N files without splitting lines/records
  l/K/N   output Kth of N to stdout without splitting lines/records
  r/N     like 'l' but use round robin distribution
  r/K/N   likewise but only output Kth of N to stdout

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/split>
or available locally via: info '(coreutils) split invocation'

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0