WindowsでLinuxコマンドを使う - busybox

スポンサーリンク
スポンサーリンク

Linuxコマンドを1つのプログラムに詰め込んだ busybox というツールがあります。
Windows版には busybox-w32 があり、これを使うと WindowsでLinuxコマンドを使うことが出来ます。

スポンサーリンク

ダウンロード

ダウンロードは次のサイトの「The latest version is always busybox.exe. 」から行います。

busybox-w32

直リンは busybox.exe です。

busybox-w32 はインストール不要です。
ダウンロードした busybox.exe をそのまま実行します。

コマンドの使用

busybox では次のコマンドを使用できます。

ar, ash, awk, base64, basename, bash, bunzip2, bzcat, bzip2,
cal, cat, catv, chmod, cksum, clear, cmp, comm, cp, cpio, cut, date,
dc, dd, df, diff, dirname, dos2unix, dpkg-deb, du, echo, ed, egrep,
env, expand, expr, false, fgrep, find, fold, ftpget, ftpput, getopt,
grep, groups, gunzip, gzip, hd, head, hexdump, id, ipcalc, kill,
killall, less, ln, logname, ls, lzcat, lzma, lzop, lzopcat, man,
md5sum, mkdir, mktemp, mv, nc, od, patch, pgrep, pidof, printenv,
printf, ps, pwd, rev, rm, rmdir, rpm2cpio, sed, seq, sh, sha1sum,
sha256sum, sha3sum, sha512sum, shuf, sleep, sort, split, stat, strings,
sum, tac, tail, tar, tee, test, touch, tr, true, truncate, uname,
uncompress, unexpand, uniq, unix2dos, unlink, unlzma, unlzop, unxz,
unzip, usleep, uudecode, uuencode, vi, wc, wget, which, whoami, xargs,
xz, xzcat, yes, zcat

実行はWindowsのコマンドプロンプトから次の書式で行います。

busybox コマンド 引数
例)busybox ls -a

または、busybox.exe をコマンド名にリネームすると、そのコマンドとして動作します。
例えば、ls.exe とリネームして ls.exe を実行すると ls として動作します。
grep.exe とリネームして実行すると grep として動作します。

busybox を –install 付きで次のように実行すると、指定したディレクトリ下にコマンドが
ar.exe, ash.exe, awk.exe, base64.exe, basename.exe, bash.exe, … のように作成されます。

busybox --install d:\tmp\directory

ちなみにこのコマンドはハードリンクという機能で作成され、busybox.exe と作成先はNTFSフォーマットされた場所に無いといけません。

なお、busybox-w32 ではコマンドラインでワイルドカードは使用できません。
次のようにエラーになります。

D:\tmp>busybox.exe ls *.txt
ls: *.txt: No such file or directory

これは busybox-w32 の制限で、サイトでも言及されています。

https://github.com/rmyorston/busybox-w32/blob/master/README.md

Limitations
    Wildcard expansion is disabled by default, though it can be turned on at compile time. This only affects command line arguments to the binary: the BusyBox shell has full support for wildcards.

また、日本語の使用ですが vi では表示が化けました。

その他、これまで試した限りでは、コマンドの引数やファイル名には日本語は問題無く使えました。
grep ではファイル内の日本語も検索出来ています。

>busybox.exe cp 島根.txt 鳥取.txt
>busybox.exe ls
busybox.exe  鳥取.txt     島根.txt
>busybox cat 鳥取.txt
摩尼寺
鳥取砂丘
すなば珈琲
>busybox.exe grep 砂丘 鳥取.txt
鳥取砂丘

bash/shの使用

busybox-w32 ではシェル bash/sh も使うことが出来ます。

シェル環境でのルートディレクトリは、busybox.exe が存在するドライブのルートになります。
bash/sh を -l または –login オプションを付けてログインシェルとして動作させた場合は、Windowsユーザフォルダが存在するドライブ(通常はCドライブ)のルートになります。

ユーザ名は Windowsのユーザアカウント、ホームディレクトリは Windowsユーザフォルダ(C:\Users\ユーザ名)になります。

なお、bash/sh内では日本語の使用に問題があります。
日本語の認識で問題があるようで、例えば次のようにエラーになります。

C:/ $ ls ソースコード.txt
ls: メ[スコード.txt: No such file or directory

一方、コマンドの使用ではダメだったワイルドカードは、bash/sh内では可能で、ls *.txt のような使い方が出来ます。

その他のLinuxコマンドの使用方法

下記でも、コマンドや bash を使う方法を紹介しています。

Windows で Linux のコマンドを使う Gow
Windows で bash や sshサーバを利用する

参考

busybox-w32
Windowsで仮想やCygwinなしでUnixコマンドを使う – Qiita
busybox-w32とWin32-OpenSSHでWindowsの作業が少し捗りそう – ytooyamaのブログ

コメント

タイトルとURLをコピーしました